Mixing pytest Fixtures and xUnit Fixtures

You can mix pytest fixtures and xUnit fixtures:

 import​ pytest
 
 
 def​ setup_module():
 print​(​'​​ ​​setup_module() - xUnit'​)
 
 
 def​ teardown_module():
 print​(​'teardown_module() - xUnit'​)
 
 
 def​ setup_function():
 print​(​'setup_function() - xUnit'​)
 
 
 def​ teardown_function():
 print​(​'teardown_function() - xUnit​​ ​​'​)
 @pytest.fixture(scope=​'module'​)
 def​ module_fixture():
 print​(​'module_fixture() setup - pytest'​)
 yield
 print​(​'module_fixture() teardown - pytest'​)
 
 
 @pytest.fixture(scope=​'function'​)
 def​ function_fixture():
 print​(​'function_fixture() setup - pytest'​)
 yield
 print​(​'function_fixture() teardown - pytest'​)
 
 
 def​ test_1(module_fixture, function_fixture):
 print​(​'test_1()'​)
 
 
 def​ test_2(module_fixture, function_fixture):
 print​(​'test_2()'​)

You can do it. But please don’t. It gets confusing. Take a look at this:

 $ ​​cd​​ ​​/path/to/code/appendices/xunit
 $ ​​pytest​​ ​​-s​​ ​​test_mixed_fixtures.py
 ============ test session starts =============
 plugins: mock-1.6.0, cov-2.5.1
 collected 2 items
 
 test_mixed_fixtures.py
 setup_module() - xUnit
 setup_function() - xUnit
 module_fixture() setup - pytest
 function_fixture() setup - pytest
 test_1()
 .function_fixture() teardown - pytest
 teardown_function() - xUnit
 
 setup_function() - xUnit
 function_fixture() setup - pytest
 test_2()
 .function_fixture() teardown - pytest
 teardown_function() - xUnit
 
 module_fixture() teardown - pytest
 teardown_module() - xUnit
 
 
 ========== 2 passed in 0.01 seconds ==========

In this example, I’ve also shown that the module, function, and method parameters to the xUnit fixture functions are optional. I left them out of the function definition, and it still runs fine.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.5.201