Time for action – decorating tests

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 we will install nose in case you don't have it yet.

  1. Install nose with setuptools
    easy_install nose

    Or pip:

    pip install nose
  2. We will apply one function as being a test and another as not being a test.
    @setastest(False)
    def test_false():
      pass
    
    @setastest(True)
    def test_true():
      pass
  3. We can skip tests with the skipif decorator. Let's use a condition that always leads to a test being skipped.
    @skipif(True)
    def test_skip():
      pass
  4. Add a test function that always passes. Then decorate it with the knownfailureif decorator so that the test always fails.
    @knownfailureif(True)
    def test_alwaysfail():
        pass
  5. We will define some 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
  6. Let's disable the second test method from the previous step.
    decorate_methods(TestClass2, setastest(False), 'test_false2')
  7. We can run the tests with the following command:
    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
    raiseKnownFailureTest(msg)
    KnownFailureTest: Test skipped due to known failure
    
    ----------------------------------------------------------------------
    Ran 4 tests in 0.001s
    
    FAILED (SKIP=1, errors=1)

What just happened?

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')
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset