在使用 pytest
进行测试时,测试结果中会显示一些符号来表示每个测试用例的执行状态。以下是常见的符号及其含义:
.
(点号):
E
:
E
后面通常会跟随详细的异常信息,帮助你定位问题。s
:
@pytest.mark.skip
装饰器),或者测试用例满足某些跳过的条件(例如,某些依赖项不可用)。F
: 表示测试用例失败(failed)。这意味着测试用例中的某个断言失败了,但测试用例本身没有抛出异常。x
: 表示测试用例预期失败(expected to fail)。这通常是因为测试用例被标记为预期失败(使用 @pytest.mark.xfail
装饰器),并且它确实失败了。X
: 表示测试用例预期失败但通过了。这通常是因为测试用例被标记为预期失败,但它却意外地通过了。!
: 表示测试用例严重错误(通常是由于测试框架或插件的问题)。假设你有以下测试代码:
import pytest
def test_pass():
assert 1 + 1 == 2
def test_fail():
assert 1 + 1 == 3
def test_error():
raise ValueError("Something went wrong")
@pytest.mark.skip(reason="Not implemented yet")
def test_skip():
assert 1 + 1 == 2
运行 pytest
后,你可能会看到如下输出:
test_example.py::test_pass .
test_example.py::test_fail F
test_example.py::test_error E
test_example.py::test_skip s
.
表示 test_pass
通过了。F
表示 test_fail
失败了。E
表示 test_error
抛出了异常。s
表示 test_skip
被跳过了。.
表示测试通过。E
表示测试执行过程中发生了异常。s
表示测试被跳过。这些符号帮助你快速了解测试的执行情况,从而更好地定位和解决问题。