Disabling Python nosetests

2019-01-22 17:20发布

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.

5条回答
闹够了就滚
2楼-- · 2019-01-22 17:26

You can also use unittest.skip decorator:

import unittest


@unittest.skip("temporarily disabled")
class MyTestCase(unittest.TestCase):
    ...
查看更多
狗以群分
3楼-- · 2019-01-22 17:46

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)
查看更多
够拽才男人
4楼-- · 2019-01-22 17:47

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.

查看更多
做自己的国王
5楼-- · 2019-01-22 17:48

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

查看更多
聊天终结者
6楼-- · 2019-01-22 17:49

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
查看更多
登录 后发表回答