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



Django 1.9.我想测试输入表单是否正常不可见。仅当用户按下切换菜单按钮时,才会显示登录名和密码输入元素。

问题是这都是关于类的方法。我想确保一种方法先于另一个方法执行。我发现这个解决方案在方法名称中带有 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

最新更新