nose1.3.7文档翻译--2.Basic usage

Basic usage

使用nosetests脚本

nosetests [options] [(optional) test files or directories]

除了传递命令行参数,你也可以通过在家目录中编写.noserc或nose.cfg配置文件配置选项。该配置文件是标准.ini-style风格配置文件。把你的配置写在[nosetests]section中,并把-前缀去掉:

[nosetests]
verbosity=3
with-doctest=1

也有不使能配置加载的文件的情况(might be useful when runnig i.e. tox and you don’t want your global nose config file to be used by tox。ps:不知道怎么翻译)。为了忽略配置文件,可以设置NOSE_IGNORE_CONFIG_FILES环境变量。
除了nosetests脚本,也有其他方式使用nose测试运行器。可以在脚本中这样使用nose:

import nose
nose.main()

如果你不想要测试脚本exit,并且退出标识符0表示成功1表示失败(像unittest.main一样),可以使用nose.run()代替:

import nose
result = nose.run()

如果测试运行成功,result将为true,任意一条测试失败或者抛出了未被捕捉的异常,result将为false。最新的版本,你可以直接运行nose.core,让将运行nose.main():

python /path/to/nose/core.py

请查看nosetests脚本使用信息来获得如何控制nose运行的信息。

Extended usage

nose可以从工作目录(默认为当前工作目录)中的python源文件,目录和包中自动收集测试例。任何匹配testMatch正则表达式(默认为 ?:\b|_)的python源文件,目录和包,测试例将会被收集作为一个测试(或者叫做测试集合的源)。另外,在工作目录中的所有其他packages也会被测试是否匹配testMatch。包查找会便利所有的树型结构,因此package.tests,package.sub.tests,package.sub.sub2.tests也会被收集。
在一个测试目录或者包中,任何匹配testMatch的python源文件将会被作为case测试。在一个测试module中,命名方式匹配testMatch的函数和类,任意名字但是继承于TestCase的类,都将会被加载运行。测试可能会使用assert关键字或者抛出AssertErrors来表示测试失败。TestCase的子类可能也会与此相同,也可能使用TestCase提供的方式表示测试失败
标注nose的行为也是重要的,它表示在文件中哪些用例不会被执行。想要从这些文件中包含测试例,移除他们的可执行标志,或者使用exe标志(查看下文中的Options)。

Selecting Tests

想要明确哪些用例将会被执行,可以在命令行中传递测试名:

nosetests only_test_this.py

测试names明确文件或模块,也可能随意地明确一个模块或文件中将会被执行的case,,case name带有冒号。文件可以使绝对或者相对路径。比如:

nosetests test.module
nosetests another.test:TestCase.test_method
nosetests a.test:TestCase
nosetests /path/to/test/file.py:test_function

你或许也会改变nose查找测试例的工作目录,通过使用-w即可:

nosetests -w /path/to/tests

更深层次的测试选择和加载的定制可以通过plugins来实现。
测试结果的输出与unittest相同,除非使用下文介绍的额外特性(error class,插件提供的比如输出捕捉和assert反省的特性)。

Configuration

除了传递命令行参数,你也可以通过在家目录中编写.noserc或nose.cfg配置文件配置选项。该配置文件是标准.ini-style风格配置文件。把你的配置写在[nosetests]section中,Options与命令行中相同,但是需要把-前缀去掉,被提供的参数必须有值:

[nosetests]
verbosity=3
with-doctest=1

所有被发现的配置文件都会被加载,然后options合并。你可以通过-c 覆盖加载的标准配置文件。

Using Plugins

有许多可以通过easy_install或者其他方式安装的nose plugins可以获得。想要使用它们,安装即可。plugin可以增加到nosetests的命令行参数中。想要查看已经安装的plugin,运行:

nosetests --plugins

你可以增加-v或者-vv参数来显示每个plugin的详细信息。
如果你在脚本中使用nose.main()或nose.run(),你可以通过函数中plugins关键字参数传递一系列plugins。

0.9 plugins

nose 1.0可以使用许多为nose 0.9编写的plugins,默认的插件管理器插入一个兼容0.9的装饰器,装饰器被用来适配被改变的api调用。然而,访问nose内部的plugins可能会失败,尤其是它们试图访问测试case或者suite类。例如,试图决定一个私人的测试例或suite是否被传递到startTest,将会失败,部分原因是suites不再可以被传递到startTest中,还有一部分是plugins试图查出是否一个实例是类的实例也不再被支持。

0.10 and 0.11 plugins

所有的为nose 0.10和0.11编写的插件均可以作用于nose1.0中。

Options

参数,不想翻译了。。。。。。
-V, –version
Output nose version and exit

-p, –plugins
Output list of available plugins and exit. Combine with higher verbosity for greater detail

