1、pytest安装:pip install -U pytest -i https://pypi.tuna.tsinghua.edu.cn/simple
2、pytest运行方式:
·不带参数:pytest
·指定模块:pytest .py文件名
·指定目录:pytest dir\
·关键字过滤:pytest -k "MyClass and not method"
·关键字包括 文件名、类名、方法名
·节点id:py模块名::类名::方法名 或者 py模块名::函数名 pytest test_xxx.py::TestXxx::func_xxx
·标签表达式:执行被装饰器 @pytest.mark.smoke 装饰的所有测试用例 pytest -m smoke
·从包里面运行:从pkg.test包所在位置查找和执行用例 pytest --pyargs pkg.test
·停止测试:
-x 遇到错误时停止测试 pytest -x test_xxx.py
当用例错误个数达到指定数量时,停止测试 ytest –maxfail=1
3、pytest运行规则:
·pytest 匹配 当前目录 下以 test_*.py 或者 *_test.py 命名的所有文件
·如果文件中存在以 test_ 开头的函数,则运行所有 test_ 开头的函数
·如果文件中存在以 Test_ 开头的类(没有__init__函数),则匹配 Test_ 类中以 test_ 开头的方法
4、编写一个名为 demo_1.py 的文件
def my_func(x): return x def test_my_func(): assert my_func('hello world!') == 'hello world' class TestMethod: def test_1(self): a = 'hello world!' assert 'hello' in a def test_2(self): x = 'hello world!' assert hasattr(x, 'helloWorld') def others(self): b = 3 assert b == 4 |
1)直接执行 pytest,运行结果未找到可执行用例,因为文件名为 demo_1,不符合pytest运行的规则
2)指定文件名,执行 pytest demo_1.py,用例执行成功,只执行 test_开头的函数,others()方法未执行
5、测试用例设计规则:
·以test_*.py 或者 *_test.py 的文件
·以 test 开头的函数名
·以Test 开头的类(没有__init__函数),以test_开头的函数
6、unittest 框架代码兼容,修改 pycharm 测试脚本的默认执行器为 pytest,再次执行时,会出现 pytest in xx.py 的执行方式。
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理