在使用unittest
模块时是否存在Python命名测试的约定。我都知道,从unittest.TestCase
继承的类内的每个方法应与试验开始,但我不知道什么是更好的:Python:测试的约定名称
1简短的描述名称没有文档字符串
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。数字跟在单词测试之后,用解释测试的文档字符串。
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")
哪个选项是非常理想的,如果其中任何一个我应该怎么用?
总是选择描述性的。您的测试是一种提供文档形式的方式,以提供有关代码如何工作以及测试内容的上下文。它使它更具可读性。 – idjaw
因此,这意味着选项二,或者我应该混合一个文档字符串,即一个简短的描述性名称? – lmiguelvargasf
您也不一定需要在每种测试方法中都有一个文档字符串。你的方法的名称应该是描述性的,但简洁。然而,这绝对是一种意见,我相信每一方都有争论。 :) – idjaw