把一个函数定义为Fixture很简单,只要在函数声明之前加上“@pytest.fixture”。其他函数要来调用这个Fixture,只用把它当做一个输入的参数即可。
演示代码:
import pytest@pytest.fixture()def before(): print ‘\nBefore each test‘def test_1(before): print ‘test_1()‘def test_2(before): print ‘test_2()‘ assert 0 # For test purpose
运行结果:
(wda_python) bash-3.2$ pytest -q test_smtpsimple.py .F [100%]================================================================ FAILURES ================================================================_________________________________________________________________ test_2 _________________________________________________________________before = None def test_2(before): print ‘test_2()‘> assert 0 # For test purposeE assert 0test_smtpsimple.py:12: AssertionError--------------------------------------------------------- Captured stdout setup ----------------------------------------------------------Before each test---------------------------------------------------------- Captured stdout call ----------------------------------------------------------test_2()1 failed, 1 passed in 0.09 seconds(wda_python) bash-3.2$
可以用以下三种不同的方式来写,我只变化了函数名字和类名字,内容没有变。第一种是每个函数前声明,第二种是封装在类里,类里的每个成员函数声明,第三种是封装在类里在前声明。在可以看到3中不同方式的运行结果都是一样。
import pytest@pytest.fixture()def before(): print ‘\nBefore each test‘@pytest.mark.usefixtures("before")def test_1(): print ‘test_1()‘@pytest.mark.usefixtures("before")def test_2(): print ‘test_2()‘class Test1: @pytest.mark.usefixtures("before") def test_3(self): print ‘test_3()‘ @pytest.mark.usefixtures("before") def test_4(self): print ‘test_4()‘@pytest.mark.usefixtures("before")class Test2: def test_5(self): print ‘test_5()‘ def test_6(self): print ‘test_6()‘
我们用“pytest -v -s test_module.py”运行详细模式测试并打印输出,运行结果:
================================================== test session starts ===================================================platform darwin -- Python 2.7.15, pytest-4.1.0, py-1.7.0, pluggy-0.8.0 -- /Users/jackey/Documents/iOS/code/iOS-Auto/MyPyEnv/wda_python/bin/python2.7cachedir: .pytest_cacherootdir: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test, inifile:collected 6 items test_smtpsimple.py::test_1 Before each testtest_1()PASSEDtest_smtpsimple.py::test_2 Before each testtest_2()PASSEDtest_smtpsimple.py::Test1::test_3 Before each testtest_3()PASSEDtest_smtpsimple.py::Test1::test_4 Before each testtest_4()PASSEDtest_smtpsimple.py::Test2::test_5 Before each testtest_5()PASSEDtest_smtpsimple.py::Test2::test_6 Before each testtest_6()PASSED================================================ 6 passed in 0.06 seconds ================================================(wda_python) bash-3.2$
fixture在创建的时候有一个关键字参数scope:
scope=‘session‘
,它将只运行一次,不管它在哪里定义。
scope=‘class‘
表示每个类会运行一次。
scope=‘module‘表示每个module的所有test只运行一次。
scope=‘function‘表示每个test都运行, scope的默认值
实例代码:
import pytestimport time@pytest.fixture(scope="module")def mod_header(request): print ‘\n------------------‘ print ‘module : %s‘ % request.module.__name__ print ‘-------------------‘@pytest.fixture(scope="function")def func_header(request): print ‘\n------------------‘ print ‘function: %s‘ % request.module.__name__ print ‘time: %s‘ % time.asctime() print ‘-------------------‘def test_1(mod_header,func_header): print ‘in test_1()‘def test_2(mod_header,func_header): print ‘in test_2()‘
运行结果:
================================================== test session starts ===================================================platform darwin -- Python 2.7.15, pytest-4.1.0, py-1.7.0, pluggy-0.8.0 -- /Users/jackey/Documents/iOS/code/iOS-Auto/MyPyEnv/wda_python/bin/python2.7cachedir: .pytest_cacherootdir: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test, inifile:collected 2 items test_smtpsimple.py::test_1 ------------------module : test_smtpsimple-------------------------------------function: test_smtpsimpletime: Sun Jan 13 12:19:25 2019-------------------in test_1()PASSEDtest_smtpsimple.py::test_2 ------------------function: test_smtpsimpletime: Sun Jan 13 12:19:25 2019-------------------in test_2()PASSED================================================ 2 passed in 0.04 seconds ================================================(wda_python) bash-3.2$
可以看到module在整个module中只执行了一次
比如上面的例子,我们可以这样写效果也是一样的:
import pytestimport time@pytest.fixture(scope="module", autouse=True)def mod_header(request): print ‘\n------------------‘ print ‘module : %s‘ % request.module.__name__ print ‘-------------------‘@pytest.fixture(scope="function", autouse=True)def func_header(request): print ‘\n------------------‘ print ‘function: %s‘ % request.module.__name__ print ‘time: %s‘ % time.asctime() print ‘-------------------‘def test_1(): print ‘in test_1()‘def test_2(): print ‘in test_2()‘
运行结果:
================================================== test session starts ===================================================platform darwin -- Python 2.7.15, pytest-4.1.0, py-1.7.0, pluggy-0.8.0 -- /Users/jackey/Documents/iOS/code/iOS-Auto/MyPyEnv/wda_python/bin/python2.7cachedir: .pytest_cacherootdir: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test, inifile:collected 2 items test_smtpsimple.py::test_1 ------------------module : test_smtpsimple-------------------------------------function: test_smtpsimpletime: Sun Jan 13 12:34:59 2019-------------------in test_1()PASSEDtest_smtpsimple.py::test_2 ------------------function: test_smtpsimpletime: Sun Jan 13 12:34:59 2019-------------------in test_2()PASSED================================================ 2 passed in 0.04 seconds ================================================(wda_python) bash-3.2$
在上面的例子中,fixture返回值都是默认None,我们可以选择让fixture返回我们需要的东西。如果你的fixture需要配置一些数据,读个文件,或者连接一个数据库,那么你可以让fixture返回这些数据或资源。
如何带参数,可以把参数赋值给params,默认是None。对于param里面的每个值,fixture都会去调用执行一次,就像执行for循环一样把params里的值遍历一次。
import pytest@pytest.fixture(params=[1,2,3])def test_data(request): return request.paramdef test_not_2(test_data): print ‘test_data: %s‘ % test_data assert test_data != 2
运行结果:
========================================================== test session starts ===========================================================platform darwin -- Python 2.7.15, pytest-4.1.0, py-1.7.0, pluggy-0.8.0 -- /Users/jackey/Documents/iOS/code/iOS-Auto/MyPyEnv/wda_python/bin/python2.7cachedir: .pytest_cacherootdir: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test, inifile:collected 3 items test_smtpsimple.py::test_not_2[1] test_data: 1PASSEDtest_smtpsimple.py::test_not_2[2] test_data: 2FAILEDtest_smtpsimple.py::test_not_2[3] test_data: 3PASSED================================================================ FAILURES ================================================================_____________________________________________________________ test_not_2[2] ______________________________________________________________test_data = 2 def test_not_2(test_data): print ‘test_data: %s‘ % test_data> assert test_data != 2E assert 2 != 2test_smtpsimple.py:9: AssertionError=================================================== 1 failed, 2 passed in 0.09 seconds ===================================================(wda_python) bash-3.2$
可以看到test_not_2里面把用test_data里面定义的3个参数运行里三次。