简体   繁体   中英

run unittests from a command prompt

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM