Pytest learning notes - configuration file pytest ini

Configuration file pytest ini

preface

quite a lot pytest settings You can set it in the configuration file, which is usually located in the root directory of the repository or in the test folder

pytest.ini files take precedence over other files, even if they are empty

The pytest configuration file can change the operation mode of pytest. It is a fixed file pytest INI file, read the configuration information and run it in the specified way

Common configuration items

markers

Its function is to make registration marks to prevent spelling mistakes. For example, @ pytest mark. Smoke spell @ pytest mark. Somke, by default. This will not cause program errors. Pytest will think this is another tag you created. To avoid spelling mistakes. Can be found in pytest Registration mark in INI file

[pytest]
# Register custom tags
markers =
    smoke: Run the smoke test functions for tasks project
    get: Run the test functions that test tasks.get()

After the tag is registered, you can view it through pytest --markers

testpaths

testpaths indicates where pytest is going to visit. testpaths are a series of paths relative to the root directory to limit the search scope of test cases. This option works only when pytest does not specify a file directory parameter or a test case identifier, for example 🌰:

Modify pytest The INI file is as follows:

[pytest]
testpaths = test_02.py

Modify test_01.py test_02.py file is as follows:

# FileName: test_01.py
import pytest

def test_01(fixturedemo):
    print('Execute test case 1')
    
# FileName: test_02.py
def test_02(fixturedemo):
    print('Execute test case 2')

Execute pytest -v and the result is as follows:

addopts

Change default command line options

When pytest is run on the command line, sometimes some parameters need to be used frequently, and you don't want to input them repeatedly, you can use pytest addopts setting in INI file, for example 🌰, Modify pytest The INI file is as follows:

[pytest]
addopts=-vs

The result of executing pytest is as follows:

You can see that the file where the use case is located and the use case name are printed, and the print output is also printed

Another example 🌰:

For example, if you want to generate a report after testing, run it twice again after failure, and run it twice in total. If you write it in cmd, the command will be very long

pytest -v --rerun=2 --count=2 --html=report.html --self-contained-html

You need to knock every time, which will waste a lot of time. You can use pytest Ini perfect solution:

[pytest]
# Command line parameters
addopts = -v --reruns=1 --count=2 --html=reports.html --self-contained-html

xfail_strict

Function: set xfail_strict = True allows those marked @ pytest mark. Xfail, but the test case that actually shows XPASS is reported as a failure

Format: True, False (default), 1, 0

Modify pytest Ini is as follows:

[pytest]
xfail_strict = True

Modify test_01.py file is as follows:

# FileName: test_01.py
import pytest
def test_02(fixturedemo):
    print('implement test_02 Test case 2')

@pytest.mark.xfail()
def test_01():
    print("implement test_02 Test case 1")

The results are as follows:

log_cli

Function: the console outputs logs in real time

Format: log_cli=True or False (default), or log_cli=1 or 0

Modify pytest Ini is as follows:

[pytest]
log_cli = True

log_cli is 0 / 1, and the comparison of the execution results before and after is as follows:

Added log_ After cli = 1, you can clearly see whether the test case under which module in which package is passed or failed

norecursedirs

Function: when collecting test cases, pytest will recursively traverse all subdirectories, including some directories you know you don't need to traverse. In this case, you can use the norecursedirs parameter to simplify the search of pytest

Default setting: norecursedirs =* build dist CVS _ darcs {arch} *. egg

Correct writing: multiple paths are separated by spaces

For example 🌰, To avoid traversing the venv, src, resources, log, report and util folders, you can set the following settings

[pytest]
norecursedirs = .* build dist CVS _darcs {arch} *.egg venv src resources log report util

Change test case collection rules

pytest default test case collection rules

The file name is in test * Py files and*_ test.py

With test_ Function at the beginning

Classes starting with Test cannot contain init methods

With test_ Methods in the first class

This use case collection rule can be modified or added (it is recommended to add it to the original rule), for example 🌰:

Modify pytest The INI file is as follows:

[pytest]
python_files =     test_*  *_test  test*
python_classes =   Test*   test*
python_functions = test_*  test* tes_*

Modify test_02.py as follows:

# FileName: test_02.py
import pytest

def test_02(fixturedemo):
    print('implement test_02 Test case 2')

@pytest.mark.xfail()
def test_01():
    print("implement test_02 Test case 1")

The results are as follows:

matters needing attention

pytest. The INI file should be placed in the root directory of the project. Do not change the name, otherwise it will not be recognized

Keywords: Python pytest

Added by gtibok on Wed, 09 Feb 2022 17:08:17 +0200