Related articles
Pytest framework of fixture details (a)
Pytest framework of fixture explained (II)
Pytest framework of fixture explained (3)
This article on fixture is as follows:
- The autouse parameter of the fixture
- session and module level fixtures
1、fixture's autouse parameter
The fixture in pytest, by default, needs to be actively requested by the test case/class before it will be executed after it has been defined.
But it has a parameter called autouse, which is False by default.
Turns off auto-call/auto-execution for fixtures.
If autouse=True is set, it means that the fixture will be executed automatically within its scope.
There are two types of cases, which are explained in the following examples.
Scenario 1: When the fixture is defined inside a test class
If the fixture has autouse=True set, it will only take effect inside the test class class. Use cases outside the test class are not affected.
Define a test_pytest that defines a TestDemo class.
Define 2 fixtures, one with autouse=True and one with the default value of False.
Implementation results:
In the second case, set autouse=True in the fixture.
All test classes/test cases that can be affected will be executed (depending on the scope of the fixture to decide in which scope to execute).
There is the following directory structure:
The test case files (test_autouse1.py, test_autouse2.py) do not use @("func_fix") to actively request a fixture.
The contents of the document are as follows:
The fixture named func_fix has autouse=True and is scoped to function.
Each test case under the pytest20210301 package performs its pre-preparation action before executing the case and its post-cleanup action after executing the case.
The result of executing (collecting use cases and executing) the file is as follows:
Based on the above, fixtures at the function, class, and module level generally don't turn on autouse=True.
Different test cases/test classes don't have exactly the same needs for prep work and post cleanup.
If autouse=True is turned on, then it will be executed regardless of whether the test case/test class needs it or not.
Letting test cases/classes actively request fixtures based on actual requirements is the right way to go.
2. session and module level fixtures
pytest fixtures have session and module levels in addition to function and class.
session-level fixture
A session in this context is a test session.
It refers to the entire process of collecting use cases from the start of pytest to the completion of the execution of the use cases as a single session.
For example, if you have collected 100 test cases to execute, the session clamp will hold those 100 cases.
For example, if you have collected 30 test cases to execute, the session clamp will hold those 30 cases.
As it is executed only once during the execution of all use cases.
If a session-level fixture is defined, that means you need to execute it.
So it is possible to set autouse=True.
When we do automated tests, if some prep work is done for the whole test session, then session level can be defined, such as cleaning/creating some files etc.
Still using the above use case structure as an example, add a session level fixture to when and set it to autouse=True:
The results of the implementation are as follows:
module-level fixture
module refers to the test py file, and the fixture is the test cases in the entire test_*.py file.
In test_*.py, the line of code that calls the module-level fixture is the one that holds all the test cases after that line of code.
Let's take an example. Let's take the above use case structure and add a module-level fixture to it:
In test_autouse2.py, the module-level fixture is called, but not before the first use case.
The results of the implementation are as follows:
To this point this article on the Pytest framework of fixture is introduced to this article. I hope to help you learn, but also hope that you support me more.