1.在第一个(或N个)失败后停止
example 文件: test_fail.py
def func(x):
return x + 2
def test_anwser(): # pass
assert func(3) == 5
class TestMyClass:
def test_one(self): #fail
x = "this"
assert "hss" in x
def test_two(self): #fail
x = "hello"
assert x in 'wqewqeq'
def test_three(self):
x = "this"
assert "h123123" in x
def test_four(self):
x = "this"
assert "h" in x
pytest --maxfail=2 test_fail.py
达到第二个失败的时候停止
-如文件 test_fail.py,执行后两个失败一个成功。执行到第二个失败会终止。不会到执行方法test_three
2.执行文件、文件夹
pytest test_mod.py
执行该模块下的测试类测试方法pytest testing
执行该文件夹下的所有模块
3.通过关键字表达式运行测试
-
pytest -k "MyClass and not one" test_fail.py
- 必须是双引号
- and是并且的意思
- 执行test_fail.py文件内的TestMyClass类下面除了one的所有方法(two、three、four)
-
pytest -k "MyClass or anwser" test_fail.py
- 执行test_fail.py文件内的所有方法
4.执行特定方法
pytest test_cgi.py::test_answer
,执行test_answer方法pytest test_cgi.py::TestMyClass::test_one
,执行TestMyClass类下的test_one方法
5.查看详细的输出
- -r随意拼接下方字符
- 如 -rfs(查看失败和跳过的测试)、-rs(查看跳过的测试)
这是可以使用的可用字符的完整列表:
- f -失败
- E -错误
- s -跳过
- x -xfailed
- X -xpass
- p -通过
- P -通过输出