Unittest tests order

后端 未结 19 1281
刺人心
刺人心 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 03:58

    There are scenarios where the order can be important and where setUp and Teardown come in as to limited. There's only one setUp and tearDown method, which is logical but you can only put so much information in them until it get's unclear what setUp or tearDown might actually be doing.

    Take this integration test as an example:

    You are writing tests to see if the registration form and the login form are working correctly. In such a case the order is important, as you can't login without an existing account. More importantly the order of your tests represents some kind of user interaction. Where each test might represent a step in the whole process or flow you're testing.

    Dividing your code in those logical pieces has several advantages.

    Might not be the best solution but, I often use one method that kicks off the actual tests.

    def test_registration_login_flow(self):
        _test_registration_flow()
        _test_login_flow()
    

提交回复
热议问题