Reputation: 15084
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!
Upvotes: 6
Views: 416
Reputation: 1825
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.
Upvotes: 2
Reputation: 533
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.
Upvotes: 1