我需要用python3和selenium webdriver编写一些ui测试。使用以下测试用例,测试运行正常。但是,我的问题是我编写测试用例的更好方法是什么,以及如何通过" base"每个测试用例和pytest fixture函数之间的变量 我需要1:在每个测试用例之前打开主页,2:在每个测试用例之后重新加载主页并通过共享变量来减少代码" base"每个测试用例和pytest fixture功能之间。 导入pytest 来自modules.base import Home
class TestLogin(object):
def setup_method(self, method):
self.driver = WebDriver(desired_capabilities=desired_capabilities, command_executor=command_executor)
self.current_method_name = method.__name__
def teardown_method(self, method):
self.driver.close()
self.driver.quit()
@pytest.fixture(scope="function")
def loadpage():
self.base = Home(self.driver).open()
def loadLogin():
base.loadLogin()
def test_a(self):
base = Home(self.driver).open()
assert True == base.dotesta()
base.loadLogin()
def test_b(self):
base = Home(self.driver).open()
assert True == base.dotestb()
base.loadLogin()
def test_c(self):
base = Home(self.driver).open()
assert True == base.dotestc()
base.loadLogin()
def test_d(self):
base = Home(self.driver).open()
assert True == base.dotestd()
base.loadLogin()
答案 0 :(得分:1)
根据您当前的要求,您无需使用夹具。这是你的代码。
class TestLogin(object):
def setup_method(self, method):
self.driver = WebDriver(desired_capabilities=desired_capabilities, command_executor=command_executor)
self.current_method_name = method.__name__
self.base = Home(self.driver).open()
def teardown_method(self, method):
self.base.loadLogin()
self.driver.close()
self.driver.quit()
def test_a(self):
assert True == self.base.dotesta()
def test_b(self):
assert True == self.base.dotestb()
def test_c(self):
assert True == self.base.dotestc()
def test_d(self):
assert True == self.base.dotestd()
编辑: 要仅打开页面一次,请用
替换设置和拆卸 def setup_class(cls):
cls.driver = WebDriver(desired_capabilities=desired_capabilities, command_executor=command_executor)
cls.current_method_name = method.__name__
cls.base = Home(self.driver).open()
def teardown_method(cls):
cls.base.loadLogin()
cls.driver.close()
cls.driver.quit()
有关cls。
的详细信息