【问题标题】:Best practice for ensuring test methods order确保测试方法顺序的最佳实践
【发布时间】:2016-05-22 17:58:57
【问题描述】:

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-testing


    【解决方案1】:

    最佳实践是,您的测试方法的顺序根本不重要,如果您从 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
    

    【讨论】:

      猜你喜欢
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      • 2011-06-29
      • 2020-02-05
      • 2021-04-06
      相关资源
      最近更新 更多