pythonUI自动化之pytest的组成以及运行(008)

pytest组成:

  测试模块:以“test”开头或结尾的py文件

  测试用例:在测试模块里或测试类里,名称符合test_xxx函数或者示例函数。

  测试类:测试模块里面命名符合Test_xxx的类

函数级:

import pytest


class TestReport:
    # 函数级开始
    def setup(self):
        print(1)

    # 函数级结束
    def teardown(self):
        print(3)

    def test_001(self):
        print(2)

    def test_002(self):
        print(4)


if __name__ == '__main__':
    pytest.main(["-s"])
运行结果:
test_20211217_01.py 1
2
.3
1
4
.3

 

类级:

import pytest

class TestReport:
    # 函数级开始
    def setup_class(self):
        print(1)

    # 函数级结束
    def teardown_class(self):
        print(3)

    def test_001(self):
        print(2)

    def test_002(self):
        print(4)

if __name__ == '__main__':
    pytest.main(["-s"])
运行结果:
test_20211217_01.py 1
2
.4
.3

 

pytest运行方式:第一种:main函数执行

pytest.main(["-s","../web_key/test_20211217_01.py"])

 

 

第二种:命令行运行

点击“终端”, 先进入所属目录, 输入命令:pytest 测试文件名。 或直接输入pytest,自动执行目录下所有test开头的py文件。

 

 

第三种:另外新增一个文件,写main方式运行。可以执行多个py文件

 

第四种:配置文件运行(模糊匹配执行文件,*代表通配符)

[pytest]
# 命令行参数
# addopts = --alluredir ./temp -s
# 搜索文件名
pytest_files = *20211217*.py
# 搜索的类名
pytest_classes = Test_*
# 搜索的函数名
pytest_functions = test_*

 

 

 

 

 

 

 

 

 

 

 

 

 

    
posted @ 2021-10-26 22:10  博无止境  阅读(148)  评论(0)    收藏  举报