简体   繁体   English

QT function 抑制 python 中的测试失败

[英]QT function suppressing test failures in python

Working on a complex unittest that is testing a python UI, and a QT function appears to be suppressing test failures.处理测试 python UI 和 QT function 的复杂单元测试似乎正在抑制测试失败。 I believe I've been able to create a minimal file that repeats the behavior:我相信我已经能够创建一个重复该行为的最小文件:

import pytest
from unittest import TestCase
from PySide2.QtCore import QTimer

def meaningless():
    return 'fire'


class TestClass(TestCase):

    def test_1(self):
        def inner_test_1():
            self.assertEqual(meaningless(),'x')
        
        inner_test_1()

    def test_2(self):
        def inner_test_2():
            self.assertEqual(meaningless(),'x')

        QTimer.singleShot(1, inner_test_2)


if __name__ == '__main__':
    import sys

    sys.exit(pytest.main([__file__]))

The first test fails as it should, but the second passes erroneously.第一次测试失败了,但第二次错误地通过了。 In my more complex real-world unit test, the equivalent of "inner_test_2" does indeed fire, and the resulting assertion error can be seen in the test logs, but the test does not register as a failure.在我更复杂的真实世界单元测试中,“inner_test_2”的等效项确实会触发,并且可以在测试日志中看到由此产生的断言错误,但测试不会注册为失败。 From what I can tell in the QT docs, this may have something to do with multi-threading?从我在 QT 文档中可以看出,这可能与多线程有关? How do I get failures to fail?我如何让失败失败?

This may have something to do with multi-threading?这可能与多线程有关?

Yes, exactly.对,就是这样。 Failing tests are detected via exceptions and exceptions in a different thread are not caught by the unittest framework, because it can not associate a thread with a test.失败的测试是通过异常检测的,并且不同线程中的异常不会被 unittest 框架捕获,因为它无法将线程与测试相关联。

A minimal, reproducible example would be:一个最小的、可重现的例子是:

import pytest
from unittest import TestCase
import threading

class TestClass(TestCase):
    def test_2(self):
        def inner_test_2():
            self.assertTrue(False)
        myTestThread = threading.Thread(target=inner_test_2)
        myTestThread.start()
        myTestThread.join()

if __name__ == '__main__':
    import sys

    sys.exit(pytest.main([__file__]))

Note that while assertTrue(False) raises an exception we do not see a failing test.请注意,虽然assertTrue(False)引发了异常,但我们没有看到失败的测试。 But what we do see (besides the stacktrace of the exception) is a warning from pytest :但是我们确实看到(除了异常的堆栈跟踪)是来自pytest的警告:

    warnings.warn(pytest.PytestUnhandledThreadExceptionWarning(msg))

-- Docs: https://docs.pytest.org/en/stable/warnings.html

How do I get failures to fail?我如何让失败失败?

You need to catch the exceptions from the thread and raise them in the main thread so the unittest framework can pick them up and understand what test failed.您需要从线程中捕获异常并在主线程中引发它们,以便 unittest 框架可以拾取它们并了解哪些测试失败了。 How to do that is a different topic, see Make Python unittest fail on exception from any thread .如何做到这一点是另一个主题,请参阅Make Python unittest fail on exception from any thread

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM