Python:测试的约定名称

时间:2015-11-15 13:57:50

标签: python python-unittest

使用unittest模块时是否存在在Python中命名测试的约定。我知道继承自unittest.TestCase的类中的每个方法都应该从test开始,但我想知道更好的是什么:

1。没有docstring的简短描述性名称

def test_for_a_date_on_a_weekday(self):
        customer_type = "Regular"
        dates = ["16Mar2009(mon)"]
    self.assertEquals(self.hotel_reservation_system.find_cheapest_hotel(customer_type, dates), "Lakewood")

2。单词test之后的数字,用docstring解释测试。

def test_1(self):
    """Tests the cheapest hotel when the date is on a weekday.
    """
    customer_type = "Regular"
    dates = ["16Mar2009(mon)"]
    self.assertEquals(self.hotel_reservation_system.find_cheapest_hotel(customer_type, dates), "Lakewood")

哪个选项更受欢迎,如果有任何选项我应该使用什么?

1 个答案:

答案 0 :(得分:1)

Generally it is preferable to increase readability by :
    - choosing an adequate name     
    - describing how it works

选择您的姓名,使其简短且具有描述性。为了便于阅读,请使用snake_case。例如:test_week_date。

始终在函数中包含docstring。如果名称不够清楚,或者如果他没有真正理解该方法的作用/方式,那么读者可以获得所有必要的信息。

结论:带有文档字符串的简短描述性(snake_case)名称。