测试人社区

打造迷你测试框架公开课

课程顾问联络

image

pytest示例

import pytest

from test_frameworks.demo import fun


def test_fun():
    assert fun(2, 1) == 2
    assert fun(2, 1) == 1


class TestFun:
    @classmethod
    def setup_class(cls):
        print("setup_class")

    def setup_method(self):
        print("setup_method")

    def test_fun_1(self):
        assert fun(2, 1) == 2

    def test_fun_2(self):
        assert fun(2, 1) == 1

    @pytest.mark.parametrize("first, second, expect", [
        (2, 1, 2),
        (10, 2, 5),
        (10, 5, 2)
    ])
    def test_params(self, first, second, expect):
        assert fun(first, second) == expect

    def teardown_method(self):
        print("teardown_method")

BasePage示例

from selenium.webdriver.remote.webdriver import WebDriver


def exception_handle(fun):
    def magic(*args, **kwargs):
        print(f"{fun.__name__} ( {args}, {kwargs})")
        res = fun(*args, **kwargs)
        print(f"{fun.__name__} finish")
        return res

    return magic


class BasePage:
    def __init__(self, driver: WebDriver):
        self.driver = driver

    @exception_handle
    def find(self, by, value):
        # todo: 异常处理
        return self.driver.find_element(by, value)

    def close(self):
        self.driver.quit()

Page定义

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

from test_frameworks.base_page import BasePage
from test_frameworks.search_page import SearchPage


class MainPage(BasePage):
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(10)
        self.driver.get("https://xueqiu.com/")

    def search(self, keyword):
        element_input=self.find(By.NAME, 'q')
        element_input.clear()
        element_input.send_keys(keyword)
        element_input.send_keys(Keys.ENTER)
        return SearchPage(self.driver)

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.webdriver import WebDriver

from test_frameworks.base_page import BasePage


class SearchPage(BasePage):

    def get_name(self):
        return self.find(By.CSS_SELECTOR, '.search__stock__bd span').text

    def search(self, keyword):
        element_input=self.find(By.NAME, 'q')
        #todo: clear不生效
        element_input.clear()
        print(element_input.text)
        element_input.send_keys(keyword)
        element_input.send_keys(Keys.ENTER)
        return self

测试用例定义

import pytest
import yaml
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

from test_frameworks.main_page import MainPage
from test_frameworks.search_page import SearchPage


class TestWeb:
    main_page: MainPage = None

    # @classmethod
    # def setup_class(cls):
    #     cls.main_page = MainPage()
    #
    def setup_method(self):
        self.main_page = MainPage()

    def test_search_by_po_sogo(self):
        assert self.main_page.search("sogo").get_name() == "搜狗"

    def test_search_by_po_alibaba(self):
        assert self.main_page.search("alibaba").get_name() == "阿里巴巴"

    @pytest.mark.parametrize("keyword, name", yaml.safe_load(open("test_frameworks/demo.yaml")))
    def test_search_by_po_dd(self, keyword, name):
        assert self.main_page.search(keyword).get_name() == name

    @pytest.mark.parametrize("keyword, name", [
        ("pdd", "拼多多"),
        ("xiaomi", "小米"),
        ("jd", "京东")
    ])
    def test_search_by_po_dd_yaml(self, keyword, name):
        assert self.main_page.search(keyword).get_name() == name

    def teardown_method(self):
        self.main_page.close()

1 Like