PEP8 naming convention on test classes

2020-02-27 06:37发布

问题:

I have been looking at PEP 8 -- Style Guide for Python Code and PEP8 -- Advanced Usage for clues on how to name my test classes. However, this is never mentioned on both sites, as well as many other sites I have looked at, such as the unittest page in the Python documentation. The only consistent style I see is "CapWords". In the unittest documentation they have examples for TestSequenceFunctions as well as DefaultWidgetSizeTestCase.

What I am trying to find out is whether to use "Name"Test or Test"Name". Methods use test_"name" and that's pretty much established. With regards to classes I am struggling to find a convention if there's one.

Would appreciate the forum's help on this.

回答1:

The documentation for unittest suggests, e.g.:

class TestSequenceFunctions(unittest.TestCase):

    def test_shuffle(self):
        ...

    def test_choice(self):
        ...

commenting

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.



回答2:

Django and SQLAlchemy, two large, popular Python projects, both use "Name"Test(s). Personally, I prefer that to Test"Name", mainly because when there are multiple TestCases in a file, having each start with "Test" makes scanning difficult.

Not saying this equals a consensus, only two significant data points and a personal observation.