【问题标题】:Two tracebacks on test failure in PyCharm 2018PyCharm 2018 中测试失败的两个回溯
【发布时间】:2018-05-21 06:56:46
【问题描述】:

我在site_tests.py 文件中有简单的测试:

import unittest


class SiteTests(unittest.TestCase):

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

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

当我使用默认 PyCharm 配置运行“Unittest in test_site.py”时,我得到:

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&gt;python lost_hat_tests.py 输出没问题:

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)

是否有简单的答案为什么第二条消息出现在 PyCharm 运行器中?

我还在控制台中使用 _jb_unittest_runner.py 运行测试 - 嗯,两个 testSuites - 很有趣

C:\testSiteDemoTests>python "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.3\helpers\pycharm\_jb_unittest_runner.py" --path C:/testSiteDemoTests/site_tests.py
##teamcity[enteredTheMatrix timestamp='2018-05-20T23:59:59.931']
Launching unittests with arguments python -m unittest C:/testSiteDemoTests/site_tests.py in C:\testSiteDemoTests

##teamcity[testCount timestamp='2018-05-20T23:59:59.946' count='1']
##teamcity[testSuiteStarted timestamp='2018-05-20T23:59:59.946' locationHint='python<C:\testSiteDemoTests>://site_tests' name='site_tests' nodeId='1' parentNodeId='0']
##teamcity[testSuiteStarted timestamp='2018-05-20T23:59:59.946' locationHint='python<C:\testSiteDemoTests>://site_tests.SiteTests' name='SiteTests' nodeId='2' parentNodeId='1']
##teamcity[testStarted timestamp='2018-05-20T23:59:59.962' captureStandardOutput='true' locationHint='python<C:\testSiteDemoTests>://site_tests.SiteTests.test' name='test' nodeId='3' parentNodeId='2']
##teamcity[testFailed timestamp='2018-05-20T23:59:59.977' actual='b' details='Traceback (most recent call last):|n  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.3\helpers\pycharm\teamcity\diff_tools.py", line 32, in _patched_equals|n    old(
self, first, second, msg)|n  File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 829, in assertEqual|n    assertion_func(first, second, msg=msg)|n  File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\
lib\unittest\case.py", line 1202, in assertMultiLineEqual|n    self.fail(self._formatMessage(msg, standardMsg))|n  File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 670, in fail|n    raise self.failureException(msg)
|nAssertionError: |'a|' != |'b|'|n- a|n+ b|n|n|nDuring handling of the above exception, another exception occurred:|n|nTraceback (most recent call last):|n  File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 59, in t
estPartExecutor|n    yield|n  File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 605, in run|n    testMethod()|n  File "C:\testSiteDemoTests\site_tests.py", line 7, in test|n    self.assertEqual(|'a|', |'b|')|n' e
xpected='a' locationHint='python<C:\testSiteDemoTests>://site_tests.SiteTests.test' message='|nb != a|n' name='test' nodeId='3' parentNodeId='2' type='comparisonFailure']
##teamcity[testFinished timestamp='2018-05-20T23:59:59.977' duration='30' locationHint='python<C:\testSiteDemoTests>://site_tests.SiteTests.test' name='test' nodeId='3' parentNodeId='2']


Ran 1 test in 0.031s

FAILED (failures=1)
##teamcity[testSuiteFinished timestamp='2018-05-20T23:59:59.977' locationHint='python<C:\testSiteDemoTests>://site_tests.SiteTests' name='SiteTests' nodeId='2' parentNodeId='1']
##teamcity[testSuiteFinished timestamp='2018-05-20T23:59:59.977' locationHint='python<C:\testSiteDemoTests>://site_tests' name='site_tests' nodeId='1' parentNodeId='0']

【问题讨论】:

    标签: python pycharm python-unittest


    【解决方案1】:

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

    如果在异常处理程序或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
    

    【讨论】:

    • 有解决办法吗?
    • @mr.bjerre 补丁 _patched_equals 按照 stackoverflow.com/questions/30235516/…
    • 不确定当我通过 Pycharm IDE 使用鼻子测试运行测试时是否有那么简单。
    • @mr.bjerre 好吧,go and check it out, do your homework。我写了你需要做什么来逐步通过测试机器来为自己找到正在发生的事情。如果在nosetests 的情况下,除了我在答案中引用的其他代码在起作用,那么这个问题甚至不适用。
    • @ivan_pozdev 好吧,我有完全相同的回溯,所以是的,它确实适用!我只是没有打补丁的经验。
    猜你喜欢
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2018-05-09
    • 2018-12-03
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多