Not technically, because the Pytest framework

Keywords: Python unit testing software testing Testing

summary

pytest is a very mature and full-featured Python testing framework. Its main features are as follows:

  • Simple and flexible, easy to use, rich documents;

  • Support parameterization, which can fine-grained control the test cases to be tested;

  • It can support simple unit testing and complex function testing, and can also be used for selenium/appnium and other automated testing and interface automated testing (pytest+requests);

  • Pytest has many third-party plug-ins and can be extended by users, such as pytest selenium (integrated selenium), pytest html (perfect html test report generation), pytest rerunfailures (repeated execution of failure case s), pytest xdist (multi CPU distribution), etc;

  • skip and xfail processing of test cases;

  • It can be well combined with CI tools, such as jenkins

Introduction to use

1. Installation:

pip install pytest

2. Writing rules:

Writing a pytest test sample is very simple. You only need to follow the following rules:

  • Test file to test_ Start (or end with _test)

  • Test class starts with test and cannot have init method

  • Test function with test_ start

  • Assertions can be made using the basic assert

pytest1.py

scope parameter of fixture

  • Function: each test runs. The default is the scope of function

  • Class: all test s of each class are run only once

  • Module: all test s of each module are run only once

  • Session: each session runs only once

setup and teardown operations

  • setup: execute before testing functions or classes to complete preparations, such as database links, test data, open files, etc

  • teardown: execute after testing the function or class to complete the closing work, such as disconnecting the database link, reclaiming memory resources, etc

Note: you can also implement the setup and teardown functions through yield in the fixture function

3. Test results

How to execute

  • 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

Classify and execute the test method through pytest.mark

Use @ pytest.mark to control which feature tests need to be executed. For example, add the modification @ pytest.mark.website before executing the test

  • Execute the test method marked with website through -m "website"
$ 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: .cacheUsing --randomly-seed=1522925202rootdir: /home/kevin/learn/python-web/tox/case2, inifile:plugins: randomly-1.0.0, mock-1.2, cov-2.0.0collected 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 ============================================================
  • Execute the test method without website tag through - m "not website"
$ 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: .cacheUsing 

--randomly-seed=1522925192rootdir: /home/kevin/learn/python-web/tox/case2, inifile:plugins: randomly-1.0.0, mock-1.2, cov-2.0.0collected 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 ============================================================

Introduction to Console parameters

  • -v is used to display the execution results of each test function

  • -q displays only the overall test results

  • -s is used to display the output of the print() function in the test function

  • -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_2PASSED

============================================================================= 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 ===================================================================

03. Extension

1. Test report

Installation and sample

#Calculate pytest coverage and support the output of test reports in multiple formats:

pip install pytest-cov

pytest --cov-report=html --cov=./ test_code_target_dir

Introduction to Console parameters

  • – cov = [path], measure coverage for file system path (multi allowed), which specifies the tested object and is used to calculate the test coverage

  • – cov report = type, type of report to generate: term, term missing, annotate, HTML, XML (multi allowed), type of test report

  • – cov config = path, config file for coverage, default:. Coveragerc, coverage configuration file

  • – no cov on fail, do not report coverage if test run failures, default: false, if the test fails, no test report will be generated

  • – cov fail under = MIN, fail if the total coverage is less than MIN

Console Result

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

Html Result

2. Random test sequence
pip install pytest-randomly

3. Distributed testing
pip install pytest-xdist

4. Error returned immediately
pip install pytest-instafail

Finally, the following is a knowledge architecture diagram of the development direction of software test engineers.

I hope you can benefit a lot from this growth process. It can be said that this process will make you miserable, but as long as you get through it. Life will be much easier in the future. As the saying goes, everything is difficult at the beginning. As long as you take the first step, you will have achieved half of your success. The ancients said that "no small step, no even a thousand miles." when you look back on this journey after it is completed, you will feel a lot of emotion.

Due to the limited size of CSDN uploaded pictures, friends in need can pay attention to my official account: programmers two black, reply 1, you can get the original picture.

The following is a supporting software testing resource package:


The above are some supporting resources. These resources should be the most comprehensive and complete war preparation warehouse for friends of software testing. In order to better sort out each module, I also refer to many high-quality online blogs and projects, and strive not to miss every knowledge point. Many friends reviewed these contents and got offer s from BATJ and other major manufacturers, This warehouse has also helped many learners of software testing. I hope it can also help you. Pay attention to my WeChat official account: programmers two black, free access!

The most difficult time is when we are not far from success! If you don't want to experience the feeling of giving up after a few days when you can't find materials and no one answers questions, you can join our group: 785128166. Let's discuss and exchange learning together.

If you think the article is good, please like, share, watch and collect it, because it will be the strongest driving force for me to continue to output more high-quality articles!

Wonderful recommendation

Working in Ali for 6 years, the voice of a 29 year old female software testing engineer

Refuse the invitation of station B, from 3k a month to 47W a year. My experience is worth learning from every tester

Ali p8, the new comer of the company, read the APP and interface tests I did and gave me this document

Posted by bough on Wed, 22 Sep 2021 07:16:59 -0700