Reputation: 19204
I have created python files and unittests but I do not understand how can i run all the tests in one go from command prompt.
file structure
- py
- __init__.py
- file1.py
- file2.py
- tests
__init__.py
- test_file1.py
- test_file2.py
Please let me know the same. when i try to run tests using nosetests, it says me
no module name file1
no module name file2
Upvotes: 1
Views: 3740
Reputation: 37569
For simple discovery, you can use the built-in command-line functionality of the unittest
module
$ cd /path/to/your/project/tests
$ python -m unittest discover
Or, if your tests
directory has an __init__.py
, you can do
$ cd /path/to/your/project
$ python -m unittest discover -s ./tests
If you absolutely must use python 2.6, the python docs say you can install unittest2
, which is a backport of the new features added in python 2.7. You invoke it slightly differently
$ unit2 discover
Also, if you haven't already gone too far down the unittest
testing rabbit hole, I would recommend checking out pytest
. It requires a lot less boilerplate when writing tests and allows you to just use the built-in assert
. It also comes with some other cool features like fixtures and parameterized test functions that also allow you to reduce the amount of code in your unittests.
Upvotes: 3
Reputation: 44464
Below is a snip from Nose's documentation (since you've tagged nose
)
nose collects tests automatically from python source files, directories and packages found in its working directory (which defaults to the current working directory). Any python source file, directory or package that matches the testMatch regular expression (by default:
(?:\b|_)[Tt]est
will be collected as a test (or source for collection of tests). In addition, all other packages found in the working directory will be examined for python source files or directories that match testMatch. Package discovery descends all the way down the tree, so package.tests and package.sub.tests and package.sub.sub2.tests will all be collected.
Emphasis mine.
You can simply run all the test cases from the root directory of your project:
$ cd /path/to/your/project
$ nosetests
Upvotes: 3