Python:测试的约定名称

标签 python python-unittest

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

<强>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。单词 test 后面的数字以及解释测试的文档字符串。

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")

哪个选项更可取,如果有的话我应该使用什么?

最佳答案

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

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

始终在函数中包含文档字符串。如果名称不够清晰或者读者并不真正理解该方法的作用/她是如何做到的,这将使读者能够获得所有必要的信息。

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

关于Python:测试的约定名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33720522/

相关文章:

python - 单击 python CLI.testing TypeError

python - ubuntu 与 windows 中通过 python 的屏幕截图

python - Bitnami Odoo Stack 上的 Pycups

python - 嵌套字典

python - 如何修补 ConfigParser 键/值?

python - 是否可以使用 Pytest 运行 Robot Framework 单元测试?

python - 在 Django 模板中按索引引用列表项?

python - 这个语法有替代品吗?

python - 压缩标准库时如何运行pycharm单元测试?

python - 测试 postgres 数据库 python