【问题标题】:Pytest - error vs failPytest - 错误与失败
【发布时间】:2014-02-02 17:27:01
【问题描述】:

我正在从 PyUnit 迁移到 Pytest,我发现,与 PyUnit 不同,Pytest 在运行测试(打印点)时不会在快速报告中区分测试报告中的失败和错误。如何教 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 ==============================

【问题讨论】:

    标签: python testing pytest python-unittest


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

      据我所知,py.test 确实区分了失败和错误,请考虑以下示例:

      import pytest
      
      def test_fail():
          assert 1 == 2
      
      @pytest.fixture
      def fix():
          raise Exception('oops')
      
      def test_error(fix):
          assert fix == 2
      

      运行这个测试模块会出现一个失败和一个错误:

      ================ test session starts =========================
      platform linux2 -- Python 2.7.5 -- py-1.4.20 -- pytest-2.5.2
      plugins: timeout, capturelog, xdist
      collected 2 items 
      
      ../../tmp/test_foo.py FE
      
      ======================= ERRORS ===============================
      _________________ ERROR at setup of test_error _______________
      
          @pytest.fixture
          def fix():
      >       raise Exception('oops')
      E       Exception: oops
      
      /tmp/test_foo.py:8: Exception
      ====================== FAILURES ==============================
      __________________________ test_fail ____________________________
      
          def test_fail():
      >       assert 1 == 2
      E       assert 1 == 2
      
      /tmp/test_foo.py:4: AssertionError
      ============= 1 failed, 1 error in 0.12 seconds ================
      

      更新

      但是请注意,py.test 将异常期间引发的任何异常视为正常失败。这实际上是一件好事,通常您希望能够以不是 AssertionError(或其子类)的异常使测试失败。在上面的示例中,您会发现错误条件是通过在夹具中引发异常而不是在测试期间触发的。

      但是,使用 UnitTest 类进行尝试后发现,在 .setUp() 方法中引发异常确实会导致失败而不是错误。这可能是一个错误,您可以根据需要报告它。

      【讨论】:

        猜你喜欢
        • 2015-11-30
        • 1970-01-01
        • 1970-01-01
        • 2016-11-02
        • 2016-06-02
        • 1970-01-01
        • 2017-05-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多