Introduced
Pytest is a full-fledged, fully functional Python testing tool that helps you to write better programs.
Suitable for testing from simple units to complex functions
- L Modular parametrizeable Device (in 2.3, continuous improvement)
- L Parametric test function (use case)
- L Mark test functions and properties
- L Skip and Xfail: Processing unsuccessful test cases (improved in 2.4)
- L distributed test to multiple CPUs via xdist plug-in
- L continually re-run failed tests
- • Flexible conventions for Python test discovery
Home page:http://pytest.org
Installation
>pip install-u pytest # Install via PIP
>py.test--version # View Pytest version
This was Pytest version 2.7.2, imported from C:\PYTHON27\LIB\SITE-PACKAGES\PYTEST.PYC
A simple test
Let's create the first file and test it for a simple function.
# Coding=utf-8 # function def func (x): return x + 1# Test Case def test_answer (): assert Func (3) = = 5
Switch to the directory where the test file is located and run the test with the "py.test" command.
>py.test
Execution results such as:
===================================================================
Create multiple test cases in a test class:
#Coding=utf-8classTestClass:defTest_one (self): x=" This" assert "h" inchxdefTest_two (self): x="Hello" assertx = ="Hi"
To run the test:
>py.test-q test_class.py
- Q to be Quiet . Indicates that the output in quiet mode is called. What's the difference between adding and not adding this argument? The reader can compare the logs of the output two times. In fact, is a few pytest version of the information.
===================================================================
calling pytest from Python code
The main () function is also provided in Pytest to execute the test case.
pytest/
├──test_sample.py
├──test_class.py
└──test_main.py
This directory is for our practice directory, open test_mian.py
Import pytest def Test_main (): assert 5! = 5If__name__'__main__': Pytest.main ()
run the program directly, Sublime in press ctrl+b run. The results are as follows:
============================= test Session starts =============================Platform Win32--Python 2.7.10--py-1.4.30--pytest-2.7.2Rootdir:d:\pyse\pytest, inifile:collected4Itemstest_class.py. ftest_main.py ftest_sample.py F ================================== Failures ===================================_____________________________Testclass.test_two______________________________ Self= <test_class. TestClass instance at 0x000000000304f548>defTest_two (self): x="Hello">assertx = ="Hi"Eassert 'Hello'=='Hi'E-Helloe+hitest_class.py:11: Assertionerror__________________________________Test_main__________________________________ defTest_main ():>assert5! = 5Eassert5! = 5test_main.py:4: Assertionerror_________________________________Test_answer_________________________________ deftest_answer ():>assertFunc (3) = = 5Eassert4 = = 5E+ WHERE 4 = func (3) test_sample.py:9: Assertionerror===================== 3 failed, 1 passedinch0.03 seconds ======================[Finishedinch0.3S]
As seen from the execution result,main () defaults to all test files in the same directory as the current file.
So, what if we just want to run a test file? You can add parameters to main () , just as you would at the cmd command prompt:
# Coding=utf-8 Import pytest def Test_main (): assert 5! = 5If__name__'__main__': Pytest.main ("-q test_main.py") # specifying test files
Operation Result:
F================================== failures ===================================___________________ _________________________________________________ def Test_main ():> assert 5! =5 Eassert 5! = 5test_main.py:4: Assertionerror in 0.01 seconds
What if I want to run a test case in a directory? Specify the test directory.
# Coding=utf-8 Import pytest def Test_main (): assert 5! = 5If__name__'__main__': Pytest.main ("d:/pyse/pytest/") # Specify test directory
===================================================================
* Finally,pytest If the test case is identified? as with the unittest Unit Test Framework, it defaults to checking methods or functions that begin with test and executing them.
Pytest There are a lot of places to discuss, as the first section of this series, first introduced here.
The pytest of the Python Unit test framework---How to execute test cases