Malik Benkirane
Posted on May 7, 2018
I need to build some tests with python. This can usually be done coding a
module with many asserts or doing everything manually aka typing some
python -i totest.py
then playing around with the code to see if there are case where the code is
broken. But you would agree that it's better to design well the tests for your
code in order to automate this process for example. This process has already
been discussed since very long time ago and there is very few things left
behind this. Moreover, this process has also been extended to things so called
continuous integration, code coverage or you may have heard of builds that
pass. That's right, you can always put tests in the build process. Pythonistas
could rather use nose2
.
Installation
You could use any environment (virtual, user, global) to install nose2
pip[3] install [--user] --upgrade nose2
For Python 2.7 and pypy, nose2 requires six version 1.1.
How this works ?
To find tests,
nose2
looks for modules whose names start withtest
. In
those modules,nose2
will load tests from allunittest.TestCase
subclasses, as well as functions whose names start withtest
.
Basically you need to write the python tests and execute nose2
to take care
of your testing jobs. And
nose2
will look in each directory under the starting directory, unless the
configuration modifies the included paths. Within directories and within any
Python packages found in the starting directory and any source directories in
the starting directory,nose2
will discover test modules and load tests from
them.
just
nose2
great right ?
Naming Tests
nose2
looks in
- directories that contains
__init__.py
- direcotries that the lowercased name contains
test
- directories the name is either
src
orlib
and (without configuration)
-
nose2
will run all python files (.py
extension) that the name starts withtest
- within test modules,
nose2
loadsunittest.TestCase
subclasses
What's next ?
- go to python
unittest
documentation (here python3, here python 2) - go to
nose2
(rtd here) - you will figure it out
Greetings,
Posted on May 7, 2018
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.