Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PEP8 naming convention on test classes

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.

like image 459
musabaloyi Avatar asked Dec 11 '13 08:12

musabaloyi


People also ask

Which is the proper naming convention of a test case?

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.

How do you name a test in Python?

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.

How do I name a file in Python pep8?

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.


Video Answer


2 Answers

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.

like image 169
jonrsharpe Avatar answered Sep 18 '22 14:09

jonrsharpe


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.

like image 42
Chris Lawlor Avatar answered Sep 19 '22 14:09

Chris Lawlor