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.
Test class naming convention usually follows the naming convention of the class being tested, e.g., the class under test is “Order” the corresponding test class will be “OrderTests“. When in doubt, it is a good practice to model the test class name after the production class it is testing.
Unit tests are usually written as a separate code in a different file, and there could be different naming conventions that you could follow. You could either write the name of the unit test file as the name of the code/unit + test separated by an underscore or test + name of the code/unit separated by an underscore.
If you're going to follow PEP 8, you should stick to all-lowercase names, with optional underscores. To quote PEP 8's naming conventions for packages & modules: Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With