Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Python's unittest test in parallel, like nose can?

People also ask

Does Python unittest run tests in parallel?

unittest-parallel is a parallel unit test runner for Python with coverage support. By default, unittest-parallel runs unit tests on all CPU cores available. To run your unit tests with coverage, add either the "--coverage" option (for line coverage) or the "--coverage-branch" for line and branch coverage.

Does MSTest run tests in parallel?

The biggest advantage of MSTest is that it allows parallelization at the method level. As opposed to the other frameworks which only allow parallelization at the class level. So, for example, If you have 100 test methods in 5 classes, MSTest will let you run 100 tests in parallel.

Do xUnit tests run in parallel?

Running unit tests in parallel is a new feature in xUnit.net version 2. There are two essential motivations that drove us to not only enable parallelization, but also for it to be a feature that's enabled by default: As unit testing has become more prevalent, so too have the number of unit tests.


Python unittest's builtin testrunner does not run tests in parallel. It probably wouldn't be too hard write one that did. I've written my own just to reformat the output and time each test. That took maybe 1/2 a day. I think you can swap out the TestSuite class that is used with a derived one that uses multiprocess without much trouble.


The testtools package is an extension of unittest which supports running tests concurrently. It can be used with your old test classes that inherit unittest.TestCase.

For example:

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

Please use pytest-xdist, if you want parallel run.

The pytest-xdist plugin extends py.test with some unique test execution modes:

  • test run parallelization: if you have multiple CPUs or hosts you can use those for a combined test run. This allows to speed up development or to use special resources of remote machines.

[...]

More info: Rohan Dunham's blog


If you only need Python3 suport, consider using my fastunit.

I just change few code of unittest, making test case run as coroutines.

It really saved my time.

I just finished it last week, and may not testing enough, if any error happens, please let me know, so that I can make it better, thanks!


Another option that might be easier, if you don't have that many test cases and they are not dependent, is to kick off each test case manually in a separate process.

For instance, open up a couple tmux sessions and then kick off a test case in each session using something like:

python -m unittest -v MyTestModule.MyTestClass.test_n

If this is what you did initially

runner = unittest.TextTestRunner()
runner.run(suite)

-----------------------------------------

replace it with

from concurrencytest import ConcurrentTestSuite, fork_for_tests

concurrent_suite = ConcurrentTestSuite(suite, fork_for_tests(4))
runner.run(concurrent_suite)