使用 nose2
命名测试
nose2将在起始目录下的每个目录中查找,除非配置修改了包含的路径。在目录中以及在起始目录中找到的任何python包和起始目录中的任何源目录中,nose2将发现测试模块并从中加载测试。”“测试模块”指名称以“测试”开头的任何模块。有关修改搜索测试的方法,请参阅配置部分。
目录nose2将查找:
包含一个
__init__.py
文件(python包)包含“test”的目录名。
目录名是
lib
或src
将运行以下每个测试文件::
test.py
test_views.py
test_models.py
testThingy.py
这些文件将不会运行::
not_a_test.py
myapp_test.py
some_test_file.py
在测试模块中,nose2将从 unittest.TestCase
子类,以及来自测试函数(名称以“test”开头的函数)。
运行试验
在最简单的情况下,转到包含项目源的目录并运行 nose2
那里:
nose2
这将在该目录下的包和测试目录中发现测试,加载并运行它们,然后输出如下内容:
.............................................................................
----------------------------------------------------------------------
Ran 77 tests in 1.897s
OK
若要更改发现开始的位置,或更改项目的顶级可导入目录,请使用 -s
和 -t
选项。
- -s START_DIR, --start-dir START_DIR
开始发现的目录。默认为当前工作目录。这个目录是nose2开始查找测试的地方。
- -t TOP_LEVEL_DIRECTORY, --top-level-directory TOP_LEVEL_DIRECTORY, --project-directory TOP_LEVEL_DIRECTORY
项目的顶级目录。默认为起始目录。这是一个包含可导入模块和包的目录,并且总是在
sys.path
在测试发现开始之前。
指定要运行的测试
通过 测试名称 在命令行上nose2以运行单个测试模块、类或测试。
测试名称由 python对象部分 对于发生器或参数化测试,一个 参数部分 . 这个 python对象部分 是虚线名称,例如 pkg1.tests.test_things.SomeTests.test_ok
. 参数部分用冒号(“:”)与python对象部分分隔,并指定 指数 要选择的生成测试, 从1开始 . 例如, pkg1.test.test_things.test_params_func:1
将选择 第一 从参数化测试生成的测试 test_params_func
.
插件可以提供其他的测试选择方法。
使用运行测试 python setup.py test
nose2支持分发/设置工具' python setup.py test
运行测试的标准。要使用nose2运行包的测试,请将以下内容添加到安装程序.py::
setup(...
test_suite='nose2.collector.collector',
...
)
(不是字面意思。不要把“…”部分放进去。)
以这种方式运行测试的两个警告。
第一:因为SETUPTOOLS测试命令是有限的,所以nose2返回一个“测试套件”,它实际上完全接管测试运行过程,绕过测试结果和调用它的测试运行程序。这可能与某些包不兼容。
第二:由于测试命令的命令行参数可能与nose2的参数不匹配,所以由收集器启动的nose2实例 不接受任何命令行参数 . 这意味着它总是运行所有测试,并且当以这种方式运行测试时,您不能在命令行上配置插件。作为解决方法,在测试命令下运行时,nose2将从 setup.cfg
如果有,除了 unittest.cfg
和 nose2.cfg
. 这使您能够将特定于SETUPTOOLS测试命令的配置放入 setup.cfg
--例如,激活插件,否则将通过命令行激活插件。
得到帮助
运行:
nose2 -h
为nose2本身和所有加载的插件获取帮助。
usage: nose2 [-s START_DIR] [-t TOP_LEVEL_DIRECTORY] [--config [CONFIG]]
[--no-user-config] [--no-plugins] [--verbose] [--quiet] [-B] [-D]
[--collect-only] [--log-capture] [-P] [-h]
[testNames [testNames ...]]
positional arguments:
testNames
optional arguments:
-s START_DIR, --start-dir START_DIR
Directory to start discovery ('.' default)
-t TOP_LEVEL_DIRECTORY, --top-level-directory TOP_LEVEL_DIRECTORY, --project-directory TOP_LEVEL_DIRECTORY
Top level directory of project (defaults to start dir)
--config [CONFIG], -c [CONFIG]
Config files to load, if they exist. ('unittest.cfg'
and 'nose2.cfg' in start directory default)
--no-user-config Do not load user config files
--no-plugins Do not load any plugins. Warning: nose2 does not do
anything if no plugins are loaded
--verbose, -v
--quiet
-h, --help Show this help message and exit
plugin arguments:
Command-line arguments added by plugins:
-B, --output-buffer Enable output buffer
-D, --debugger Enter pdb on test fail or error
--collect-only Collect but do not run tests. With '-v', this will output test names
--log-capture Enable log capture
-P, --print-hooks Print names of hooks in order of execution