[Tutor] Best way to setup Unit Testing?

Albert-Jan Roskam fomcl at yahoo.com
Wed Jul 10 10:32:14 CEST 2013


_______________________________
>From: Srinivas Nyayapati <shireenrao at gmail.com>
>To: tutor at python.org 
>Sent: Sunday, July 7, 2013 10:16 PM
>Subject: [Tutor] Best way to setup Unit Testing?
>
>
>
>I am tryng to figure out the best way to do Unit Testing for all my projects going forward. I am using the unittest framework. For execution I am executing the test cases by either directly using python or by using the nose test runner. I found the following 3 types of how this can be setup.
>
>
>Type 1
>======
>
>
>The unit tests are located in the same package as the module being tested. This is the easiest to implement and run. I don't have to do anything special to run the tests. The directory structure is like this
>
>
>Projects/
>    MyApp/
>        __init__.py
>        myapp.py
>        test_myapp.py
>
>
>Simply run the test from within the MyApp folder as
>
>
>% python test_myapp.py
>or use the nose test runner
>
>
>% nosetests
>
>
>Type 2
>======
>
>
>Here the unit tests are located in its own tests folder in the package you want to test. Running the unit tests from inside the tests folder won't work as it worked in Type 1. That's because the myapp module can not be resolved. Here is how the directory structure looks like
>
>
>Projects/
>    MyApp/
>        __init__.py
>        myapp.py
>        tests/
>            __init__.py
>            test_myapp.py
>It will work if you ran it from inside the package folder and gave the complete path to the test file as
>
>
>% python tests/test_myapp.py
>It will also work if you used the nose test runner from inside MyApp.
>
>
>% nosetests
>It will also work from inside the tests folder if you added MyApp to your PYTHONPATH, but that is an option I dont like. I don't want to keep changing my PYTHONPATH.
>
>
>Type 3
>======
>
>
>This is where the tests folder is on the same level as the package folder. You can not run the test scripts from within the tests folder as again it will not be able to resolve the myapp package. Here is how the directory structure looks like
>
>
>Projects/
>    MyApp/
>        __init__.py
>        myapp.py
>    tests/
>        __init__.py
>        test_myapp.py
>
>
>You can run the tests from within the MyApp package by referencing the tests folder as
>
>
>% python ../tests/test_myapp.py
>
>
>As you are running the test from the package, the myapp module is automatically in your PYTHONPATH. The other way of doing this would be by putting your package in your PYTHONPATH, which again as I mentioned earlier I don't like to do.
>
>
>What is the best way of doing this? I would really appreciate to know how the experts are doing this. Is there anything I need to consider before I start using test suites. 

Hi, sorry, I am not an expert but I can tell you how I did it. I would certainly put the tests in a separate folder. I used a  [nosetests] section in setup.cfg to tell nose (IIRC using where=) where the tests live (with a relative path).


More information about the Tutor mailing list