确保测试方法顺序的最佳实践

Best practice for ensuring test methods order

Django 1.9。我想测试输入表单是否正常不可见。并且只有当用户按下切换菜单按钮时,才会出现登录和密码输入元素。

问题是这都是关于 class 的方法。我想确保一种方法先于另一种方法执行。我在方法名称中找到了带有 0 和 1 的解决方案。

class FuncTestTablets(TestCase):

    @classmethod
    def setUpClass(cls):
        pass

    @classmethod
    def tearDownClass(cls):
        pass

    def test_0_tablets_login_input_form_absent(self):
        # At home page with tablet screen size Edith sees no login input element.
        ## 0 is for stating explicitly that this test goes before the ones with 1 in their name. 
        self.browser.get('http://localhost:8000')
        login_input = self.browser.find_element_by_id('login')        
        self.assertFalse(login_input.is_displayed(), "Login input element is visible on large devices")

    def test_1_tablets_login_input_form_present_when_menu_button_pressed(self):
        # At home page Edith presses menu button and login input appears.
        ## 1 is for stating explicitly that this test goes after the ones with 0 in their name.
        menu_button = self.browser.find_element_by_class_name('navbar-toggle')
        menu_button.click()
        login_input = self.browser.find_element_by_id('login')
        self.assertTrue(login_input.is_displayed(), "Login input element is not visible on tablet devices when menu button is pressed.")

这似乎有效。你能告诉我对于这种情况是否有一些众所周知的方法。也许一些最佳实践。

我刚刚开始使用 Django,但我认为我的解决方案并不是最好的。

这就是我决定问你的原因。提前谢谢你。

最佳做法是测试方法的顺序根本无关紧要,如果您继承自 django.test.TestCase,则应该无关紧要。然而,我曾经遇到过这样的情况,我想在一种方法中测试一个特性 'foo',然后为了方便(比如不必将它包装在 try-except 中),假设它工作正常在其他测试其他东西的方法中('bar')。

我命名了我的测试方法

test_a_foo
test_b_bar

这似乎比使用数字更合适,因为测试是按字典顺序执行的,例如

test_11_eleven

先于

执行
test_2_two

如果以后必须插入另一个测试,您仍然可以将名称更改为:

test_b1_first_b
test_b2_second_b