@pytest.fixture(scope="session")
def moduleSetup(request):
module_setup = Module_Setup()
request.addfinalizer(module_setup.teardown())
return module_setup
def test_1(moduleSetup):
print moduleSetup
print '...'
#assert 0
# def test_2(moduleSetup):
# print moduleSetup
# print '...'
# #assert 0
在conftest.py我有
class Module_Setup:
def __init__(self):
self.driver = webdriver.Firefox()
def teardown(self):
self.driver.close()
当我运行它时启动并关闭浏览器.
但是我也得到错误self =< CallInfo when ='teardown'exception:'nonetype'对象不可调用>,func =< function< lambda>在0x104580488>,当=’teardown’时
另外如果我想使用相同的驱动程序对象运行test_1和test_2测试,我需要使用范围模块或会话吗?
解决方法:
关于例外
使用request.addfinalizer()时,您应该传入对函数的引用.
request.addfinalizer(module_setup.teardown())
你应该这样称呼它:
request.addfinalizer(module_setup.teardown)
关于夹具范围
如果您的fixture允许在多个测试调用中重用,请使用“session”
范围.如果它只允许重用于一个模块中的测试,请使用“模块”范围.
替代夹具解决方案
你使用灯具的方式并不是pytest风格,它更像是unittest.
从您显示的代码看来,您需要的唯一想法是运行带驱动程序的Firefox,允许在测试中使用它,完成后,您需要关闭它.
这可以通过单个夹具完成:
@pytest.fixture(scope="session")
def firefox(request):
driver = webdriver.Firefox()
def fin():
driver.close()
request.addfinalizer(fin)
甚至更好地使用@ pytest.yield_fixture
@pytest.yield_fixture(scope="session")
def firefox(request):
driver = webdriver.Firefox()
yield driver
driver.close()
产量是位置,夹具停止执行,产生创建的值(驱动程序)到测试用例.
测试结束后(或更好,当我们的夹具的范围结束时),它
继续运行yield之后的指令并进行清理
工作.
在所有情况下,您可以按如下方式修改测试用例:
def test_1(firefox):
print moduleSetup
print '...'
并且moduleSetup fixture变得完全过时了.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。