Official website: https://docs.pytest.org/en/latest/
Pytest to help you write a better program.
1. Installation:
Ubuntu16.04 Installing Pytest:
PIP3 Install Pytest
Pytest--version
This is Pytest version 3.2.3, imported from/usr/local/lib/python3.5/dist-packages/pytest.py
2, an example of a easy test: (a simple example), named test_pytest1.py
1 def funx (x): 2 return x + 1345def test_answer ():6 Assert funx (2) = = 5
Run:
Go to Python script path: pytest test_pytest1.py
[Email protected]:/home/ranxf/python3 Unit Test/demo# pytest test_pytest1.py
============================= test Session starts ==============================
Platform Linux--Python 3.5.2, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
RootDir:/home/ranxf/python3 unit Test/demo, Inifile:
Collected 1 Item
test_pytest1.py F
=================================== Failures ===================================
_________________________________ Test_answer __________________________________
Def test_answer ():
> Assert Funx (2) = = 5
E Assert 3 = = 5
E + where 3 = Funx (2)
Test_pytest1.py:8: Assertionerror
=========================== 1 failed in 0.02 seconds ===========================
Enter the Python script path: Pytest-q test_pytest1.py (plus a parameter-Q) to run the result:
[Email protected]:/home/ranxf/python3 Unit Test/demo# pytest-q test_pytest1.py
F
=================================== Failures ===================================
_________________________________ Test_answer __________________________________
Def test_answer ():
> Assert Funx (2) = = 5
E Assert 3 = = 5
E + where 3 = Funx (2)
Test_pytest1.py:8: Assertionerror
1 failed in 0.02 seconds
There is a little difference between the two running results, which is that some version information is missing.
3. Create multiple test cases in a test class:
1 #a test class to create multiple test cases2 3 4 classTestClass:5 defTest_one (self):6x =" This"7 assert "s" inchx8 9 defTest_two (self):Tenx ="Hello" One assertx = ="Hi"
View Code
Python Unit Test Framework--pytest