We will apply the @setastest
decorator directly to test functions. Then we will apply the same decorator to a method to disable it. Also, we will skip one of the tests and fail another. First, install nose
in case you don't have it yet.
nose
with setuptools
:$ [sudo] easy_install nose
Or pip:
$ [sudo] pip install nose
@setastest(False) def test_false(): pass @setastest(True) def test_true(): pass
@skipif
decorator. Let's use a condition that always leads to a test being skipped:@skipif(True) def test_skip(): pass
@knownfailureif
decorator so that the test always fails:@knownfailureif(True) def test_alwaysfail(): pass
test
classes with methods that normally should be executed by nose
:class TestClass(): def test_true2(self): pass class TestClass2(): def test_false2(self): pass
decorate_methods(TestClass2, setastest(False), 'test_false2')
$ nosetests -v decorator_setastest.py decorator_setastest.TestClass.test_true2 ... ok decorator_setastest.test_true ... ok decorator_test.test_skip ... SKIP: Skipping test: test_skipTest skipped due to test condition decorator_test.test_alwaysfail ... ERROR ====================================================================== ERROR: decorator_test.test_alwaysfail ---------------------------------------------------------------------- Traceback (most recent call last): File "…/nose/case.py", line 197, in runTest self.test(*self.arg) File …/numpy/testing/decorators.py", line 213, in knownfailer raise KnownFailureTest(msg) KnownFailureTest: Test skipped due to known failure ---------------------------------------------------------------------- Ran 4 tests in 0.001s FAILED (SKIP=1, errors=1)
We decorated some functions and methods as not being tests so that they were ignored by nose. We skipped one test and failed another too. We did this by applying decorators directly and with the decorate_methods()
function (see decorator_test.py
):
from numpy.testing.decorators import setastest from numpy.testing.decorators import skipif from numpy.testing.decorators import knownfailureif from numpy.testing import decorate_methods @setastest(False) def test_false(): pass @setastest(True) def test_true(): pass @skipif(True) def test_skip(): pass @knownfailureif(True) def test_alwaysfail(): pass class TestClass(): def test_true2(self): pass class TestClass2(): def test_false2(self): pass decorate_methods(TestClass2, setastest(False), 'test_false2')