I found another solution, based on how the unittest.skip decorator works. By setting the __unittest_skip__ and __unittest_skip_why__.
Label-based
I wanted to apply a labeling system, to label some tests as quick, slow, glacier, memoryhog, cpuhog, core, and so on.
Then run all 'quick' tests, or run everything except 'memoryhog' tests, your basic whitelist / blacklist setup
Implementation
I implemented this in two parts:
- First add labels to tests (via a custom
@testlabel class decorator)
- Custom
unittest.TestRunner to identify which tests to skip, and modify the testlist content before executing.
Working implementation is in this gist:
https://gist.github.com/fragmuffin/a245f59bdcd457936c3b51aa2ebb3f6c
(A fully working example was too long to put here.)
The result being...
$ ./runtests.py --blacklist foo
test_foo (test_things.MyTest2) ... ok
test_bar (test_things.MyTest3) ... ok
test_one (test_things.MyTests1) ... skipped 'label exclusion'
test_two (test_things.MyTests1) ... skipped 'label exclusion'
----------------------------------------------------------------------
Ran 4 tests in 0.000s
OK (skipped=2)
All MyTests1 class tests are skipped, because it has the foo label.
--whitelist also works