The pytest series can make your peers look at you [setup, teardown, conftest.py]

I. Introduction

In the pytest framework, there are two pre- and post-writing methods, which are used for the operations before and after the execution of the use case, that is to say, the pre-position will be executed before the use case is executed, and the post-position will be executed after the execution of the use case.

Why have pre- and post-positioning? For example, you need to log in before executing this use case or need some other parameters, which can be achieved by pre- and post-positioning

First look at the first way of writing

setup和teardown

Remind that these two are setting all use cases, which means that each use case will be executed after you set it.

def setup() will execute the code in this method before each execution of the use case

def teardown() will execute the code in this method after each execution of the use case

def setup_class() initialization work before each class is executed

def teardown_class() The finishing work after each class is executed 

Code example:

import pytest


class TestLogin:
    def setup(self):
        print("This is the prefix")

    def teardown(self):
        print("This is postfix")

    def test_08(self):
        print("Hello 08")

    def test_01(self):
        print("Hello 01")

    def test_05(self):
        print("Hello 05")

    def test_03(self):
        print("Hello 03")

operation result:

It can be seen that the pre and post are printed when each use case is executed. Let's take a look at the pre and post of the class.

Code example:

import pytest


class TestLogin:
    def setup_class(self):
        print("This is a prefix")

    def teardowm_class(self):
        print("This is postfix")

    def test_08(self):
        print("Hello 08")

    def test_01(self):
        print("Hello 01")

    def test_05(self):
        print("Hello 05")

    def test_03(self):
        print("Hello 03")

operation result:

 It can be seen that the pre and post of the class here will only be executed once before and after the class runs, and the intermediate use cases will not be executed one by one.

In the previous essays, I have basically understood the use of pytest commands, collection of use cases, use of finxture and their scope of action. Today, I will briefly introduce the function of the conftest.py file and how to use this file in actual projects!

Example scene

First of all, let's think about such a question: if we are writing tests, the use cases in each test file need to be logged in before the subsequent operations can be completed, then how to implement it? This requires us to master the use of the conftest.py file.

example code

Create the following directory

ContestFile
    |conftest.py
    |test_file_01.py
    |test_file_02.py
    |__init__.py
# conftest.py
import pytest
@pytest.fixture()
def login():  
  print('\n---------------conftest file login method starts executing------------------------- ----')
  print('login in contest.py')
  print('----------------conftest.py file login method execution ends ----------------------- ----')
# test_file_01.py
def test_01(login):
  print('\n-----------------Case file 1 test case 1 starts executing-------------------' )
  print('login after : in test_file_01->case test_01')
  print('------------------- case file 1 test case 1 execution ends-------------------- ----')
# test_file_02.py
def test_02(login):
  print('\n-----------------Case file 2 Test case 2 starts executing-------------------' )
  print('login after : in test_file_01->case test_01')
  print('-------------------case file 2 test case 2 execution ends-------------------- ----')

Let's run this example code first to see the output

1. You can right-click the directory to run in pycharm

2. You can enter pytest -vs in the cmd directory to run

test_file_01.py
---------------conftest file login method starts executing ----------------------------
login in conftest.py
----------------conftest.py file login method execution ends -------------------------- -
.
------------------ case file 1 test case 1 starts executing ------------------
login after : in test_file_01->case test_01
------------------- Use case file 1 test case 1 execution ends ----------------------- -
                                                        [ 50%]
test_file_02.py
---------------conftest file login method starts executing ----------------------------
login in conftest.py
----------------conftest.py file login method execution ends -------------------------- -
.
------------------ case file 2 test case 2 starts executing ------------------
login after : in test_file_01->case test_01
------------------- Use case file 2 Test case 2 execution ends ----------------------- -
                                                        [100%]

========================== 2 passed in 0.04 seconds ===========================

It can be seen that the login method in the conftest.py file is executed before the test case of each test file is executed. Through this mode, we can realize the preparation of the test case preconditions!

The conftest file needs to be used in combination with the fixture in the actual application, so the parameter scope in the fixture also applies to the features of the fixture in conftest, and I will explain it here.

1. The scope parameter of fixture in conftest is session, then all test files are executed once before execution

2. The scope parameter of the fixture in conftest is module, then the fixture in the conftest file will be executed once before each test file is executed.

3. The scope parameter of the fixture in conftest is class, then the fixture in the conftest file will be executed once before the test class in each test file is executed.

4. The scope parameter of the fixture in conftest is function, then the fixture in the conftest file will be executed once before the test cases of all files are executed.

Summarize

Theories often require practical verification, so if you want to master the specific use of conftest, you need to use more code verification! The above code just verifies that the test function in the test file uses conftest.py. In actual work, not only the function is used, but there is often not only a conftest.py file. The following are the features I summarize, I hope to help you!

1. The name of the conftest.py file is fixed and cannot be modified

2. The file and the use case file are in the same directory, then conftest.py acts on the entire directory

3. The __init__.py file must exist in the directory where the conftest.py file is located

4. The conftest.py file cannot be imported by other files

5. The conftest.py file will be executed before all test files in the same directory are run

 


Thanks to everyone who read my article carefully, watching the rise and attention of fans all the way, there is always a need for a gift exchange, although it is not a very valuable thing, if you can use it, you can take it directly:

① More than 2000 Python e-books (mainstream and classic books should be available)

② Python standard library information (the most complete Chinese version)

③ Project source code (forty or fifty interesting and classic training projects and source code)

④ Videos on basic introduction to Python, crawler, web development, and big data analysis (suitable for novice learning)


 ⑤ Python learning roadmap (say goodbye to inexperienced learning)

In my QQ technical exchange group (technical exchange and resource sharing, advertisements come in to interrupt you)

You can take it away by yourself. The free information in the group number 913569736 (note "csdn000") is the essence of the author's more than ten years of testing career. There are also peer gods to exchange technology together.

The learning materials can be found by our Miss Beibei [mashan-qq] remarks [csdn000] for free

【Must note】Otherwise it will not pass
 

Guess you like

Origin blog.csdn.net/csdnchengxi/article/details/122579857