pytest's fixture initializes automated test data more elegantly

what is fixture

Fixture is a shell function executed by pytest before and after the test function is run. Its function is to separate some non-core test logic from the test function to facilitate the use of other test functions while maintaining the consistency of these edge logics.

The code in the fixture can be customized to meet the testing requirements of the variable pair, including defining the data set to be passed in the test, configuring the initial state of the system before testing, providing data sources for batch testing, etc. fixture1.png

The pytest.fixture() decorator is used to declare that the function is a fixture. If the parameter list of the test function contains the name of the fixture, pytest will detect it and execute the fixture before the test function is run. The fixture will return the data to the test function. fixture2.png

 

Fixture execution and destruction logic

Generally speaking, the fixture will be run before the test function is executed, but if the fixture function contains the yield keyword, then pytest will stop at the yield point and run the test function instead. After the test function is executed, it will return to the fixture and continue. Execute the code after yield. Does it sound familiar? Every test case will have setup and teardown. fixture3.png

pytest --setup-show test_add.py

 

--setup-show will output what is executed during the test process and the order of execution on the console. Execute the above command to view the output content.

 

As you can see from the screenshot, the test function is sandwiched in the middle. pytest divides the execution of each fixture into SETUP and TEARDOWN. There are multiple setups before the test function is executed because some built-in fixtures are used in the custom fixture. , I will write an article about the built-in fixture later.

The F and S in front of the fixture represent the scope of the fixture, F represents the function-level scope, and S represents the session-level scope.

Use fixture to transfer data

Fixtures are great for storing test data, and they can return any type of data.

fixture5.png

 

By constructing a fixture and making it return a tuple, pass in the fixture in the test function, and the assertion fails. The following figure is the output result

 

pytest gives a clear stack content, and also gives the function parameter value that caused the assert exception. So if an exception occurs in the fixture, will it be traced by the stack, and is there any difference in the output information?

 

Add a doomed assertion to the fixture and run the test case. pytest correctly locates the exception that occurred in the fixture, and it is not reported as FAIL, but ERROR. This distinction is very clear. If the test result is marked as FAIL, then The user will know that the failure occurred in the ongoing test case, not in the dependent fixture.

Finally: The following is the supporting learning materials. For those who are doing [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you!

Software testing interview applet

A software test question bank that has been used by millions of people! ! ! Who is who knows! ! ! The most comprehensive interview test mini program on the Internet, you can use your mobile phone to answer questions, take the subway, bus, and roll it up!

Covers the following interview question sections:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. Web, app, interface automation, 7. Performance testing, 8. Programming basics, 9. HR interview questions, 10. Open test questions, 11. Security testing, 12. Computer basics

  How to obtain the full set of information: Click on the small card below to get it yourself

Guess you like

Origin blog.csdn.net/weixin_57794111/article/details/132776958