Pytest测试框架(一):pytest安装及用例执行

PyTest是基于Python的开源测试框架,语法简单易用,有大量的插件,功能非常多。自动检测测试用例,支持参数化,跳过特定用例,失败重试等功能。

安装 pip install -U pytest  # 安装 pytest --version # 查看版本 pytest -h # 查看帮助信息 用例识别

pytest识别文件名为test_.py或者_test.py的测试文件

测试文件中可以在Test*类中写测试用例(测试用例以test_*开头,并且测试类不能有__init__方法)

不在Test类中的test_用例也可以被识别到。

识别包含“add”的用例:

pytest -k "add" --collect-only

(base) D:\ProgramWorkspace\TestingDemo>pytest -k "add" --collect-only ============================================== test session starts ============================================== platform win32 -- Python 3.7.6, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 rootdir: D:\ProgramWorkspace\TestingDemo plugins: hypothesis-5.5.4, arraydiff-0.3, astropy-header-0.1.2, doctestplus-0.5.0, openfiles-0.4.0, remotedata-0.3 .2 collected 20 items / 11 deselected / 9 selected                                                                  <Package D:\ProgramWorkspace\TestingDemo\testing>   <Module test_calc.py>     <UnitTestCase TestCalc>       <TestCaseFunction test_add_1>       <TestCaseFunction test_add_2>   <Module test_calc2.py>     <Class TestCalc>         <Function test_add[1-2-3]>         <Function test_add[-1--2--3]>         <Function test_add[0-1-1]>         <Function test_add[0--1--1]>         <Function test_add[0.1-0.2-0.3]>         <Function test_add[999999-1000000-1999999]>         <Function test_add_1>

打印文件下所有用例:

pytest --collect-only

(base) D:\ProgramWorkspace\TestingDemo>pytest --collect-only ============================================== test session starts ============================================== platform win32 -- Python 3.7.6, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 rootdir: D:\ProgramWorkspace\TestingDemo plugins: hypothesis-5.5.4, arraydiff-0.3, astropy-header-0.1.2, doctestplus-0.5.0, openfiles-0.4.0, remotedata-0.3 .2 collected 20 items                                                                                              <Module test_pytest.py>   <Function test_one>   <Function test_two>   <Function test_three> <Module test_pytest2.py>   <Class Test_Demo>       <Function test_one>       <Function test_two>       <Function test_three> <Package D:\ProgramWorkspace\TestingDemo\testing>   <Module test_calc.py>     <UnitTestCase TestCalc>       <TestCaseFunction test_add_1>       <TestCaseFunction test_add_2>   <Module test_calc2.py>     <Class TestCalc>         <Function test_add[1-2-3]>         <Function test_add[-1--2--3]>         <Function test_add[0-1-1]>         <Function test_add[0--1--1]>         <Function test_add[0.1-0.2-0.3]>         <Function test_add[999999-1000000-1999999]>         <Function test_div[1-2-0.5]>         <Function test_div[-1--2-0.5]>         <Function test_div[0-1-0]>         <Function test_div[1-0-0]>         <Function test_div[0.1-0.2-0.5]>         <Function test_add_1> Pytest执行

一个简单的测试用例test_pytest.py:

import pytest def calc(a,b):     return a + b class TestDemo():     def test_answer1(self):                assert calc(1, 1) == 2     def test_answer2(self):                assert calc(2, 1) == 3 @pytest.mark.answer3            def test_answer3(self):                assert calc(6, 6) == 12 if __name__=='__main__':     pytest.main() 用例执行 # 执行test_pytest.py所有用例(模块) pytest test_pytest.py # 执行test_pytest.py里的TestDemo类 pytest test_pytest.py::TestDemo # 执行test_pytest.py里的TestDemo类的test_answer2方法 pytest test_pytest.py::TestDemo::test_answer2 打印日志信息 # 打印详细运行日志信息 pytest -v test_pytest.py pytest -s test_pytest.py

Pytest测试框架(一):pytest安装及用例执行

跳过某个用例 # 跳过运行某个用例 pytest -v -k "类名 and not方法名" 文件名 pytest -v -k "TestDemo and not test_answer2" test_pytest.py

Pytest测试框架(一):pytest安装及用例执行

运行某个标记的用例 # 将运行有这个标记的测试用例:@pytest.mark.[标记名] pytest -m [标记名] 文件名 pytest -m answer3 test_pytest.py

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zygyfs.html