关于PyCharm 2018测试失败的两个追溯

时间:2018-05-21 06:56:46

标签: python pycharm python-unittest

我在first()文件中进行了简单的测试:

site_tests.py

当我使用默认的PyCharm配置运行“testtest in test.site.py”时,我得到了:

import unittest


class SiteTests(unittest.TestCase):

    def test(self):
        self.assertEqual('a', 'b')

if __name__ == '__main__':
     unittest.main()

最后一部分非常有趣,因为在没有Testing started at 23:45 ... C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.3\helpers\pycharm\_jb_unittest_runner.py" --path C:/testSiteDemoTests/site_tests.py Launching unittests with arguments python -m unittest C:/testSiteDemoTests/site_tests.py in C:\testSiteDemoTests b != a Expected :a Actual :b <Click to see difference> Traceback (most recent call last): File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.3\helpers\pycharm\teamcity\diff_tools.py", line 32, in _patched_equals old(self, first, second, msg) File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 829, in assertEqual assertion_func(first, second, msg=msg) File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 1202, in assertMultiLineEqual self.fail(self._formatMessage(msg, standardMsg)) File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 670, in fail raise self.failureException(msg) AssertionError: 'a' != 'b' - a + b During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 59, in testPartExecutor yield File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 605, in run testMethod() File "C:\testSiteDemoTests\site_tests.py", line 7, in test self.assertEqual('a', 'b') Ran 1 test in 0.000s FAILED (failures=1) Process finished with exit code 1 的情况下运行此文件所以_jb_unittest_runner.py 输出没问题:

C:\testSite>python lost_hat_tests.py

为什么第二条消息出现在PyCharm跑步者中有简单的答案吗?

我还在控制台中使用F ====================================================================== FAIL: test (__main__.SiteTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "lost_hat_tests.py", line 7, in test self.assertEqual('a', 'b') AssertionError: 'a' != 'b' - a + b ---------------------------------------------------------------------- Ran 1 test in 0.000s FAILED (failures=1) 运行测试 - 嗯两个testSuites - 有趣的

_jb_unittest_runner.py

1 个答案:

答案 0 :(得分:2)

如果第一个This message shows upanother exception was raised in an exception handler or finally clause,则在Python 3中的异常格式中

upgrade documentation for Elm 0.18

  

如果在异常处理程序或finally子句中引发异常,则类似的机制会隐式工作:然后将先前的异常附加为新异常的__context__属性

在PyCharm中为测试设置断点,然后进一步进入机器,显示抛出第二个异常的位置。 _jb_unittest_runner修补unittest中的断言方法:

PyCharm Community Edition\helpers\pycharm\_jb_unittest_runner.py

from teamcity import unittestpy

PyCharm Community Edition\helpers\pycharm\teamcity\unittestpy.py

def run(self, test):
    <...>
    patch_unittest_diff(subtest_filter)
    <...>

PyCharm Community Edition\helpers\pycharm\teamcity\diff_tools.py

def patch_unittest_diff(<...>):

    old = unittest.TestCase.assertEqual

    def _patched_equals(self, first, second, msg=None):
        try:
            old(self, first, second, msg)
            return
        except AssertionError as native_error:
            if not test_filter or test_filter(self):
                error = EqualsAssertionError(first, second, msg)
                if error.can_be_serialized():
                    raise error
            raise native_error

    unittest.TestCase.assertEqual = _patched_equals