Construction of Python pytest interface automation framework

Time:2021-8-7

1. Install pytest first

pip install pytest

2. Write single test cases

In the pytest framework, there are the following constraints:

  • All single test file names need to meettest_*.pyFormat or*_test.pyFormat.

  • In the single test file, you can includetest_The function at the beginning can also containTestClass starting with.

  • In a single test class, you can include one or moretest_Function at the beginning.

At this point, thepytestCommand will automatically find test functions that meet the above constraints from the current directory and subdirectories to execute.

Construction of Python pytest interface automation framework

import pytest

content of test_sample.py

def func(x):

return x + 1

def test_answer():

assert func(3) == 5

Construction of Python pytest interface automation framework

Run pytest or specify a specific file pytest – Q test_ sample.py

2.1 run pytest to traverse the current directory and subdirectories

Construction of Python pytest interface automation framework

Looking at the part circled in red, you can see that it loops through its current directory and subdirectories

2.2 pytest -q test_ Class.py specifies the file to run

3. Test case search

Definition: the process of searching test files and test cases is called test search

If you want to be found, you must follow the naming rules of pyteset:

  • All single test file names need to meettest_*.pyFormat or*_test.pyFormat.

  • In the single test file, you can includetest_The function at the beginning can also containTestClass starting with.

  • In a single test class, you can include one or moretest_Function at the beginning.

For ease of remembering, test files and test functions must be written in test_ The class starts with test

4. Explanation of console information

Construction of Python pytest interface automation framework

Construction of Python pytest interface automation framework

4.1. test session starts

This is the split line for each run

4.2. Platform information

1. Win32 stands for windows and MAC shows Darwin

2. Next, Python and pytest versions are displayed, and py and pluggy are pytest packages

3. Rootdir is the current starting directory. IniFile refers to the configuration file. I will explain it in detail in the following chapters

*  4. collected 5 itemes *How many test cases have been collected

*  5. test_run01.py …

The “.” at the back of the test file means that the test passes. F (fail), e (error), s (skip), X (xpass) > is expected to fail but succeed

X (xfail) > expected failure and execution failure. (I’ll explain the details below)

  6. 4 fail,1passed in 0.06 seconds======

Indicates the number of passes. If it fails, it will summarize the number and time spent

*

5. Use command line options

With the command line option, our use case becomes very flexible. If the unittest framework wants to execute complex scenarios

This is where pytest wins unittest

The following are common command line options that can be used by us. If you want to expand, you can use them

Pytest – help can find all the commands. You can try it yourself. There is no screenshot here

5.1 -–collect-only

As can be seen from the picture, a total of 5 test cases have been collected. When the execution result is no tests, ran indicates that only collection is not executed.

5.2. -k options

The important function is to use the expressions you use to specify the test cases you want to run

For example, you use pytest – K “expression”

Pytest is based on the expression you enter to match whether there is this expression in the name of the test case

Then run. If you want to find multiple contents, you can use or to connect. Please see the screenshot below:

Construction of Python pytest interface automation framework

5.3. – M option

M is the abbreviation of marker. Its function is to execute the test cases marked by itself. I’ll explain how to mark the test cases first

If we want to execute with first_ How to mark a case

Please look at the example below. It uses @ pytest.mark.first_ Case, where first_ Case is what the decorator uses to mark

Construction of Python pytest interface automation framework

import pytest

def inc(x):

return x + 1

def test_answer():

assert inc(3) == 5

@pytest.mark.first_case

def test_task01():

assert 1 == 1

def test_task02():

assert 1 == 1

def test_task03():

assert 1 == 1

Construction of Python pytest interface automation framework

pytest -m “first_ Case “– collect only or

pytest -m “first_case”

| 1234567891011121314151617 | PS E:\Python_Web\pytest-requests-allure> pytest -m ``"first_case" --collect-only``================================================= test session starts =================================================``platform win32 -- Python 3.7.2, pytest-4.5.0, py-1.8.0, pluggy-0.12.0``rootdir: E:\Python_Web\pytest-requests-allure``plugins: allure-pytest-2.6.3, html-1.20.0, metadata-1.8.0``collected 8 items / 6 deselected / 2 selected``================================================== warnings summary ===================================================``d:\software\python\python37\lib\site-packages\_pytest\mark\structures.py:324``d:\software\python\python37\lib\site-packages\_pytest\mark\structures.pyConstruction of Python pytest interface automation framework PytestUnknownMarkWarning: Unknown pytest.mark.first_case - ``is this a typo? You can register custom marks to avoid ``this warning - ``for details, see https:``//docs.pytest.org/en/latest/mark.html``PytestUnknownMarkWarning,``-- Docs: https:``//docs.pytest.org/en/latest/warnings.html``====================================== 6 deselected, 1 warnings ``in 0.11 seconds ======================================= |

If you want to use multiple markers, you can write “marker1 or marker2”, which means to execute all test cases of mark1 and Mark2

You can also use not to filter, “marker1 and not marker2” means to execute marker1 and do not include marker2 use cases

6. – x option

This option is prepared for debugging. The normal process is that if the assertion fails, it will continue to execute without breaking. If I want to stop the execution when the first assertion fails, I can use the – x option. Stop the execution when the assertion fails. It is helpful for us to find the reason

***7. – lf option (LF is the abbreviation of last failed)***

This option is very useful. It can directly filter out the test case of the last failure, which is helpful for us to find the failed case and solve it, * * we can find the last failure by running this if option, which saves us from executing the successful case and saves our time

8. – FF (failed first)

The difference from – LF is that it gives priority to running failed cases, and then runs those that have passed the previous run. Try it yourself. There is no demonstration here

9. – V option

This will output more content on the console. The most obvious difference is each textEach test case in the piece occupies one line (previously, each file occupies one line)

*I’ll print out both cases and have a look

*

Construction of Python pytest interface automation framework

Construction of Python pytest interface automation framework

*10 – TB = style option***

This option is used to display the failure output information,

The output information generally includes 1. Which line the failure occurs in 2. What failure is 3. How to fail. These three elements are information traceability

Our common styles are short, no, line

Examples are as follows:

Construction of Python pytest interface automation framework

No blocks all backtracking information, but only displays which file and which case failed,

Line to which assert

The specific parameter of short lists more detailed failure information

In practice, the combination of – Q and – TB = line is enough

This work adoptsCC agreement, reprint must indicate the author and the link to this article