Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the optimal naming convention for test files in Python?

I am looking for an optimal naming convention for python test files that ease the usage of different test frameworks (unittest, note, pyunit, ...) and also that is friendly with test auto-discovery for these tools.

I just want a clear set of recomandation that would require minimal configuration for tools.

  • Tests directory name? test or tests?
  • Where to put the tests? in module/testdir or module/../testdir
  • Test filenames using underscore or dash? test_0.py or test-0.py

I know, I have too much time :)

like image 750
bogdan Avatar asked Dec 01 '11 15:12

bogdan


People also ask

How do you name a test file 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.

What is the naming convention for Python files?

modules (filenames) should have short, all-lowercase names, and they can contain underscores; packages (directories) should have short, all-lowercase names, preferably without underscores; classes should use the CapWords convention.

Which is the proper naming convention of a test case?

Test case names are usually limited to a specific length, so space is at a premium. Make sure to keep names unique while refraining from adding any details that aren't required for identification. You can add those details to the case's instructions or test data, for example.


2 Answers

Don't call the directory test or it will conflict with the built-in test package.

The naming conventions are defined in PEP 8. See the 'Naming Conventions' section. Underscores are better than hyphens!

The layout of your package is a bit more flexible. I tend to do the following:

package |-- package |  |-- __init__.py |  `-- <etc> |-- tests |  `-- <etc> |-- setup.py |-- README |-- LICENCE `-- <etc> 

This keeps the tests separate from the package itself. Installing the package using setup.py can install just the source, which keeps people's interpreters tidy. The tests are there for developers that need them when they get the package source.

You should look at The Hitch Hiker's Guide to Packaging for more info on Python packages.

like image 74
adamnfish Avatar answered Sep 25 '22 09:09

adamnfish


It will depends of the tool you're using to run your tests.

If you're using nosetest, the philosophy used to detect test is pretty simple :

If it looks like a test, it’s a test.

If you're using py.test, the conventions are pretty open too.

About the "where to put test" question, personnaly, I prefer to store tests in a subdirectory in each package to be sure to not forgot to run/touch the tests when someone edit the code ;)

like image 25
Cédric Julien Avatar answered Sep 24 '22 09:09

Cédric Julien