[python learning] unit testing framework pytest (1)-28

pytest is a very mature and full-featured Python testing framework, similar to the unittest testing framework that comes with Python, but it is simpler and more efficient to use than the unittest framework.

Pytest is a very mature Python testing framework with the following main features:

  • Very easy to use, easy to get started, rich documentation, there are many examples in the documentation for reference
  • Ability to support simple unit tests and complex functional tests
  • During the execution of the test, some test cases can be skipped (skip), or some cases that are expected to fail can be marked as failed
  • Support repeated execution (rerun) failure case
  • Supports running test cases written by nose, unittest
  • Can generate html report
  • Convenient jenkins continuous integration
  • Can support execution of some use cases
  • Has many third-party plugins and can be custom extended

How to write pytest test cases

  1. Test files start with test_ (and end with _test as well)
  2. A test class starts with Test and cannot have an init method
  3. Test functions start with test_
  4. Assertions can use basic assert

pytest running rules

1. The name of the test file must be in the format of test_*.py, or the format of *_test.py

2. The name of the test class must start with Test, and the class must not have a constructor (__init__)

3. The name of the test function must start with test

pytest installation method:

pip install pytest

In the process of executing code using the pytest unit testing framework in pycharm, switch is performed as follows:

first step:

Step 2: 

 

third step: 

 

 

Example code (1):

def test_a():
    print("第一条测试用例")
def test_b():
    print("第二条测试用例")
def test_c():
    print("第三条测试用例")
def test_d():
    print("第四条测试用例")



-------------------------------------执行结果-------------------------------------

pytestname.py::test_a PASSED                                             [ 25%]第一条测试用例

pytestname.py::test_b PASSED                                             [ 50%]第二条测试用例

pytestname.py::test_c PASSED                                             [ 75%]第三条测试用例

pytestname.py::test_d PASSED                                             [100%]第四条测试用例


============================== 4 passed in 0.50s ==============================

 Example code (2) Console execution result:

 

Example code (2):

Make assertion settings to determine whether the use case is executed or not

a=10
b=5

def test_a():
    print("第一条测试用例")
    assert a>b
def test_b():
    print("第二条测试用例")
    assert a<b
def test_c():
    print("第三条测试用例")
    assert a==b
def test_d():
    print("第四条测试用例")
    assert a!=b

Example code (2) Console execution result:

 Example code (2) Detailed explanation of console execution results:

Note: As long as the " AssertionError " use case occurs, the execution fails

 

The use case fails:

        1. An " AssertionError " occurred

        2. The use case throws other exceptions

Guess you like

Origin blog.csdn.net/admins_/article/details/122154761