Search code examples
pythonnosetests

Filtering nosetests by numeric range


All of my nosetests are of the form:

def test_555():    chk(555); do_some_testing(...)
def test_556():    chk(556); some_more_testing()

The chk() function will check to see if that test number is one that I am currently wanting to execute -- this is my own ignore-test logic based on numeric ranges. this function simply raises: unittest.SkipTest() to skip a test.

I am happy with this setup, but I would like to somehow auto decorate ALL of my tests in a way that I automatically derive the 'chk(i)' check for each test. I cannot see how to obtain the currently executing test object, during the test itself, nor how to somehow auto decorate all tests.

but maybe there is a better way? My goal to be able to update :

RUN_TESTS = (590,720)

in my source code, and have the correct thing happen

thanks!


Solution

  • nose-parametarized is your friend:

    from nose_parameterized import parameterized
    
    def chk(number):
        print number
    
    @parameterized.expand([[x, x] for x in range(100)])
    def test(_, number):
        chk(number)
    

    Running tests:

    $ nosetests pr.py
    pr.test_0 ... ok
    pr.test_1 ... ok
    ...
    pr.test_98 ... ok
    pr.test_99 ... ok
    ----------------------------------------------------------------------
    Ran 100 tests in 0.078s
    
    OK
    

    Selecting tests:

    $ nosetests pr.py:test_89 -v -s
    pr.test_89 ... 89
    ok
    
    ----------------------------------------------------------------------
    Ran 1 test in 0.002s
    
    OK