PEP8测试类命名约定

2024-04-19 10:10:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在寻找PEP 8 -- Style Guide for Python CodePEP8 -- Advanced Usage来获得如何命名测试类的线索。但是,在这两个站点以及我所看到的许多其他站点(如Python文档中的unittest页面)中,从来没有提到过这一点。我看到的唯一一致的风格是“大写字母”。在unittest文档中,有TestSequenceFunctions和DefaultWidgetSizeTestCase的示例。

我想知道是用“Name”测试还是用“Name”测试。方法使用test_uu“name”,这已经基本确定了。关于课程,我正在努力寻找一个惯例,如果有的话。

感谢论坛在这方面的帮助。


Tags: name文档for站点styleusagecode页面
1条回答
网友
1楼 · 发布于 2024-04-19 10:10:56

Django和SQLAlchemy这两个流行的大型Python项目都使用“Name”测试。就我个人而言,我更喜欢测试“Name”,主要是因为当一个文件中有多个测试用例时,每个用例都以“Test”开头会使扫描变得困难。

不是说这等于一个共识,只有两个重要的数据点和个人观察。

网友
2楼 · 发布于 2024-04-19 10:10:56

documentation for ^{}建议,例如:

class TestSequenceFunctions(unittest.TestCase):

    def test_shuffle(self):
        ...

    def test_choice(self):
        ...

评论

The three individual tests are defined with methods whose names start with the letters test. This naming convention informs the test runner about which methods represent tests.

相关问题 更多 >