Python unit testing framework: pytest

(A) Introduction

  pytest is a very mature full-featured Python testing framework, the main features are the following:

1, simple and flexible, easy to use;

2, supports parameterized;

3, it is possible to support the simple unit test and test complex functions, it can also be used for selenium / appnium test automation, automatic test interfaces (pytest + requests);

4, pytest with many third-party plug-ins, and can be custom extensions, such as better quality pytest-selenium (integrated selenium), pytest-html (perfect test report generation html), pytest-rerunfailures (case fails repeatedly performed), pytest -xdist (multi-CPU distribution) and so on;

5, test cases and xfail skip processing;

6, can be very good and jenkins integration;

(B) installation

  pip install -U pytest # installation by pip

  pip install -U pytest-html

  pip install -U pytest-rerunfailures

  py.test --version # View pytest version

       This is pytest version 2.7.2, imported from C:\Python27\lib\site-packages\pytest.pyc

There are also many good third-party plug-ins, go to http://plugincompat.herokuapp.com/ and https://pypi.python.org/pypi?%3Aaction=search&term=pytest-&submit=search Find

(C) example

Here is a list of several examples of pytest-document

1, the default implementation of all prefixed with test_ (test _ *. Py) or _test suffix (* _test.py) files in the current directory to test_ prefixed function

import pytest

# content of test_sample.py
def func(x):
    return x + 1
def test_answer():
    assert func(3) == 5

Py.test run or specify a particular file py.test -q test_sample.py

2, composed of a plurality of classes by using the example of

import pytest

# content of test_class.py
class TestClass:
    def test_one(self):

        x = "this"
    assert 'h' in x
    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')

3, call pytest in python: python test_class.py

import pytest

# content of test_class.py
class TestClass:
    def test_one(self):
        print 'one'
        x = "this"
        assert 'h' in x
    def test_two(self):
        print 'two'
        x = "hello"
        assert hasattr(x, 'check')
if __name__ == '__main__':
    pytest.main("-q --html=a.html")

4, to a support parameterized example, parametric pytest.mark.parametrize parameters, the first tuple of variables, the second is the list of tuples variable assignment, following this section describes particular carefully

# content of test_time.py
import pytest
from datetime import datetime, timedelta

testdata = [
(datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1)),
(datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1)),
]

@pytest.mark.parametrize("a,b,expected", testdata)
def test_timedistance_v0(a, b, expected):
    diff = a - b
    assert diff == expected
    
@pytest.mark.parametrize("a,b,expected", testdata, ids=["forward", "backward"])
def test_timedistance_v1(a, b, expected):

    diff = a - b
    assert diff == expected
    
def idfn(val):
    if isinstance(val, (datetime,)):
    # note this wouldn't show any hours/minutes/seconds
        return val.strftime('%Y%m%d')
@pytest.mark.parametrize("a,b,expected", testdata, ids=idfn)
def test_timedistance_v2(a, b, expected):
    diff = a - b
    assert diff == expected

Guess you like

Origin www.cnblogs.com/yaoteng/p/10979024.html