Unittest tests order

后端 未结 19 1278
刺人心
刺人心 2020-12-01 03:17

How do I be sure of the unittest methods order? Is the alphabetical or numeric prefixes the proper way?

class TestFoo(TestCase):
    def test_1(self):
               


        
19条回答
  •  眼角桃花
    2020-12-01 04:05

    If you use 'nose' and you write your test cases as functions (and not as methods of some TestCase derived class) 'nose' doesn't fiddle with the order, but uses the order of the functions as defined in the file. In order to have the assert_* methods handy without needing to subclass TestCase I usually use the testing module from numpy. Example:

    from numpy.testing import *
    
    def test_aaa():
        assert_equal(1, 1)
    
    def test_zzz():
        assert_equal(1, 1)
    
    def test_bbb():
        assert_equal(1, 1)
    

    Running that with ''nosetest -vv'' gives:

    test_it.test_aaa ... ok
    test_it.test_zzz ... ok
    test_it.test_bbb ... ok
    ----------------------------------------------------------------------
    Ran 3 tests in 0.050s
    OK
    

    Note to all those who contend that unit tests shouldn't be ordered: while it is true that unit tests should be isolated and can run independently, your functions and classes are usually not independent. They rather build up on another from simpler/low-level functions to more complex/high-level functions. When you start optimising your low-level functions and mess up (for my part, I do that frequently; if you don't, you probably don't need unit test anyway;-) then it's a lot better for diagnosing the cause, when the tests for simple functions come first, and tests for functions that depend on those functions later. If the tests are sorted alphabetically the real cause usually gets drowned among one hundred failed assertions, which are not there because the function under test has a bug, but because the low-level function it relies on has.

    That's why I want to have my unit tests sorted the way I specified them: not to use state that was built up in early tests in later tests, but as a very helpful tool in diagnosing problems.

提交回复
热议问题