在用pytest执行用例时,可以按照以下方式来运行用例
1、执行目录及其子目录下的所有用例
pytest 文件名
2、执行某一个py文件下的用例
import pytest
def func(x):
return x
def test_answer():
assert func(5) == 5
def test_answer2():
assert func(5) == 5
def test_answer3():
assert func(5) == 5
3、-k 按关键字匹配
pytest test_class.py -k “TestClass and not two”
运行test_class.py中的TestClass.test_one,不运行TestClass.test_two。
4、按节点运行
每个收集的测试都分配了一个唯一的nodeid,由模块文件名后跟说明符组成。
运行文件中某个测试用例:
pytest test_sample.py::test_answer #文件名::函数名
运行文件中某个测试类中的某个用例:
pytest test_mod.py::TestClass::test_method
5、-m执行标记用例
import pytest
class TestClass(object):
@pytest.mark.webtest
def test_one(self):
x = "this"
assert 'h' in x
def test_two(self):
x = "hello"
assert hasattr(x, 'check')
6、pytest -s
-s:执行用例,携带详细信息,比如打印的print内容.
7、-x 遇到错误时,停止测试
-x:遇到错误的用例,立即退出执行,并输出结果
8、用例错诨个数达到指定数量时,停止测试
pytest --maxfail=1 test_class.py
转载:https://blog.csdn.net/sl01224318/article/details/117403554
查看评论