[python learning] unit testing framework pytest (2)-29

pytest: write test cases - collect test cases - execute test cases - generate test reports

Write a test case:
   use case name, use case steps, expected result, actual result, pre and post
   1, use case name: start with test_
   2, assert assert: (actual and expected comparison) assert expression (True/False )

Automatic collection of test cases:
    1. Directory for collection of test cases: take rootdir as the root directory. Start searching for use cases from the rootdir directory.
    2. File filtering in the directory: py files whose file names start with test_, or py files whose file names end with _test.
    3. Use case filtering under the file: functions under .py, function names start with test_/methods in classes under .py (start with Test), method names start with test_

Such as: test_case01.py

from random import randint

def random_num():
    return randint(1,10)

def test_1():
    print("第一条测试用例")
    assert random_num() == 2

def test_2():
    print("第二条测试用例")
    assert random_num() == 4

def test_3():
    print("第三条测试用例")
    assert random_num() == 6

def test_4():
    print("第四条测试用例")
    assert random_num() == 8

def test_5():
    print("第五条测试用例")
    assert random_num() == 10

def test_6():
    print("第六条测试用例")
    assert random_num() == 1

In the root directory, create a py file, such as: currently named: test.py

Note: When the test.py file is executed, it will automatically search for files starting with test_ in the current root directory, execute the test cases in the py file, and output the test report

import pytest
pytest.main(["-s","-v","--html=测试报告.html","--alluredir=allure-report-files"])

The output html test report is as follows:

 html report - html plugin
        1. pip install pytest-html
        2. Add parameters to the pytest command: --html=report path (relative to rootdir)) 

-------------------------------------------------------------------------------------------------------------------------------

 The test case file can be executed through the pycharm console:

1. Select Terminal, and then enter the current file directory

2. Enter in the console: pytest

 

 The output results are as follows:

(Current execution results: 5 failures, 1 success)

 

Guess you like

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