python pytest Advanced xunit fixture Detailed Explanation

  • 2021-07-03 00:34:35
  • OfStack

Preface

Today, we will talk about the use of fixture, which is the same as pytest framework and unittest framework. Students who know unittest should know that unittest uses setUp and tearDown methods when initializing the environment and destroying the work, so there are similar methods in pytest framework. Today, we will explain them in detail.

Attach a paragraph of the official document first

1. Each level of setup/teardown can be reused multiple times

2. If the corresponding initialization function fails or is skipped, the teardown method will not be executed

3. Before pytest4.2, xunit fixture does not follow the action rule of fixture, so setup_method method can be executed before fixture with session level and parameter auto=True

But so far, all xunit fixture have followed the rules enforced by fixture

function level

Instances


 def setup_function(function):
 print('\n--------------------')
 print(' The action done before the function executes ')
print('\n--------------------')
 def teardown_function(function):
print('\n--------------------')
 print(' The action done after the function is executed ')
print('\n--------------------')
def test_function_1():
print('\n Test function 1')
def test_function_2():
 print('\n Test function 2')
if __name__ == '__main__':
import pytest
pytest.main(['-sq', 'functionLevel.py'])

Output result


functionLevel.py 
--------------------
 The action done before the function executes 
--------------------
 Test function 1
--------------------
 The action done after the function is executed 
--------------------
--------------------
 The action done before the function executes 
--------------------
 Test function 2
--------------------
 The action done after the function is executed 
--------------------
[100%]
========================== 2 passed in 0.03 seconds ===========================

Description

From the output results, we can conclude that setup_function will perform initialization operation before every 1 test function; teardown_function performs destruction after every 1 test function is executed

method level

Instances


 class TestMethod(object):
 def setup_method(self, method):
 print('\n--------------------')
 print(' The action done before the method executes ')
 print('\n--------------------')
 def teardown_method(self, method):
 print('\n--------------------')
print(' The action done after the method is executed ')
print('\n--------------------')
def test_method_1(self):
print('\n Test method 1')
def test_method_2(self):
print('\n Test method 2')
if __name__ == '__main__':
import pytest
pytest.main(['-sq', 'methodLevel.py'])

Output result


methodLevel.py 
--------------------
 The action done before the method executes 
--------------------
 Test method 1
--------------------
 The action done after the method is executed 
--------------------
--------------------
 The action done before the method executes 
--------------------
 Test method 2
--------------------
 The action done after the method is executed 
--------------------
[100%]
========================== 2 passed in 0.03 seconds ===========================

Description

From the output results, we can conclude that setup_method will perform initialization operation before every 1 test method; teardown_method performs destruction after every 1 test method is executed, and method-level fixture acts on methods in the test class

class level

Instances


 class TestClass(object): 
 @classmethod
 def setup_class(cls):
 print('\nsetup_class() for {}'.format(cls.__name__)) 
 @classmethod
def teardown_class(cls):
 print('\nteardown_class() for {}'.format(cls.__name__))
def test_1(self):
 print('self.test_1()')
def test_2(self):
print('self.test_2()')
if __name__ == '__main__':
import pytest
 pytest.main(['-sq', 'classLevel.py'])

Output result


classLevel.py 
setup_class() for TestClass
.self.test_1()
.self.test_2()
teardown_class() for TestClass
[100%]
========================== 2 passed in 0.06 seconds ===========================

Description

From the output results, we can conclude that setup_class will perform initialization operation once before the test class is executed; teardown_class performs one destruction after the test class is executed, and fixture at class level requires @ classmethod decoration

module Level

Instances


def setup_module(module):
 print('\nsetup_module() for {}'.format(module.__name__))
def teardown_module(module):
 print('\nteardown_module() for {}'.format(module.__name__))
def test_1():
 print('test_1()') 
def test_2():
print('test_2()')
class TestClass(object):
def test_3(self):
print('self.test_3()')
def test_4(self):
print('self.test_4()')
if __name__ == '__main__':
 import pytest
pytest.main(['-sq', 'moduleLevel.py'])

Output result


moduleLevel.py 
setup_module() for moduleLevel
.test_1()
.test_2()
.self.test_3()
.self.test_4()
teardown_module() for moduleLevel
[100%]
========================== 4 passed in 0.04 seconds ===========================

Description

Through the output results, we can summarize: setup_module will perform initialization operation once before the whole test file, that is, the test class or test function and test method in the module are executed; teardown_module will destroy the whole test file, that is, the test class, test function and method in the module once

These are the four levels of xunit fixture. How to use it in practical work needs more practice and in-depth understanding to be handy!

Attach official documents for reference. Although they are in English, they are very detailed


Related articles: