python - 测试工具 ConcurrentStreamTestSuite 的一个简单的工作示例

标签 python unit-testing testing

我正在寻找一个演示如何使用 testtool's 的简单示例并发测试。我找到了一个例子,来自 here :

import unittest
import testtools

class MyTester(unittest.TestCase):
    # Tests...

suite = unittest.TestLoader().loadTestsFromTestCase(MyTester)
concurrent_suite = testtools.ConcurrentStreamTestSuite(lambda: ((case, None) for case in suite))
concurrent_suite.run(testtools.StreamResult()) 

但是,它有一个相当大的问题。它不会告诉您测试是通过还是失败。不仅 StreamResult 对象是临时的,而且 StreamResult 的方法也不做任何事情。

最佳答案

根据StreamResult documentation ,调用status方法来处理事件,默认实现什么都不做。

要让它打印一些东西,你需要继承它来做你想做的事。例如,要打印正在发生的事情:

import unittest
import testtools

class MyTester(unittest.TestCase):
    def test_foo(self):
        self.assertEqual(1+2, 2)
    def test_foo2(self):
        self.assertEqual(1+2, 3)

class TracingStreamResult(testtools.StreamResult):
    def status(self, *args, **kwargs):
        print('{0[test_id]}: {0[test_status]}'.format(kwargs))

suite = unittest.TestLoader().loadTestsFromTestCase(MyTester)
concurrent_suite = testtools.ConcurrentStreamTestSuite(lambda: ((case, None) for case in suite))
result = TracingStreamResult()
result.startTestRun()
concurrent_suite.run(result)
result.stopTestRun()

但是,已经有多个类继承了 StreamResult。所以您可能不需要定义自己的版本。参见 Extensions to TestResult .

关于python - 测试工具 ConcurrentStreamTestSuite 的一个简单的工作示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22484805/

相关文章:

Python导入模块依赖

java - OutputStream 的 JUnit 测试

javascript - 如何在捕获 Promise 时使 Node 单元测试失败?

java - 我可以使用 TestFX 和 JUnit 测试舞台按钮或警报对话框吗?

html - rspec 测试失败,但应该通过

需要 Java JUnit 测试指南

python - Emacs python 模式 : Keyboard shortcuts for pdb step-by-step debugging

python - 加速 Pandas 数据框迭代

python - IP 地址格式的语法错误

javascript - "clock"指令的 Angular 单元测试 $interval