-v=DEFAULT, –verbose=DEFAULT
Be more verbose. [NOSE_VERBOSE]

–verbosity=VERBOSITY
Set verbosity; –verbosity=2 is the same as -v

-q=DEFAULT, –quiet=DEFAULT
Be less verbose

-c=FILES, –config=FILES
Load configuration from config file(s). May be specified multiple times; in that case, all config files will be loaded and combined

-w=WHERE, –where=WHERE
Look for tests in this directory. May be specified multiple times. The first directory passed will be used as the working directory, in place of the current working directory, which is the default. Others will be added to the list of tests to execute. [NOSE_WHERE]

–py3where=PY3WHERE
Look for tests in this directory under Python 3.x. Functions the same as ‘where’, but only applies if running under Python 3.x or above. Note that, if present under 3.x, this option completely replaces any directories specified with ‘where’, so the ‘where’ option becomes ineffective. [NOSE_PY3WHERE]

-m=REGEX, –match=REGEX, –testmatch=REGEX
Files, directories, function names, and class names that match this regular expression are considered tests. Default: (?:\b|_)[Tt]est [NOSE_TESTMATCH]

–tests=NAMES
Run these tests (comma-separated list). This argument is useful mainly from configuration files; on the command line, just pass the tests to run as additional arguments with no switch.

-l=DEFAULT, –debug=DEFAULT
Activate debug logging for one or more systems. Available debug loggers: nose, nose.importer, nose.inspector, nose.plugins, nose.result and nose.selector. Separate multiple names with a comma.

–debug-log=FILE
Log debug messages to this file (default: sys.stderr)

–logging-config=FILE, –log-config=FILE
Load logging config from this file – bypasses all other logging config settings.

-I=REGEX, –ignore-files=REGEX
Completely ignore any file that matches this regular expression. Takes precedence over any other settings or plugins. Specifying this option will replace the default setting. Specify this option multiple times to add more regular expressions [NOSE_IGNORE_FILES]

-e=REGEX, –exclude=REGEX
Don’t run tests that match regular expression [NOSE_EXCLUDE]

-i=REGEX, –include=REGEX
This regular expression will be applied to files, directories, function names, and class names for a chance to include additional tests that do not match TESTMATCH. Specify this option multiple times to add more regular expressions [NOSE_INCLUDE]

-x, –stop
Stop running tests after the first error or failure

-P, –no-path-adjustment
Don’t make any changes to sys.path when loading tests [NOSE_NOPATH]

–exe
Look for tests in python modules that are executable. Normal behavior is to exclude executable modules, since they may not be import-safe [NOSE_INCLUDE_EXE]

–noexe
DO NOT look for tests in python modules that are executable. (The default on the windows platform is to do so.)

–traverse-namespace
Traverse through all path entries of a namespace package

–first-package-wins, –first-pkg-wins, –1st-pkg-wins
nose’s importer will normally evict a package from sys.modules if it sees a package with the same name in a different location. Set this option to disable that behavior.

–no-byte-compile
Prevent nose from byte-compiling the source into .pyc files while nose is scanning for and running tests.

-a=ATTR, –attr=ATTR
Run only tests that have attributes specified by ATTR [NOSE_ATTR]

-A=EXPR, –eval-attr=EXPR
Run only tests for whose attributes the Python expression EXPR evaluates to True [NOSE_EVAL_ATTR]

-s, –nocapture
Don’t capture stdout (any stdout output will be printed immediately) [NOSE_NOCAPTURE]

–nologcapture
Disable logging capture plugin. Logging configuration will be left intact. [NOSE_NOLOGCAPTURE]

–logging-format=FORMAT
Specify custom format to print statements. Uses the same format as used by standard logging handlers. [NOSE_LOGFORMAT]

–logging-datefmt=FORMAT
Specify custom date/time format to print statements. Uses the same format as used by standard logging handlers. [NOSE_LOGDATEFMT]

–logging-filter=FILTER
Specify which statements to filter in/out. By default, everything is captured. If the output is too verbose, use this option to filter out needless output. Example: filter=foo will capture statements issued ONLY to foo or foo.what.ever.sub but not foobar or other logger. Specify multiple loggers with comma: filter=foo,bar,baz. If any logger name is prefixed with a minus, eg filter=-foo, it will be excluded rather than included. Default: exclude logging messages from nose itself (-nose). [NOSE_LOGFILTER]

–logging-clear-handlers
Clear all other logging handlers

–logging-level=DEFAULT
Set the log level to capture

–with-coverage
Enable plugin Coverage: Activate a coverage report using Ned Batchelder’s coverage module. [NOSE_WITH_COVERAGE]

–cover-package=PACKAGE
Restrict coverage output to selected packages [NOSE_COVER_PACKAGE]

