Python developer unit testing, essential

First introduce Python unit testing framework used

  • unittest
  • pytest
  • nose

The following highlights pytest

1 Overview

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

  • 1, simple and flexible, easy to use, rich documentation;
  • 2, supports parametric, fine-grained control test can be tested;
  • 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, and CI tool can be a good binding, e.g. jenkins

2. Introduction

2.1. Installation

pip install pytest

2.2. Sample Code

Write rules

Write pytest test sample is very simple, just follow these rules:

  • Test file begins test_ (ending _test can)
  • Test classes to begin the test, and not with the init method
  • Test function to begin with test_
  • Assertions to assert basic

pytest1.py

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

@pytest.fixture(scope='function')
def setup_function(request):
    def teardown_function():
        print("teardown_function called.")
    request.addfinalizer(teardown_function)  # 此内嵌函数做teardown工作
    print('setup_function called.')

@pytest.fixture(scope='module')
def setup_module(request):
    def teardown_module():
        print("teardown_module called.")
    request.addfinalizer(teardown_module)
    print('setup_module called.')

@pytest.mark.website
def test_1(setup_function):
    print('Test_1 called.')

def test_2(setup_module):
    print('Test_2 called.')

 

 

fixture of the scope parameters

Four scope parameter, namely 'function', 'module', 'class', 'session', as the default function.

  • function: each test run, the default is a function of the scope
  • class: All test run only once for each class of
  • module: all the module for each test run only once
  • session: Each session runs only once

setup and teardown operations

  • setup, before testing the function or class execution, preparation work, such as database links, test data, open files, etc.
  • tearDown, after performing the test function or class, finishing touches, for example, the database link is disconnected, memory resources recycling
  • Note: You can also yield achieved through the setup and teardown function by function in the fixture

2.3. Test results

How to perform

  • pytest # run all tests below current dir
  • pytest test_mod.py # run tests in module file test_mod.py
  • pytest somepath # run all tests below somepath like ./tests/
  • pytest -k stringexpr # only run tests with names that match the
    # the "string expression", e.g. "MyClass and not method"
    # will select TestMyClass.test_something
    # but not TestMyClass.test_method_simple
  • pytest test_mod.py::test_func # only run tests that match the "node ID",
    # e.g "test_mod.py::test_func" will be selected
    # only run test_func in test_mod.py

Category execution of test methods pytest.mark

@ Pytest.mark control feature through which the test needs to be performed, such as increasing the modified @ pytest.mark.website before executing the test

  • By -m "website" test method has performed website labeled
 
$ pytest  -v -m "website" pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1 -- /home/kevin/soft/anaconda2/bin/python
cachedir: .cache
Using --randomly-seed=1522925202
rootdir: /home/kevin/learn/python-web/tox/case2, inifile:
plugins: randomly-1.0.0, mock-1.2, cov-2.0.0
collected 3 items

pytest1.py::test_1 PASSED

============================================================================= pytest-warning summary =============================================================================
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0.  Please remove the prefix and use the @pytest.fixture decorator instead.
=============================================================================== 2 tests deselected ===============================================================================
=========================================================== 1 passed, 2 deselected, 1 pytest-warnings in 0.00 seconds ============================================================

 

 

No website marked by -m "not website" execute test method

$ pytest  -v -m "not website" pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1 -- /home/kevin/soft/anaconda2/bin/python
cachedir: .cache
Using --randomly-seed=1522925192
rootdir: /home/kevin/learn/python-web/tox/case2, inifile:
plugins: randomly-1.0.0, mock-1.2, cov-2.0.0
collected 3 items

pytest1.py::test_3 PASSED
pytest1.py::test_2 PASSED

============================================================================= pytest-warning summary =============================================================================
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0.  Please remove the prefix and use the @pytest.fixture decorator instead.
=============================================================================== 1 tests deselected ===============================================================================
=========================================================== 2 passed, 1 deselected, 1 pytest-warnings in 0.00 seconds ============================================================

 

 

Console parameters introduced

  • -v for displaying the result of execution of each test function
  • -q Displays only the overall test results
  • -s function for displaying a test print () function output
  • -x, --exitfirst, exit instantly on first error or failed test
  • -h help

Case 1

$ pytest -v pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1 -- /home/kevin/soft/anaconda2/bin/python
cachedir: .cache
Using --randomly-seed=1522920341
rootdir: /home/kevin/learn/python-web/tox/case2, inifile:
plugins: randomly-1.0.0, mock-1.2, cov-2.0.0
collected 3 items

pytest1.py::test_1 PASSED
pytest1.py::test_3 PASSED
pytest1.py::test_2 PASSED

============================================================================= pytest-warning summary =============================================================================
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0.  Please remove the prefix and use the @pytest.fixture decorator instead.
================================================================== 3 passed, 1 pytest-warnings in 0.01 seconds ===================================================================

 

Case 2

$ pytest -s pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1
Using --randomly-seed=1522920508
rootdir: /home/kevin/learn/python-web/tox/case2, inifile:
plugins: randomly-1.0.0, mock-1.2, cov-2.0.0
collected 3 items

pytest1.py setup_function called.
Test_1 called.
.teardown_function called.
setup_module called.
Test_2 called.
.Test_3 called.
.teardown_module called.


============================================================================= pytest-warning summary =============================================================================
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0.  Please remove the prefix and use the @pytest.fixture decorator instead.
================================================================== 3 passed, 1 pytest-warnings in 0.01 seconds ===================================================================

 

3. Extensions

3.1. Test Report

Installation and sample

pip install pytest-cov # calculation pytest coverage, support a variety of formats report test output
pytest --cov-report = html --cov = . / test_code_target_dir

Console parameters introduced

  • --cov = [path], measure coverage for filesystem path (multi-allowed), specifies a test object, for calculating a test coverage
  • --cov-report = type, type of report to generate: term, term-missing, annotate, html, type xml (multi-allowed), the test report
  • --cov-config=path, config file for coverage, default: .coveragerc, coverage配置文件
  • --no-cov-on-fail, do not report coverage if test run fails, default: False, if the test fails, do not generate test reports
  • --cov-fail-under = MIN, Fail if the total coverage is less than MIN. If the test coverage is below MIN, think of failure

Console Result

---------------------------------------------------------------- coverage: platform linux2, python 2.7.14-final-0 ----------------------------------------------------------------
Name         Stmts   Miss  Cover
--------------------------------
pytest1.py      18      0   100%

 

Html Result

 

 

3.2. Randomized test sequence

pip install pytest-randomly

3.3. Distributed Test

pip install pytest-xdist

3.4. Error return immediately

pip install pytest-instafail

4. Reference

 

 

 

 

Guess you like

Origin www.cnblogs.com/zhaoyingjie/p/12525883.html