Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 1000
jlconlin Avatar asked Nov 05 '22 05:11

jlconlin


2 Answers

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.

like image 57
Misha Akovantsev Avatar answered Nov 07 '22 22:11

Misha Akovantsev


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.

like image 43
larsbutler Avatar answered Nov 07 '22 20:11

larsbutler