2、pytest测试用例的运行方式

234 阅读2分钟

1、主函数模式

前置条件:

我们有test_first和test_second2个用例脚本都放在test_testcase目录下面 test_first代码如下

#test_Pytest.py文件
#coding=utf-8
import time
import pytest

class Test_Pytest():
        def test_one(self,):
                print("test_one方法执行" )



        def test_two(self):
                print("test_two方法执行" )


        def test_three(self):
                print("test_three方法执行" )

test_second代码如下

#test_Pytest.py文件
#coding=utf-8
import time

import pytest

class Test_Pytest():

        def test_four(self,):
                print("test_four方法执行" )


        def test_five(self):
                print("test_five方法执行" )


        def test_six(self):
                print("test_six方法执行" )

(1)运行所有测试用例:pytest.main()

启动代码如下所示

import pytest

if __name__=="__main__":
    pytest.main()

可以看到所有测试用例都被执行

image.png

(2)指定模块:pytest.main(['test_testcase/test_first.py'])

启动代码如下所示

import pytest

if __name__=="__main__":
    pytest.main(['test_testcase/test_first.py'])

执行结果如下: 可以看到test_testcase\test_first.py这个模块文件用例被执行

image.png

(3)指定目录:pytest.main(['test_testcase'])

可以看到这个目录下面所有用例被执行

image.png

(4)多线程执行测试用例:pytest.main(['-vs','-n=2'])

可以看到分为2个线程执行用例

image.png

(5)失败重跑:pytest.main('--reruns=2')

先断言失败用例

image.png 可以看到这个用例被执行了三次

image.png

(6)自定义参数

-s:表示输出调试信息,包括print打印的信息

-v:显示更详细的信息

-vs:2个参数可以一起用

-n;支持多线程或者分布式运行测试用例;eg:pytest -vs test_testcase -n 2

--reruns NUM:失败用例重跑;

-x:表示只要有一个用例报错就停止执行;

--maxfail=2;表示出现2个用例失败就停止;

-k:表示根据测试用例的部分字符串指定用例; 示例:

pytest.main(['-vs','--reruns=2'])

结果如下所示 ============================= test session starts ============================= platform win32 -- Python 3.8.8, pytest-7.1.2, pluggy-1.0.0 -- E:\1.自动化\haitun\venv\Scripts\python.exe cachedir: .pytest_cache metadata: {'Python': '3.8.8', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '7.1.2', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins': {'assume': '2.4.3', 'forked': '1.4.0', 'html': '3.1.1', 'metadata': '2.0.1', 'rerunfailures': '10.2', 'xdist': '2.5.0'}, 'JAVA_HOME': 'C:\Program Files\Java\jdk1.8.0_144'} rootdir: E:\1.自动化\pytest+allure plugins: assume-2.4.3, forked-1.4.0, html-3.1.1, metadata-2.0.1, rerunfailures-10.2, xdist-2.5.0 collecting ... collected 6 items

test_testcase/test_first.py::Test_Pytest::test_one test_one方法执行 PASSED test_testcase/test_first.py::Test_Pytest::test_two test_two方法执行 PASSED test_testcase/test_first.py::Test_Pytest::test_three test_three方法执行 RERUN test_testcase/test_first.py::Test_Pytest::test_three test_three方法执行 RERUN test_testcase/test_first.py::Test_Pytest::test_three test_three方法执行 FAILED test_testcase/test_second.py::Test_Pytest::test_four test_four方法执行 PASSED test_testcase/test_second.py::Test_Pytest::test_five test_five方法执行 PASSED test_testcase/test_second.py::Test_Pytest::test_six test_six方法执行 PASSED

================================== FAILURES =================================== ___________________________ Test_Pytest.test_three ____________________________

self = <test_first.Test_Pytest object at 0x000001FE60170A60>

def test_three(self):
        print("test_three方法执行" )
      assert 1==2

E assert 1 == 2

test_testcase\test_first.py:18: AssertionError =========================== short test summary info =========================== FAILED test_testcase/test_first.py::Test_Pytest::test_three - assert 1 == 2 ==================== 1 failed, 5 passed, 2 rerun in 0.05s =====================

Process finished with exit code 0

2、命令行模式

打开pycharm的terminal

(1)运行所有:pytest

运行结果如下所示

image.png

(2)指定模块:pytest test_testcase/test_first.py

运行结果如下图所示 image.png

(3)指定目录:pytest test_testcase

运行结果如下图所示 image.png

(4)多线程执行用例:pytest -vs -n 2

这里加vs是为了看详细的线程执行情况

image.png

(5)失败重跑:pytest -vs --reruns 2

image.png 更多使用可以参考官方文档:www.osgeo.cn/pytest/cont…