python - Pytest - 错误与失败

标签 python testing pytest python-unittest

我正在从 PyUnit 迁移到 Pytest,我发现 Pytest 与 PyUnit 不同,在运行测试时(打印点的地方)不会区分快速报告中测试报告中的失败和错误。如何教 Pytest 去做?

更新

似乎它只对使用 Pytest 执行的 PyUnit 测试有效,感谢 flub寻找线索。

代码:

import unittest

class TestErrorFail(unittest.TestCase):
    def test_error(self):
        raise Exception('oops')

    def test_fail(self):
        self.assertTrue(False)

输出:

================================ test session starts =================================
platform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2
plugins: django
collected 2 items 

sometests.py FF

====================================== FAILURES ======================================
______________________________ TestErrorFail.test_error ______________________________

self = <sometests.TestErrorFail testMethod=test_error>

    def test_error(self):
>       raise Exception('oops')
E       Exception: oops

sometests.py:5: Exception
______________________________ TestErrorFail.test_fail _______________________________

self = <sometests.TestErrorFail testMethod=test_fail>

    def test_fail(self):
>       self.assertTrue(False)
E       AssertionError: False is not true

sometests.py:8: AssertionError
============================== 2 failed in 0.69 seconds ==============================

最佳答案

对于pytest,任何在测试函数中抛出的未捕获的异常都是失败的,包括但不限于断言错误。

错误是为 fixture 中的故障保留的。
命名 pytest fixture 中的未捕获异常,如在 flub 的示例中,或在 xUnit style setup/teardown fixtures 中,导致错误而不是失败。

我个人喜欢这种区别。
Failure 表示测试以某种方式失败。
错误表示您无法进行适当的测试。

请注意,即使异常发生在拆卸中,也会发生错误。
在这种情况下,您完成了测试,但拆卸以某种方式失败了。

关于python - Pytest - 错误与失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21513718/

相关文章:

python - Pytest 不收集静态方法

python - 何时以及如何将 `slice(obj)` 用于非 -`int`/`None` 类型?

python - 一列中的多个条目更改 pandas Dataframe 中的输出

python - 不安全请求警告 + pytest

iphone - 如何编写 OCUnit 测试用例

ruby - RSpec -- 测试是否使用之前定义的 block 调用 block

python - 根据命令行参数运行 pytest 标记

python - 什么时候反射(reflection)不再值得?

python - 在 NetworkX 的有向图中查找后继者的后继者

PHPUnit - hasOutput() 不工作