简体   繁体   中英

Appropriate file hierarchy for unittesting in Python

I am developing a collection of Python packages/modules (no executables). What is the correct/best way to set up the file hierarchy for testing. I can think of two scenarios:

Scenario 1:

AllPackages/
    package1/
        module1-1.py
        module1-2.py
    package2/
        module2-1.py
        module2-2.py
    tests/
        package1/
            test_module1-1.py
            test_module1-2.py
        package2/
            test_module2-1.py
            test_module2-2.py

Scenario 2:

AllPackages/
    package1/
        module1-1.py
        module1-2.py
        tests/
            test_module1-1.py
            test_module1-2.py
    package2/
        module2-1.py
        module2-2.py
        tests/
            test_module2-1.py
            test_module2-2.py

I am new to unittesting (I know I should have done it long ago) so I'm not sure which of these approaches is better and I'm looking for some advice from those which have more experience.

Thanks!

Scenario 1 is better, in my opinion. It makes things easier when you deploy, for example. You don't want to deploy test code, so you just omit the tests/ directory in your package. This approach is much cleaner.

Scenario 2 is messy; I don't see any advantage of mixing test code and production code in this way.

The 2nd scenario allows you to have pluggable packages and is used at least in Django framework (to mention some authority). If you use plain unittest module, it has discover utility, which will find all the tests you have in your project folder no matter how you organized them, so the 2nd approach fits here too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM