1. First install pytest
pip install pytest
Example 2. Preparation of a single probe
In pytest frame, the following constraint:
- All single measurement file name are required to meet the
test_*.py
format or*_test.py
formats. - In a single test file, can contain
test_
functions that begin with, it may be includedTest
at the beginning of the class. - In a single test class may contain one or more
test_
functions that begin with.
In this case, execute pytest
command, it will automatically look for a test function in line with the above constraints from the current directory and subdirectories to perform.
import pytest # content of test_sample.py def func(x): return x + 1 def test_answer(): assert func(3) == 5
Pytest run or specify a particular file pytest -q test_sample.py
2.1 is running pytest traverse the current directory and subdirectories
See section circled in red, it can draw loop through which the current directory and subdirectories
2.2 pytest -q test_class.py run the specified file
3. Test Case Search
Is defined as: file search test and the test case is called a test search.
Want to be seized, you must comply with the naming of pyteset:
- All single measurement file name are required to meet the
test_*.py
format or*_test.py
formats. - In a single test file, can contain
test_
functions that begin with, it may be includedTest
at the beginning of the class. - In a single test class may contain one or more
test_
functions that begin with.
To remember, test files and test functions must start test_, class is beginning Test.
4. Console explain information
4.1. test session starts
这个是每次运行的分割线
4.2. platform 信息
1.win32 代表用的是windows ,MAC 显示的是darwin
2.接着显示的是Python 和 pytest的版本 , py 和pluggy是pytest包
3.rootdir 当前的起始目录, inifile是指配置文件,后边章节我会详细讲解.
4. collected 5 itemes 一共搜集了多少测试用例.
5. test_run01.py …
测试文件后边的“.” 代表测试通过, F(Fail),E(error),s(skip),X(xpass)>预期失败但是成功了
x(xfail)>预期失败执行也失败了.(下边我讲解具体详情)
6. 4 fail,1passed in 0.06 seconds======
表示通过的数量,没有通过也会总结数量 ,以及花费得时间.
5.使用命令行选项
有了命令行选项使其操作我们的用例变的非常灵活。Unittest 框架如果想执行复杂的场景
变得很鸡肋,这是pytest 优胜Unittest的地方.
以下列举的是常用的命令行选项,已经可以满足我们使用,如果个人想扩展,可以用
pytest --help 可以查到全部的命令 自己可以尝试下,在这不截图了
5.1 -–collect-only
从图片可以看出 一共搜集了5 个测试用例,在执行结果 是 no tests ran 说明只是搜集并不执行。
5.2. -k 选项
重要功能是利用你使用的表达式来 指定希望运行的测试用例.
比如: 你用 pytest -k “表达式”
pytest 根据 你输入的表达式内容进行去匹配 测试用例的名字里是否有这个表达式内容进行过滤.
然后再运行. 如果想查找多个内容,可以用 or 进行连接 ,请看下边的截图:
5.3.-m 选项
m 是marker的缩写,功能是执行自己标记的测试用例,我先讲解如何标记测试用例
如果我们想执行带有 first_case 怎么标记呢
请看下边的实例, 用到了@pytest.mark.first_case, 其中first_case 是装饰器用于标记的内容.
import pytest def inc(x): return x + 1 def test_answer(): assert inc(3) == 5 @pytest.mark.first_case def test_task01(): assert 1 == 1 def test_task02(): assert 1 == 1 def test_task03(): assert 1 == 1
pytest -m "first_case" --collect-only 或者
pytest -m "first_case"
PS E:\Python_Web\pytest-requests-allure> pytest -m "first_case" --collect-only ================================================= test session starts ================================================= platform win32 -- Python 3.7.2, pytest-4.5.0, py-1.8.0, pluggy-0.12.0 rootdir: E:\Python_Web\pytest-requests-allure plugins: allure-pytest-2.6.3, html-1.20.0, metadata-1.8.0 collected 8 items / 6 deselected / 2 selected <Module test_module.py> <Function test_task01> <Function test_task02> ================================================== warnings summary =================================================== d:\software\python\python37\lib\site-packages\_pytest\mark\structures.py:324 d:\software\python\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.first_case - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html PytestUnknownMarkWarning, -- Docs: https://docs.pytest.org/en/latest/warnings.html ====================================== 6 deselected, 1 warnings in 0.11 seconds =======================================
如果想用多个marker ,可以这么写 “marker1 or marker2” 意思是执行 mark1 和mark2所有测试用例
也可以用not 过滤, “marker1 and not marker2” 意思是 执行marker1 并且不包含marker2用例.
6. -x 选项
这个选项是为debug 准备的,正常的流程 是如果断言失败,它会继续执行并不会断掉.如果想在第一个断言失败我就不让继续执行,就可以用-x选项. 遇到断言失败就停止执行.有利于我们查找原因.
7.–lf 选项 (lf 是 last failed 的缩写)
这个选项用处很大,直接过滤出最后一个fail 的测试用例,有利于我们查找失败的用例,解决好,我们再运行这个if 选项 ,可以再找出最后一个fail,省了我们再执行成功的用例,节省我们的时间 .
8.–ff (是 failed first)
和 --lf 不同是的是,它优先运行失败的case ,再把之前运行通过的也要运行. 自己尝试下,这里不做演示
9.-v 选项
这个会在控制台输出更多的内容,最明显的区别就是每个文件中的每个测试用例都占一行(先前是每个文件占一行)
我把这两种情况都打印出来,看下
10 --tb=style 选项
这个选项用于对失败输出信息的显示方式,
输出的信息一般包括 1. 失败出现在哪一行2.是什么失败 3.怎么失败的 这三要素是信息追溯
我们常用的style 有 short ,no, line
实例如下:
no 屏蔽所有的回溯信息,只是显示哪个文件哪个case 失败,
line 具体到哪个assert
short 具体到哪个参数,列举出更详细的失败信息.
实际工作中 -q 和 --tb=line 组合 已经够用.