Pytest测试框架之命令行参数(三)
测试用例的识别与运行
- 测试文件
- test_*.py
- *_test.py
- 用例识别
- Test*类包含的所有test_*的方法(测试类不能带有_init_方法)
- 不在class中的所有的test_*方法
命令行执行
(1)pytest或者py.test:执行该目录下所有符合的测试用例
# perject:demo# name:test_demo1.py# date:2022-4-14def test_demo1(): print("demo1....")
# perject:demo# name:test_demo2.py# date:2022-4-14def test_demo2(): print("demo2....")
# perject:demo# name:test_demo3.py# date:2022-4-14def test_demo3(): print("demo3....")
# perject:demo# name:demo4.py# date:2022-4-14def demo4(): print("demo4")
在命令行输入pytest
在命令行输入py.test
(2)pytest -v :打印详细运行日志信息
可以区别是哪一条用例
# -v后面在跟文件名,不跟文件名,则默认是执行符合规则的pytest测试用例pytest -v
(3)pytest -v -s 文件名:(s是到控制台输出结果,也是输出详细)
(4)pytest 文件名.py:执行单独一个pytest模块
执行test_demo1.py文件
(5)pytest 文件名.py::类名 :运行某个模块里的某个类
新建demo5文件,内容如下:
# perject:demo# name:test_demo5.py# date:2022-4-14class TestDemo5(): def test_demo5_1(self): print("demo5_1....") def test_demo5_2(self): print("demo5_2....")class TestDemo6(): def test_demo6_1(self): print("demo6_1....") def test_demo6_2(self): print("demo6_2....")
想要执行test_demo5.py下的TestDemo5下的所有用例
# 命令行输入pytest test_demo5.py::TestDemo5 -vs
(6)pytest 文件名.py::类名::方法名 :运行某个模块里的某个类里面的方法
想要执行test_demo5.py下的TestDemo6下的test_demo6_1的用例
# 命令行输入pytest test_demo5.py::TestDemo6::test_demo6_1 -vs
(7)pytest -k “标签名” :执行函数名包含某个标签名的用例
import pytestclass TestDemo(): @pytest.mark.p1case @pytest.mark.p2case @pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+5", 7)]) def test_eval(self,test_input, expected): assert eval(test_input) == expected @pytest.mark.p1case @pytest.mark.parametrize('a,b,expect', [ [1, 1, 2], [0.1, 0.1, 0.2] ], ids=['int1', 'float']) def test_add(self,a, b, expect): assert expect == a + b @pytest.mark.demo def test_demo(self): print("这只是一个demo")
执行函数名包含case的用例
pytest -k "case" -vs
(8)pytest -v -k “类名 and not 方法名” :跳过运行某个用例
#跳过test_demo6.py下的TestDemo 下的test_eval的用例pytest test_demo6.py -v -k "TestDemo and not test_eval"
(9)pytest -m [标记名] : @pytest.mark.[标记名]将运行有这个标记的测试用例
#执行Mark标签为p1case用例pytest test_demo6.py -v -m "p1case"
#执行Mark标签为p1case或p2case的用例pytest test_demo6.py -v -m "p1case or p2case"
#执行Mark标签为p1case 且为p2case的用例pytest test_demo6.py -v -m "p1case and p2case"
(10)pytest -x 文件名:一旦运行到报错就停止运行
新建一个test_demo7.py,内容如下:
# perject:demo# name:test_demo7.py# date:2022-4-14def test_demo1(): print("这是demo1")def test_demo2(): assert 4 == 1+2def test_demo3(): print("这是demo3")
pytest -x -vs test_demo7.py
(11)pytest --maxfail=【num】 :当运行错误达到num的时候就停止运行
#错误达到2就停止运行啦pytest test_demo7.py -vs --maxfail=2
(11)pytest执行-失败重新运行
- 场景:测试失败要运行n次,要在重新运行之间添加延迟时间,间隔n秒在运行
- 安装:
pip install pytest-rerunfailures(这个一定要安装,否则在重复执行时会出错)
- 执行:
# 3是重复执行的次数pytest --reruns 3 -v -s test_class.py # 重跑5次,间隔1秒执行pytest -v --reruns 5 --reruns-delay 1
(12)pytest执行-多条断言有失败也都运行
- 场景:一个方法中写多条断言,通常第一条过不去,下面就不执行了。我们想报错也都执行一下。
- 安装:
pip install pytest-assume
- 执行:
pytest.assume(1==4)pytest.assume(2==4)
- 测试用例的识别运行
- pycharm配置与执行pytest测试框架
- 运行方式:pytest.main([’-v’,‘Testclass’])(所有参数和pytest命令方式是一样的)
以上是我的学习记录,希望能帮助到你们,创作不易,给点鼓励~~~
超强干货来袭 云风专访:近40年码龄,通宵达旦的技术人生