Super detailed from entry to proficiency, pytest automated testing framework practice-fixture advanced (10)


foreword

1. The autouse parameter of the fixture

The fixture in pytest, after being defined by default, needs to be actively requested by the test case/test class before it will be executed.

Pytest automated testing framework: https://www.bilibili.com/video/BV18K411m7FH/

But it has a parameter called autouse, which is False by default.
Turn off the fixture's auto-call/auto-execute function.
If autouse=True is set, it means that the fixture will be automatically executed within its scope.

Divided into two situations, the following examples illustrate.

Case 1: When the defined fixture is inside the test class,
if the fixture sets autouse=True, it will only take effect inside the test class. Use cases outside of test classes are not affected.

Define a test_pytest-autouse.py and define a TestDemo class.

Define 2 fixtures in it, one sets autouse=True, and the other uses the default value False

Please add a picture description

In the second case, the fixture setting autouse=True in conftest.py
can affect that all test classes/test cases will be executed (depending on the scope of the fixture to determine which scope to execute).

There is the following directory structure:

Please add a picture description

In the test case files (test_autouse1.py, test_autouse2.py), @pytest.mark.usefixture("func_fix") is not used to actively request fixtures.

Please add a picture description

The contents of the conftest.py file are as follows:

Please add a picture description

The fixture named func_fix sets autouse=True, its scope is function, then

Each test case under the pytest20210301 package will execute its pre-preparation action before executing the use case, and will execute its post-clearing action after executing the use case.

The result of executing the main.py (collect use cases and execute) file is as follows:

Please add a picture description

Based on the above situation, generally function, class, and module level fixtures will not enable autouse=True

Different test cases/test classes have different requirements for pre-preparation work and post-clearing work.

If autouse=True is enabled, it will be executed regardless of whether the test case/test class needs it or not.

It is the right way to let test cases/test classes actively request the use of fixtures according to actual needs.

2. Session and module level fixtures

In addition to function and class, pytest's fixture also has session and module levels.

Session-level fixtures

The session here refers to the test session.
It refers to: the whole process from the collection of use cases by pytest to the completion of execution of use cases is a session.
For example, if 100 test cases are collected and executed, then the session fixture holds these 100 test cases.
For example, if 30 test cases are collected to be executed, then the session fixture holds these 30 test cases.

Since it is executed only once during the execution of all use cases.
If you define a session-level fixture, that means you need to execute it.
So it is possible to set autouse=True.
When we are doing automated testing, if some preparatory work is done for the entire test session, then we can define the session level, such as cleaning/creating some files, etc.

Still taking the above use case structure as an example, add a session-level fixture in conftest.py and set it to autouse=True:

Please add a picture description

The execution results are as follows:

Please add a picture description

Fixtures at the module level

module refers to the test py file, and the fixture contains the test cases in the entire test_*.py file.

In test_*.py, which line of code calls the fixture at the module level, then all the test cases after that line of code are clipped.

To illustrate. Still taking the above use case structure as an example, add a module-level fixture in conftest.py:

Please add a picture description

In test_autouse2.py, the fixture at the module level is called, but it is not called before the first use case.

Please add a picture description

The execution results are as follows:

Please add a picture description

The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

Don't let past failures restrict your progress, cherish the present, pursue excellence, strengthen your beliefs, and move forward bravely, you will create a better tomorrow and achieve a brilliant life!

Only by constantly breaking through ourselves and challenging the limit can we create real value. Have the courage to accept challenges, strengthen your confidence, and face up to difficulties, you will become the best self and realize the glory of life!

The road to success is full of hardships and setbacks, but as long as you are enthusiastic, down-to-earth, and persistent, you will be able to overcome difficulties and welcome your own brilliant life!

Guess you like

Origin blog.csdn.net/m0_70102063/article/details/130109197