Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Python unittests be in a separate module?

Is there a consensus about the best place to put Python unittests?

Should the unittests be included within the same module as the functionality being tested (executed when the module is run on its own (if __name__ == '__main__', etc.)), or is it better to include the unittests within different modules?

Perhaps a combination of both approaches is best, including module level tests within each module and adding higher level tests which test functionality included in more than one module as separate modules (perhaps in a /test subdirectory?).

I assume that test discovery is more straightforward if the tests are included in separate modules, but there's an additional burden on the developer if he/she has to remember to update the additional test module if the module under test is modified.

I'd be interested to know peoples' thoughts on the best way of organizing unittests.

like image 553
Jon Mills Avatar asked Aug 27 '09 12:08

Jon Mills


People also ask

Should unit tests be in a separate file?

We have a requirement that the unit testing file need to be separate with the source file in project building. It means we must not do this for testing the source file.

Should unit tests be in the same package?

Unit tests can be in any package. In essence they are just separate classes used to test the behaviour of the class being tested.

Should tests be included in Python package?

Testing is an important part of Python package development but one that is often neglected due to the perceived additional workload.


2 Answers

YES, do use a separate module.

It does not really make sense to use the __main__ trick. Just assume that you have several files in your module, and it does not work anymore, because you don't want to run each source file separately when testing your module.

Also, when installing a module, most of the time you don't want to install the tests. Your end-user does not care about tests, only the developers should care.

No, really. Put your tests in tests/, your doc in doc, and have a Makefile ready around for a make test. Any other approaches are just intermediate solutions, only valid for specific tiny modules.

like image 134
Nicolas Dumazet Avatar answered Oct 18 '22 18:10

Nicolas Dumazet


  1. Where you have to if using a library specifying where unittests should live,
  2. in the modules themselves for small projects, or
  3. in a tests/ subdirectory in your package for larger projects.

It's a matter of what works best for the project you're creating.

Sometimes the libraries you're using determine where tests should go, as is the case with Django (where you put your tests in models.py, tests.py or a tests/ subdirectory in your apps).

If there are no existing constraints, it's a matter of personal preference. For a small set of modules, it may be more convenient to put the unittests in the files you're creating.

For anything more than a few modules I create the tests separately in a tests/ directory in the package. Having testing code mixed with the implementation adds unnecessary noise for anyone reading the code.

like image 32
jds Avatar answered Oct 18 '22 17:10

jds