File naming conventions for unittesting files [closed]
Asked Answered
P

2

8

For python unit tests, is the following a common naming convention?

MyProject/
    run_ingest.py
    tests/
        run_ingest.py

Or is this too redundant? If so, what would be a better naming convention or directory structure to put testing code?

Polenta answered 21/2, 2019 at 1:17 Comment(0)
G
6

Having a directory called tests is correct but, as per my experience, the test scripts themselves are usually prefixed with test_ so in your case test_run_ingest.py. Make sure to use underscore rather than - as a separator in the names to avoid import problems. As for the structure, you'd also probably want to include __init__.py files both to the top level and /tests folder to simplify imports.

To extend your example:

MyProject/
    __init__.py
    run_ingest.py
    tests/
        __init__.py
        test_run_ingest.py

If you are new to unittest this blog might be of interest.

Gaudery answered 21/2, 2019 at 1:21 Comment(1)
I have a weird bug. I have several files called test_routes_nameofroute.py but I wanted to make a new test suite called test_jobs_database.py and for some dumb reason I get an entire segfault and the tests will not run. It's only once I rename to test_routes_database.py that it works. What gives??Verso
C
0

To add to the currently top but unaccepted answer, the python.org documentation for the unittest module indicates that the default filename pattern matcher for the module's test discovery feature is "test*.py".

A basic normal way to run unittest tests to automatically 'discover' test files with that filename pattern matcher is with python -m unittest discover.

Normal usage for running tests with the module includes specifying other pattern matcher strings as input arguments. Specifying a different pattern matcher looks like python -m unittest discover -p 'unusual_test_filenames*.py.

Here is a link to the unittest module documentation about test discovery on python.org about unittest #test discovery for reference.

Coleen answered 24/2 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.