pytest-pytest.main()运行测试用例,pytest参数

原文地址:https://blog.csdn.net/weixin_44006041/article/details/107934174

本文介绍pytest.main运行测试用例的方法

pytest.main():main中传入不同的指令用以执行指定测试用例

-s: 显示程序中的print/logging输出

-v: 丰富信息模式, 输出更详细的用例执行信息

-q: 安静模式, 不输出环境信息

-k:关键字匹配,用and区分:匹配范围(文件名、类名、函数名)

 

 

 

 test_module1.py

 1 def test_m1_1():
 2     print('这是 subpath1/test_module1.py::test_m1_1')
 3     assert 1==1
 4 
 5 def test_m1_2():
 6     print('这是 subpath1/test_module1.py::test_m1_2')
 7     assert 2==2
 8 
 9 def test_spec_1():
10     print ('这是 subpath1/test_module1.py::test_spec_1')
11     assert 2 == 2

test_module2.py

 1 def test_m2_01():
 2     print('这是 subpath1/test_module1.py::test_m1_1')
 3     assert 1==1
 4 
 5 class TestM2():
 6     def test_m2_02(self):
 7         print ('这是 subpath2/test_module2.py::TestM2::test_m2_02')
 8         assert 1==1
 9 
10     def test_pp(self):
11         print ('这是 subpath2/test_module2.py::TestM2::test_pp')
12         assert 1 == 1

maintest.py

1 import pytest
2 
3 if __name__ == '__main__':
4     # 运行当前目录下所有(test_*.py  和 *_test.py)
5     pytest.main()

运行结果:

1 ============================= test session starts =============================
2 platform win32 -- Python 3.6.6rc1, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
3 rootdir: E:\study\python\study\BasicKnowledgePoints\s5_frame\f001_pytest_用例运行
4 collected 6 items
5 
6 subpath1\test_module1.py ...                                             [ 50%]
7 subpath2\test_module2.py ...                                             [100%]
8 
9 ============================== 6 passed in 0.15s ==============================

 

 

 

 

 

 

 

 

 

本文介绍pytest.main运行测试用例的方法pytest.main():main中传入不同的指令用以执行指定测试用例-s: 显示程序中的print/logging输出-v: 丰富信息模式, 输出更详细的用例执行信息-q: 安静模式, 不输出环境信息-k:关键字匹配,用and区分:匹配范围(文件名、类名、函数名)
示例
test_module1.py
def test_m1_1():    print('这是 subpath1/test_module1.py::test_m1_1')    assert 1==1
def test_m1_2():    print('这是 subpath1/test_module1.py::test_m1_2')    assert 2==2
def test_spec_1():    print ('这是 subpath1/test_module1.py::test_spec_1')    assert 2 == 21234567891011test_module2.py
def test_m2_01():    print('这是 subpath1/test_module1.py::test_m1_1')    assert 1==1
class TestM2():    def test_m2_02(self):        print ('这是 subpath2/test_module2.py::TestM2::test_m2_02')        assert 1==1
    def test_pp(self):        print ('这是 subpath2/test_module2.py::TestM2::test_pp')        assert 1 == 1123456789101112maintest.py
import pytest
if __name__ == '__main__':    # 运行当前目录下所有(test_*.py  和 *_test.py)    pytest.main()12345运行结果:
============================= test session starts =============================platform win32 -- Python 3.6.6rc1, pytest-5.4.1, py-1.8.1, pluggy-0.13.1rootdir: E:\study\python\study\BasicKnowledgePoints\s5_frame\f001_pytest_用例运行collected 6 items
subpath1\test_module1.py ...                                             [ 50%]subpath2\test_module2.py ...                                             [100%]
============================== 6 passed in 0.15s ==============================1234567891、运行指定路径下的用例
pytest.main(['./'])               # 运行./目录下所有(test_*.py  和 *_test.py)1pytest.main (['./subpath1'])    # 运行./subpath1 目录下用例1pytest.main (['./subpath1/test_module1.py'])    # 运行指定模块1pytest.main (['./subpath1/test_module1.py::test_m1_1'])  # 运行模块中的指定用例1pytest.main (['./subpath2/test_module2.py::TestM2::test_m2_02'])  # 运行类中的指定用例1pytest.main (['-k','pp'])         # 匹配包含pp的用例(匹配目录名、模块名、类名、用例名)1pytest.main(['-k','spec','./subpath1/test_module1.py'])     # 匹配test_module1.py模块下包含spec的用例1pytest.main(['-k','pp','./subpath2/test_module2.py::TestM2'])   # 匹配TestM2类中包含pp的用例12、运行参数
pytest.main(['-s','./subpath1/test_module1.py'])        # -s: 显示程序中的print/logging输出1运行结果:
subpath1\test_module1.py 这是 subpath1/test_module1.py::test_m1_1.这是 subpath1/test_module1.py::test_m1_2.这是 subpath1/test_module1.py::test_spec_1.1234pytest.main(['-v','./subpath1/test_module1.py'])        # -v: 丰富信息模式, 输出更详细的用例执行信息1运行结果:
subpath1/test_module1.py::test_m1_1 PASSED                               [ 33%]subpath1/test_module1.py::test_m1_2 PASSED                               [ 66%]subpath1/test_module1.py::test_spec_1 PASSED                             [100%]
============================== 3 passed in 0.06s ==============================12345pytest.main(['-q','./subpath1/test_module1.py'])        # -q: 安静模式, 不输出环境信息1运行结果:
...                                                                      [100%]3 passed in 0.06s12pytest.main(['-v','-s','./subpath1/test_module1.py'])        # 多个参数组合1运行结果:
subpath1/test_module1.py::test_m1_1 这是 subpath1/test_module1.py::test_m1_1PASSEDsubpath1/test_module1.py::test_m1_2 这是 subpath1/test_module1.py::test_m1_2PASSEDsubpath1/test_module1.py::test_spec_1 这是 subpath1/test_module1.py::test_spec_1PASSED
============================== 3 passed in 0.10s ==============================————————————————版权声明:本文为CSDN博主「小白爱吃饭」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/weixin_44006041/article/details/107934174

posted @ 2021-05-05 22:47  Chaman囍  阅读(863)  评论(0编辑  收藏  举报