Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A simple, working example for testtools ConcurrentStreamTestSuite

I'm looking for a simple example demonstrating how to use testtool's concurrent tests. I've found one example, from 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()) 

However, it suffers from a rather large problem. It doesn't tell you if your tests passed or failed. Not only is the StreamResult object temporary, StreamResult's methods don't do anything.

like image 545
razeh Avatar asked Mar 18 '14 16:03

razeh


2 Answers

According ot the StreamResult documentation, the status method is called to handle event, and default implementation does nothing.

To make it print something you need to inherit it to do what you want. For example, to print what's going on:

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()

But, there are already multiple classes that inherit the StreamResult. So you maybe not needed to define your own version. See Extensions to TestResult.

like image 123
falsetru Avatar answered Sep 28 '22 23:09

falsetru


Have a look at concurrencytest library which already implemented testtools classes

like image 37
Dejell Avatar answered Sep 28 '22 22:09

Dejell