项目目录下运行 pytest —> 寻找 当前目录及子目录下以 test_*.py 或 *_test.py 的文件 —> 寻找 Test开头的类 —> 寻找以 test开头函数 。
1.查看pytest命令行参数,可以用 pytest -h 或 pytest --help 查看
C:\Users\admin>pytest -h usage: pytest [options] [file_or_dir] [file_or_dir] [...] positional arguments: file_or_dir general: -k EXPRESSION only run tests which match the given substring expression. An expression is a python evaluatable expression where all names are substring-matched against test names and their parent classes. Example: -k ‘test_method or test_other‘ matches all test functions and classes whose name contains ‘test_method‘ or ‘test_other‘, while -k ‘not test_method‘ matches those that don‘t contain ‘test_method‘ in their names. Additionally keywords are matched to classes and functions containing extra names in their ‘extra_keyword_matches‘ set, as well as functions which have names assigned directly to them. -m MARKEXPR only run tests matching given mark expression. example: -m ‘mark1 and not mark2‘. --markers show markers (builtin, plugin and per-project ones). -x, --exitfirst exit instantly on first error or failed test reporting: -v, --verbose increase verbosity. -q, --quiet decrease verbosity. --verbosity=VERBOSE set verbosity 只贴了一部分
test_sample.py 代码如下:
# -*- coding:utf-8 -*- # @Author: Sky # @Email: 2780619724@qq.com # @Time: 2021/7/13 14:55 def func(x): return x + 1 def test_answer(): assert func(3) == 5
test_class.py 代码如下:
# -*- coding:utf-8 -*- # @Author: Sky # @Email: 2780619724@qq.com # @Time: 2021/7/13 14:55 class TestClass: def test_one(self): x = "this" assert ‘h‘ in x def test_two(self): x = "hello" assert hasattr(x, ‘check‘) def test_three(self): a = "hello" b = "hello world" assert a in b
cmd执行pytest用例有三种方法,以下三种方法都可以,一般推荐第一个
如果不带参数,在某个文件夹下执行时,它会查找该文件夹下所有的符合条件的用例(查看用例设计原则)
1.执行某个目录下所有的用例
pytest 目录名
2.执行某一个py文件下用例
pytest 脚本名称.py (py文件的相对路径)
3.-k 按关键字匹配
pytest -k "MyClass and not method" (包含 MyClass 不包含 method )
pytest -k "test and TestClass and not test_a" test_03.py 指定运行 test_03.py 中包含test 、TestClass 但 不包含 test_a 的运行;
pytest -k "test and TestClass and not test_a" 寻找当前目录下所有文件中,包含test 、TestClass 但 不包含 test_a 的运行;
4.按节点运行
每个收集的测试都分配了一个唯一的nodeid,它由模块文件名和后跟说明符组成
来自参数化的类名,函数名和参数,由 :: characters分隔。(两个分号)
运行.py模块里面的某个函数 ( .py 模块里面没有类时,模块::方法 ,如果有类则必须 模块::类::方法)
pytest test_mod.py::test_func
运行.py模块里面,测试类里面的某个方法
pytest test_mod.py::TestClass::test_func
5.标记表达式
pytest -m slow
将运行用 @ pytest.mark.slow 装饰器修饰的所有测试
代码如下:
import pytest class TestClass(object): @pytest.mark.aa def test_01(self): print(‘====test_01==aaaaaaa‘) x = "this" assert ‘h‘ in x @pytest.mark.slow def test_02(self): print(‘======test_02=======‘) x = "hello" assert hasattr(x, ‘check‘) @pytest.mark.slow def test_a(self): print(‘=========test_a========‘) assert 1 == 2
执行结果如下:
6、从包里面运行
pytest --pyargs pkg.testing
这将导入pkg.testing 并使用其文件系统位置来查找和运行测试。pytest --pyargs mytest
pytest -x test_class.py
从运行结果可以看出,本来有3个用例,第二个用例Fail后就没继续往下执行了
pytest --maxfail=1
当用例错误个数达到指定数量时,停止测试。
原文:https://www.cnblogs.com/qq-2780619724/p/15007328.html