Disabling Python nosetests
Solution 1
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.
def unit_disabled(func):
def wrapper(func):
func.__test__ = False
return func
return wrapper
@unit_disabled
def test_my_sample_test():
assert 1 <> 1
def test2_my_sample_test():
assert 1 <> 1
Solution 2
Nose already has a builtin decorator for this:
from nose.tools import nottest
@nottest
def test_my_sample_test()
#code here ...
Also check out the other goodies that nose provides: https://nose.readthedocs.org/en/latest/testing_tools.html
Solution 3
You can also use unittest.skip
decorator:
import unittest
@unittest.skip("temporarily disabled")
class MyTestCase(unittest.TestCase):
...
Solution 4
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:
def skipped(func):
from nose.plugins.skip import SkipTest
def _():
raise SkipTest("Test %s is skipped" % func.__name__)
_.__name__ = func.__name__
return _
Example output:
$ nosetests tests
..........................................................................
..................................S.............
----------------------------------------------------------------------
Ran 122 tests in 2.160s
OK (SKIP=1)
Solution 5
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:
from unittest import TestCase
class Base(TestCase):
def test_something(self):
print "Testing something in " + self.__class__.__name__
class Derived(Base):
def test_something_else(self):
print "Testing something else in " + self.__class__.__name__
And the output from running nose on it:
$ nosetests test.py -s
Testing something in Base
.Testing something in Derived
.Testing something else in Derived
.
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
The Base
class is included in the tests.
I cannot just slap @nottest
on Base
because it will mark the entire hierarchy. Indeed if you just add @nottest
to the code above in front of class Base
, then nose won't run any tests.
What I do is add an underscore in front of the base class:
from unittest import TestCase
class _Base(TestCase):
def test_something(self):
print "Testing something in " + self.__class__.__name__
class Derived(_Base):
def test_something_else(self):
print "Testing something else in " + self.__class__.__name__
And when running it _Base
is ignored:
$ nosetests test3.py -s
Testing something in Derived
.Testing something else in Derived
.
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
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.
Comments
-
Richard Dorman almost 2 years
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.
-
ire_and_curses almost 11 yearsOr you can just
raise SkipTest
at the start of your test. Note also that you can dofrom nose import SkipTest
. -
Serge over 9 yearsTest will be just not run and not mark as skipped... Sometimes would be more useful to raise SkipTest exception, as was suggested in @Anders Waldenborg answer, so would be able to see in results that some tests were skipped.
-
Chris Huang-Leaver about 9 yearsWhy not delete this post and earn your 'peer pressure' badge?
-
Zitrax about 9 yearsNote the documentation for this: "Usually you can use TestCase.skipTest() or one of the skipping decorators instead of raising this directly.".
-
gaborous almost 9 yearsWorks perfectly well with nose too, and also on functions. Great! See pybites.blogspot.fr/2009/03/…
-
CaffeineConnoisseur over 8 yearsWhy is this so heavily downvoted? The reasoning is unclear.
-
Martin Thorsen Ranang over 8 yearsThe down-vote reasoning is based on a combination of two things: (1) the answer should be something like "don't implement your own decorator when reading the docs (or source code) would have shown you that it's already available" and (2) still this answer got accepted. This answer could have been helpful if it only read: avoid using
test
in the decorator name. -
CivFan almost 8 yearsThis also works great with parameterized tests using the
@parameterized.expand()
decorator fromnode_parameterized
... as long as you put@unittest.skip()
below the other -- ie. so that the unittest decorator runs before the parameterized decorator. -
meawoppl almost 8 yearsThis is a poorly reinvented wheel.
-
Rubens over 6 yearsThis is graceful, as one may inherit from a
@nottest
class, and can still mark the descendant as@istest
. I believe I've been all over the place looking for how to achieve this behavior, and this is the only one that works without tearing the code apart. (Nesting the descendant class to avoid nose's tracker makes the whole thing look terrible :/ ) -
Veltzer Doron over 5 yearsI don't think you can delete an answer once someone already gave it comments and a vote
-
NotTooTechy over 4 yearsI usually use @unittest.skipIf(True, 'SkippingTests message'), it gives some flexibility, chance to make conditional skip.
-
NotTooTechy over 4 yearsraise SkipTest, is good way to dynamically decide if the test is running or skipping. Ugly part, if you are using Jenkins,is writing to junit.xml, and it writes with general tags.
-
user7440787 almost 3 yearsmaybe stackoverflow needs a better way to list two valid solutions and order them in based on the number of up votes. The one question one solution model is not ideal in many aspects of the software development. That's treating software development like maths which is not.