Python test framework pytest: common parameters, find subsets, parameterization, skip

Pytest is a python-based testing framework for writing and executing test code. pytest is mainly used for API testing, you can write code to test API, database, UI, etc.
pytest is a very mature full-featured Python testing framework, which mainly has the following advantages:
simple and flexible, easy to use. The syntax of pytest is concise and clear, easy to understand and use.
Parameterization is supported. pytest can parameterize test cases through decorators or fixture methods to improve the coverage of test cases.
It can support simple unit tests and complex functional tests, and can also be used for automated testing such as selenium/appnium and interface automated testing (pytest+requests).
pytest has many third-party plug-ins, and can customize extensions, such as pytest-selenium (integrated selenium), pytest-html (perfect html test report generation), pytest-rerunfailures (failure case repeated execution), pytest-xdist (Multi-CPU distribution) etc.
Skip and xfail processing of test cases. pytest provides a flexible mechanism to skip test cases or expected failures, and one or some test cases can be skipped during the test as needed.
It can be well integrated with jenkins. pytest can be seamlessly integrated with continuous integration tools such as Jenkins to facilitate automated testing and report generation.
The report framework - allure also supports pytest. pytest can be integrated with the Allure reporting framework to generate detailed HTML test reports for easy analysis and display of test results.
pytest is a powerful, flexible and easy-to-use Python testing framework, which is suitable for various types of testing requirements and has high practical value.

Install

# 安装
pip install pytest

# 帮助
pytest -h

format requirement

File name: test_*.py or *_test.py
Function name: start with test

Common parameters

-sDisplay the standard output, equivalent to –capture=no, pytest does not output print loggingthe output by default, unless the assert fails.
-vShow detailed report.
-kSearch for test cases by keyword .
-qDisplay a compact report.
-mOnly run the marked test cases.
-xStop testing immediately when a use case fails.
-c fileLoad configuration file from file.
-l (--showlocals)Display local variables and their values ​​when backtracking with case failure information.
-rsxXReport® why test cases were skipped (s), expected to fail (x), expected to fail but actually passed (X).
-strictDisallow the use of mark flags not registered in the configuration file (pytest.ini).
--maxfail=nStop running tests after failure n.
–reruns=numFailed cases are rerun num times. Requires pytest-rerunfailuresthe plugin module to be installed.
--lf (--last-failed)Execute only the last failed use case. If there are no failed cases or no cache files found, the default is to run all cases. Used
--lfnf =[all, none]at the same time, it means that all test cases are executed when no test cases or cache files are found, and it means that no test cases are executed when no test cases or cache files are found.--lf=all=none

pytest.main(['--lf','--lfnf=none', "test.py"])

--ff (--failed-first)Execute the failed use cases first before executing other use cases.
--nf (--new-first)Start by running tests from a new file or newly modified use case.
--sw (--stepwise)Exit on test failure and start the test next time in the failed test case.
--stepwise-skipIgnore the first failed test and exit on the second test failure.
--keep-duplicatesRepeated testing.
--durations=nDisplay the slowest n use cases. Note: pytest will not show test times < 0.01s by default unless the parameter -vv is added.
--fixturesShow all available fixtures.
--tb=styleStack trace information printing mode (auto/long/short/line/native/no]).
--setup-showShow fixture execution steps.
--cache-show=[CACHESHOW]Display cache contents, do not perform collection or testing.
--cache-clearClear pytest cache before running.
--continue-on-collection-errorsEnforce testing even if a collection (collection case phase) error occurs.
--rootdir=ROOTDIRDefines the root directory for tests.
--color=colorColor of terminal output (yes/no/auto).
--collect-onlyOnly use cases are collected, not executed.
--assert=MODE"plain" does not perform any assertion debugging, "rewrite" rewrites the assert statement in the test module to provide assert expression information

basic test

file name:test_one.py

# 测试函数
def test_division():
    assert 1/1.0==1

# 测试类
class TestOne:
    def test_addition(self):
        """
        测试方法
        """
        assert 1 + 1 == 2

    def testsquare(self):
        """
        测试方法
        """
        assert 2*2 == 3
    
    def tesequality():
        """
        无效
        """
        assert 10 == 11

run:

pytest -v

-vIndicates to view details.
3

Found 3 test cases, 1 failed and 2 passed.

test subset

Find a subset by function name

test_sub.py

# 测试子集
class TestSub:
    def test_compare_one(self):
        """
        测试方法
        """
        assert 1 + 1 == 2


    def test_compare_two(self):
        """
        测试方法
        """
        assert 1 + 2 == 3
pytest -v -k compare

Use the parameter value pytest -k <substring>of the command -kto filter the function name.
1

group mark

# -*- coding: utf-8 -*-
import pytest

# 测试子集
class TestGroup:
    @pytest.mark.group
    def test_group_one(self):
        """
        测试方法
        """
        assert 1 + 1 == 2

    @pytest.mark.group
    def test_group_two(self):
        """
        测试方法
        """
        assert 1 + 2 == 3
pytest -v -m group

Here the decorator is used @pytest.mark.groupto mark the function, and then the pytest -v -m groupin -mis used to find this grouping mark.
1

fixture function

import pytest

# 测试fixture
class TestFixture:
    @pytest.fixture
    def input_value(self):
        return 36


    def test_division(self, input_value):
        """
        测试方法
        """
        assert input_value / 6 == 6

1
Here, @pytest.fixturethe modified function input_value is used to prepare the data in advance for test_divisionuse. This method can only be used in one file. If you want to use it globally, you can configure Conftest.py.

to parameterize

import pytest

@pytest.mark.parametrize("num, output",[(1,11),(2,22),(3,35),(4,44)])
def test_multiplication_11(num, output):
   assert 11*num == output

pytest test_parame.py -v

1

skip test

import pytest
@pytest.mark.xfail
@pytest.mark.great
def test_greater():
   num = 100
   assert num > 100

@pytest.mark.xfail
@pytest.mark.great
def test_greater_equal():
   num = 100
   assert num >= 100

@pytest.mark.skip
@pytest.mark.others
def test_less():
   num = 100
   assert num < 200

pytest test_xfail_skip.py -v

@pytest.mark.xfailMarked as xfail status.
@pytest.mark.skipSkip directly.

1

See the official website for more .

Guess you like

Origin blog.csdn.net/lilongsy/article/details/132150307