When using nosetests for Python it is possible to disable a unit test by setting the test function's __test__
attribute to false. I have implemented this using the following decorator:
def unit_test_disabled():
def wrapper(func):
func.__test__ = False
return func
return wrapper
@unit_test_disabled
def test_my_sample_test()
#code here ...
However, this has the side effect of calling wrapper as the unit test. Wrapper will always pass but it is included in nosetests output. Is there another way of structuring the decorator so that the test will not run and does not appear in nosetests output.
You can also use
unittest.skip
decorator:There also is a skiptest plugin for nosetest, which will cause the test show in test output as skipped. Here is a decorator for that:
Example output:
You can just start the class, method or function name with an underscore and nose will ignore it.
@nottest
has its uses but I find that it does not work well when classes derive from one another and some base classes must be ignored by nose. This happens often when I have a series of similar Django views to test. They often share characteristics that need testing. For instance, they are accessible only to users with certain permissions. Rather than write the same permission check for all of them, I put such shared test in an initial class from which the other classes derive. The problem though is that the base class is there only to be derived by the later classes and is not meant to be run on its own. Here's an example of the problem:And the output from running nose on it:
The
Base
class is included in the tests.I cannot just slap
@nottest
onBase
because it will mark the entire hierarchy. Indeed if you just add@nottest
to the code above in front ofclass Base
, then nose won't run any tests.What I do is add an underscore in front of the base class:
And when running it
_Base
is ignored:This behavior is not well documented but the code that selects tests explicitly checks for an underscore at the start of class names.
A similar test is performed by nose on function and method names so it is possible to exclude them by adding an underscore at the start of the name.
Nose already has a builtin decorator for this:
Also check out the other goodies that nose provides: https://nose.readthedocs.org/en/latest/testing_tools.html
I think you will also need to rename your decorator to something that has not got test in. The below only fails on the second test for me and the first does not show up in the test suite.