Disabling Python nosetests

前端 未结 5 1254
南方客
南方客 2020-12-08 18:42

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 usin

相关标签:
5条回答
  • 2020-12-08 18:56

    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

    0 讨论(0)
  • 2020-12-08 18:57

    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
    
    0 讨论(0)
  • 2020-12-08 19:01

    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.

    0 讨论(0)
  • 2020-12-08 19:04

    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)
    
    0 讨论(0)
  • 2020-12-08 19:08

    You can also use unittest.skip decorator:

    import unittest
    
    
    @unittest.skip("temporarily disabled")
    class MyTestCase(unittest.TestCase):
        ...
    
    0 讨论(0)
提交回复
热议问题