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

2024-05-29 03:11:13 发布

您现在位置:Python中文网/ 问答频道 /正文

但我不知道如何在python命令提示符下运行所有的测试。在

文件结构

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

请告诉我同样的情况。 当我试着用鼻子测试的时候,它说我

^{pr2}$

Tags: 文件pytestinit情况tests结构file1
2条回答

对于简单的发现,您可以使用unittest模块的内置命令行功能

$ cd /path/to/your/project/tests
$ python -m unittest discover

或者,如果您的tests目录有一个__init__.py,您可以这样做

^{pr2}$

如果您绝对必须使用python2.6,python docs表示可以安装unittest2,这是python2.7中添加的新功能的一个后端口。你调用它的方式稍有不同

$ unit2 discover

另外,如果您还没有深入到unittest测试兔子洞,我建议您去看看^{}。它在编写测试时需要的样板文件要少得多,并且允许您只使用内置的assert。它还提供了一些其他很酷的特性,比如fixtures参数化测试函数,这些特性也允许您减少单元测试中的代码量。在

下面是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

相关问题 更多 >

    热门问题