Selectively run tests

Thing you want to do

If you want to run only a subset of the tests, for example, you want to skip the time-consuming tests and run them only if you specify. One way is to divide it into files so that they can be distinguished by file name, and the unittest module's discovery option.

% python -m unittest --help
      ...
  -p pattern       Pattern to match test files ('test*.py' default)

Can be achieved using, but here

An example of how to deal with such cases is shown.

How to do

Use the skipIf, skipUnless decorators in the unittest module. For example

util.py


import unittest

run_slowtest = False


def slowtest(target):
    return unittest.skipUnless(run_slowtest, "Slow Test")(target)

Define a decorator function like this and give it to class or method:

test_sample.py


import unittest

from util import slowtest


@slowtest
class TestA(unittest.TestCase):
    def test_a(self):
        pass


class TestB(unittest.TestCase):
    @slowtest
    def test_b(self):
        pass

If run normally, the target test will be skipped:

% python -m unittest discover .
ss
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK (skipped=2)

If you want to do it all, do the following:

% cat runner_full.py
import unittest

import util

util.run_slowtest = True
unittest.TestProgram(argv=['', 'discover'])
% python runner_full.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

Recommended Posts

Selectively run tests
Run tests automatically when saving file
Run unit tests the moment you save the file