–cover-erase
Erase previously collected coverage statistics before run

–cover-tests
Include test modules in coverage report [NOSE_COVER_TESTS]

–cover-min-percentage=DEFAULT
Minimum percentage of coverage for tests to pass [NOSE_COVER_MIN_PERCENTAGE]

–cover-inclusive
Include all python files under working directory in coverage report. Useful for discovering holes in test coverage if not all files are imported by the test suite. [NOSE_COVER_INCLUSIVE]

–cover-html
Produce HTML coverage information

–cover-html-dir=DIR
Produce HTML coverage information in dir

–cover-branches
Include branch coverage in coverage report [NOSE_COVER_BRANCHES]

–cover-xml
Produce XML coverage information

–cover-xml-file=FILE
Produce XML coverage information in file

–cover-config-file=DEFAULT
Location of coverage config file [NOSE_COVER_CONFIG_FILE]

–cover-no-print
Suppress printing of coverage information

–pdb
Drop into debugger on failures or errors

–pdb-failures
Drop into debugger on failures

–pdb-errors
Drop into debugger on errors

–no-deprecated
Disable special handling of DeprecatedTest exceptions.

–with-doctest
Enable plugin Doctest: Activate doctest plugin to find and run doctests in non-test modules. [NOSE_WITH_DOCTEST]

–doctest-tests
Also look for doctests in test modules. Note that classes, methods and functions should have either doctests or non-doctest tests, not both. [NOSE_DOCTEST_TESTS]

–doctest-extension=EXT
Also look for doctests in files with this extension [NOSE_DOCTEST_EXTENSION]

–doctest-result-variable=VAR
Change the variable name set to the result of the last interpreter command from the default ‘’. Can be used to avoid conflicts with the () function used for text translation. [NOSE_DOCTEST_RESULT_VAR]

–doctest-fixtures=SUFFIX
Find fixtures for a doctest file in module with this name appended to the base name of the doctest file

–doctest-options=OPTIONS
Specify options to pass to doctest. Eg. ‘+ELLIPSIS,+NORMALIZE_WHITESPACE’

–with-isolation
Enable plugin IsolationPlugin: Activate the isolation plugin to isolate changes to external modules to a single test module or package. The isolation plugin resets the contents of sys.modules after each test module or package runs to its state before the test. PLEASE NOTE that this plugin should not be used with the coverage plugin, or in any other case where module reloading may produce undesirable side-effects. [NOSE_WITH_ISOLATION]

-d, –detailed-errors, –failure-detail
Add detail to error output by attempting to evaluate failed asserts [NOSE_DETAILED_ERRORS]

–with-profile
Enable plugin Profile: Use this plugin to run tests using the hotshot profiler. [NOSE_WITH_PROFILE]

–profile-sort=SORT
Set sort order for profiler output

–profile-stats-file=FILE
Profiler stats file; default is a new temp file on each run

–profile-restrict=RESTRICT
Restrict profiler output. See help for pstats.Stats for details

–no-skip
Disable special handling of SkipTest exceptions.

–with-id
Enable plugin TestId: Activate to add a test id (like #1) to each test name output. Activate with –failed to rerun failing tests only. [NOSE_WITH_ID]

–id-file=FILE
Store test ids found in test runs in this file. Default is the file .noseids in the working directory.

–failed
Run the tests that failed in the last test run.

–processes=NUM
Spread test run among this many processes. Set a number equal to the number of processors or cores in your machine for best results. Pass a negative number to have the number of processes automatically set to the number of cores. Passing 0 means to disable parallel testing. Default is 0 unless NOSE_PROCESSES is set. [NOSE_PROCESSES]

–process-timeout=SECONDS
Set timeout for return of results from each test runner process. Default is 10. [NOSE_PROCESS_TIMEOUT]

–process-restartworker
If set, will restart each worker process once their tests are done, this helps control memory leaks from killing the system. [NOSE_PROCESS_RESTARTWORKER]

–with-xunit
Enable plugin Xunit: This plugin provides test results in the standard XUnit XML format. [NOSE_WITH_XUNIT]

–xunit-file=FILE
Path to xml file to store the xunit report in. Default is nosetests.xml in the working directory [NOSE_XUNIT_FILE]

–xunit-testsuite-name=PACKAGE
Name of the testsuite in the xunit xml, generated by plugin. Default test suite name is nosetests.

–xunit-prefix-with-testsuite-name
Whether to prefix the class name under test with testsuite name. Defaults to false.

–all-modules
Enable plugin AllModules: Collect tests from all python modules. [NOSE_ALL_MODULES]

–collect-only
Enable collect-only: Collect and output test names only, don’t run any tests. [COLLECT_ONLY]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值