Skip to main content
 首页 » 编程设计

python之从命令提示符运行单元测试

2025年05月04日15mq0036

我已经创建了 python 文件和单元测试,但我不明白如何从命令提示符一次性运行所有测试。

文件结构

- py 
  - __init__.py 
  - file1.py 
  - file2.py 
- tests 
  __init__.py 
  - test_file1.py 
  - test_file2.py 

请告诉我相同的信息。 当我尝试使用 nosetests 运行测试时,它说我

no module name file1 
no module name file2 

请您参考如下方法:

下面是 Nose 文档的一个片段(因为你已经标记了 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.

强调我的。

您可以简单地从项目的根目录运行所有测试用例:

$ cd /path/to/your/project 
$ nosetests