[Tutor] Best way to setup Unit Testing?

Chris “Kwpolska” Warrick kwpolska at gmail.com
Wed Jul 10 17:20:48 CEST 2013


On Sun, Jul 7, 2013 at 10:16 PM, Srinivas Nyayapati
<shireenrao at gmail.com> wrote:
> 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

It makes a mess and puts your code and tests together, which you should not do.

> 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.

So don’t use this ugly thing and use:

> 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

Are you sure?  Won’t that require
% cd ..
% python tests/test_myapp.py

or other magic?  Packages *may* not work otherwise.

--
Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16
stop html mail                | always bottom-post
http://asciiribbon.org        | http://caliburn.nl/topposting.html


More information about the Tutor mailing list