pytest异常none类型对象不可调用
pytest exception none type object is not callable
在test1.py
中我有以下代码
@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' object is not callable>, func = <function <lambda> at 0x104580488>, when = 'teardown'
此外,如果我想 运行 使用相同的驱动程序对象同时测试 test_1
和 test_2
,我需要使用范围 module
或 session
?
关于异常
当使用request.addfinalizer()
时,你应该传入一个函数的引用。
您的代码正在传递调用该函数的结果。
request.addfinalizer(module_setup.teardown())
你应该这样称呼它:
request.addfinalizer(module_setup.teardown)
关于夹具范围
如果您的夹具允许在多个测试调用中重复使用,请使用 "session"
范围。如果它只允许在一个模块中重用测试,请使用 "module"
范围。
替代夹具解决方案
你使用 fixtures 的方式并没有多少 pytest
风格,它更像是单元测试。
从您显示的代码看来,您唯一需要的是 运行 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
是 fixture 停止执行的地方,将创建的值(驱动程序)产生给测试用例。
测试结束后(或者更好,当我们的夹具范围结束时),它
继续 运行 yield
之后的说明并进行清理
工作。
在所有情况下,您都可以按如下方式修改测试用例:
def test_1(firefox):
print moduleSetup
print '...'
并且 moduleSetup
夹具完全过时了。
在test1.py
中我有以下代码
@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' object is not callable>, func = <function <lambda> at 0x104580488>, when = 'teardown'
此外,如果我想 运行 使用相同的驱动程序对象同时测试 test_1
和 test_2
,我需要使用范围 module
或 session
?
关于异常
当使用request.addfinalizer()
时,你应该传入一个函数的引用。
您的代码正在传递调用该函数的结果。
request.addfinalizer(module_setup.teardown())
你应该这样称呼它:
request.addfinalizer(module_setup.teardown)
关于夹具范围
如果您的夹具允许在多个测试调用中重复使用,请使用 "session"
范围。如果它只允许在一个模块中重用测试,请使用 "module"
范围。
替代夹具解决方案
你使用 fixtures 的方式并没有多少 pytest
风格,它更像是单元测试。
从您显示的代码看来,您唯一需要的是 运行 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
是 fixture 停止执行的地方,将创建的值(驱动程序)产生给测试用例。
测试结束后(或者更好,当我们的夹具范围结束时),它
继续 运行 yield
之后的说明并进行清理
工作。
在所有情况下,您都可以按如下方式修改测试用例:
def test_1(firefox):
print moduleSetup
print '...'
并且 moduleSetup
夹具完全过时了。