[Tutor] Best way to setup Unit Testing?

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Jul 10 23:13:56 CEST 2013


On 10 July 2013 15:37, Albert-Jan Roskam <fomcl at yahoo.com> wrote:
>>
>> On 7 July 2013 21:16, 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.
>>
>> Are you the only one using these applications or are you intending
>> that other people would install them? Do you want other people to run
>> the tests on their machines when they install?
>
> Shouldn't this always be possible?

That depends if the tests are installed along with the software. It's
not uncommon for the development code base to contain tests that are
not shipped to end users. In a typical PyPI package you would use the
setup.py file to specify which files from the development code-base
are included in the source distribution (e.g. 'python setup.py
sdist'). The question about how best to organise the tests is affected
by this; if you want them in the target users site-packages directory
as a subpackage of your MyApp package then that's probably where you
should put them in the development code-base.

[snip]
>>
>> You can always just do that in a script. I would normally have a
>> Makefile and invoke tests with
>>
>> $ make test
>
> Isn't this specific for unix-like systems?

You can install make on non-unix systems. I use make on Windows. It's
not typically installed on Windows but neither is Python.

Again if you anticipate that lots of different people are going to be
working with this code using different operating systems and that
requiring everyone to have make installed is unreasonable then you may
feel that e.g. a Python script is better than a Makefile. On the other
hand if the code is only intended for a particular set of computers or
you're the only person who will ever run those tests then just use
whatever you want.

I have a number of projects that are not only very specific to my own
work (and not really useful to anyone else) but specific to the
particular machines on which they run (so not useful anywhere else).
Since I'm the only one who will ever run those tests I don't need to
worry about people who don't have make. In any case if you don't have
make you can still run the tests since if you look in the Makefile you
can find the exact commands that are invoked by 'make test' anyway.

Make is a very useful tool for code-base housekeeping. For example, if
I have an extension module that needs to be built from C code then my
Makefile can have something like:

test: myextension.pyd
    python -m MyApp.tests.run_all

myextension.pyd: myextension.c myextension.h
    python setup.py build_ext --inplace

Then 'make test' will rebuild the extension module if necessary
(because I modified the C code) and not otherwise. This means I don't
need to constantly keep track of whether I remembered to recompile
after editing the C code. Otherwise when I'm not sure I have to
recompile and rerun the tests (which obviously wastes time).


Oscar


More information about the Tutor mailing list