Should Python unittests be in a separate module?

后端 未结 7 1996
攒了一身酷
攒了一身酷 2021-02-12 16:44

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 wh

7条回答
  •  再見小時候
    2021-02-12 17:19

    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.

提交回复
热议问题