欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Writing tests

程序员文章站 2024-02-27 15:11:03
...

1.1写测试case

正如py.test一样。nose不需要继承unittest.TestCase。任何匹配testMatch规则表达式函数或者类,以及模块中匹配testMatch表达式的都会被当作测试来运行。为了兼容unittest测试cases,nose也会加载继承unittest.TestCase的测试。像py.test,nose按它们出现在模块文件中的顺序依次来运行函数测试。TestCase派生出来的测试以及其他的类测试按照字母进行排序执行。

1.2 固定方法

nos支持固定的方式(setup和teardown方法)在包、模块、类、测试等级。和py.test或者unittest的固定方法一样,setup会在所有测试case之前运行(或者包和模块的测试集合);

teardown会在所有的测试成功执行完后执行,不管测试是否成功。更多的关于每个级别的固定方法的信息,看下面

1.3 Test包

nose允许将test集合成测试包。会有package级别的setup;例如,如果你需要创建一个数据库为你的测试,在任何一个case运行之 前,你可能会在package setup中创建,然后再package teardown中删除,而不是创建和删除它在每个测试模块或者测试case。

为了创建package级别的setup和teardown方法,定义test package的setup 、teardown函数在__init__.py 中。

  • setup函数可能被命名为setup、setup_package\setUp, 或者 setUpPackage;

  • teardown函数可能被命名为teardown\teardown_package\tearDown 或者 tearDownPackage;

一旦第一个测试模块被加载从test package,test package的所有case的执行就开始了。

1.4 Test模块

测试模块是匹配testMatch规则表达式的python模块。Test modules提供了module-level的setup和teardown;

定义方法:

setup\setup_module\setUp\setUpModule

teardown\teardown_module\tearDownModule

所有的测试集合之后,一个test module的所有tests就开始执行了。

1.5 Test类

测试类是被定义到一个测试模块(该模块匹配testMatch或者是继承了unittest.TestCase的子类)。所有的测试类都以相同的方式运行:这个类的匹配testMatch的方法会被自动失败,

a test case is constructed to run each method with a fresh instance of the test class。像继承自unittest.TestCase 的子类,其他的测试类能定义setUp和tearDown方法(这些方法会在每个测试方法之前或者之后运行)

Test classes that do not descend fromunittest.TestCase可能包括generator方法以及class-level fixture。

Class-level setup可能被命名为setup_class\setupClass\setUpClass\setupAll\setUpAll;

teardown可能被命名为teardown_class\teardownClass\tearDownClass\teardownAll\tearDownAll;

Class-level setup 和teardown必须是类方法。

1.6 Test函数

在测试模块的所有匹配testMatch的方法被会包装成FunctionTestCase 然后作为一个test运行。最简单的可能失败的test如下:

def test():
    assert False

以及最简单的pass test

def test():
    pass

测试函数可能定义了setup、teardown。最这些的比较方便的方法是,特别是当在同一个模块的几个测试函数都需要相同的setup,可以使用提供的with_setup,如下:

def setup_func():
    "set up test fixtures"

def teardown_func():
    "tear down test fixtures"

@with_setup(setup_func, teardown_func)
def test():
    "test ..."

对于python2.3以及更早版本,添加属性通过调用decorator函数,如下:

def test():
    "test ... "

test = with_setup(setup_func, teardown_func)(test)

或者直接的分配

test.setup = setup_func
test.teardown = teardown_func

注意with_setup仅仅对测试函数有用,对继承自unittest.TestCase的子类或者其他测试类的测试方法无效。

对于这些cases,需要定义setUp以及tearDown方法在类中。

Writing tests

As with py.test, nose tests need not be subclasses ofunittest.TestCase. Any function or class that matches the configured testMatch regular expression ((?:^|[\\b_\\.-])[Tt]est by default – that is, has test or Test at a word boundary or following a - or _) and lives in a module that also matches that expression will be run as a test. For the sake of compatibility with legacy unittest test cases, nose will also load tests from unittest.TestCase subclasses just like unittest does. Like py.test, nose runs functional tests in the order in which they appear in the module file. TestCase-derived tests and other test classes are run in alphabetical order.

Fixtures

nose supports fixtures (setup and teardown methods) at the package, module, class, and test level. As with py.test or unittest fixtures, setup always runs before any test (or collection of tests for test packages and modules); teardown runs if setup has completed successfully, regardless of the status of the test run. For more detail on fixtures at each level, see below.

Test packages

nose allows tests to be grouped into test packages. This allows package-level setup; for instance, if you need to create a test database or other data fixture for your tests, you may create it in package setup and remove it in package teardown once per test run, rather than having to create and tear it down once per test module or test case.

To create package-level setup and teardown methods, define setup and/or teardown functions in the __init__.py of a test package. Setup methods may be namedsetup,setup_package,setUp, orsetUpPackage; teardown may be namedteardown,teardown_package,tearDownortearDownPackage. Execution of tests in a test package begins as soon as the first test module is loaded from the test package.

Test modules

A test module is a python module that matches the testMatch regular expression. Test modules offer module-level setup and teardown; define the methodsetup,setup_module,setUporsetUpModulefor setup,teardown,teardown_module, ortearDownModulefor teardown. Execution of tests in a test module begins after all tests are collected.

Test classes

A test class is a class defined in a test module that matches testMatch or is a subclass of unittest.TestCase. All test classes are run the same way: Methods in the class that match testMatch are discovered, and a test case is constructed to run each method with a fresh instance of the test class. Like unittest.TestCase subclasses, other test classes can define setUp and tearDown methods that will be run before and after each test method. Test classes that do not descend fromunittest.TestCasemay also include generator methods and class-level fixtures. Class-level setup fixtures may be namedsetup_class,setupClass,setUpClass,setupAllorsetUpAll; teardown fixtures may be namedteardown_class,teardownClass,tearDownClass,teardownAllortearDownAll. Class-level setup and teardown fixtures must be class methods.

Test functions

Any function in a test module that matches testMatch will be wrapped in aFunctionTestCaseand run as a test. The simplest possible failing test is therefore:

def test():
    assert False

And the simplest passing test:

def test():
    pass

Test functions may define setup and/or teardown attributes, which will be run before and after the test function, respectively. A convenient way to do this, especially when several test functions in the same module need the same setup, is to use the providedwith_setupdecorator:

def setup_func():
    "set up test fixtures"

def teardown_func():
    "tear down test fixtures"

@with_setup(setup_func, teardown_func)
def test():
    "test ..."

For python 2.3 or earlier, add the attributes by calling the decorator function like so:

def test():
    "test ... "

test = with_setup(setup_func, teardown_func)(test)

or by direct assignment:

test.setup = setup_func
test.teardown = teardown_func

Please note thatwith_setupis useful only for test functions, not for test methods inunittest.TestCasesubclasses or other test classes. For those cases, definesetUpandtearDownmethods in the class.

Test generators

nose supports test functions and methods that are generators. A simple example from nose’s self test suite is probably the best explanation:

def test_evens():
    for i in range(0, 5):
        yield check_even, i, i*3def check_even(n, nn):
    assert n % 2 == 0 or nn % 2 == 0

This will result in five tests. nose will iterate the generator, creating a function test case wrapper for each tuple it yields. As in the example, test generators must yield tuples, the first element of which must be a callable and the remaining elements the arguments to be passed to the callable.

By default, the test name output for a generated test in verbose mode will be the name of the generator function or method, followed by the args passed to the yielded callable. If you want to show a different test name, set the description attribute of the yielded callable.

Setup and teardown functions may be used with test generators. However, please note that setup and teardown attributes attached to the generator functionwill execute only once. To execute fixtures for each yielded test, attach the setup and teardown attributes to the function that is yielded, or yield a callable object instance with setup and teardown attributes.

For example:

@with_setup(setup_func, teardown_func)def test_generator():
    # ...
    yield func, arg, arg # ...

Here, the setup and teardown functions will be executed once. Compare to:

def test_generator():
    # ...
    yield func, arg, arg # [email protected]_setup(setup_func, teardown_func)def func(arg):
    assert something_about(arg)

In the latter case the setup and teardown functions will execute once for each yielded test.

For generator methods, the setUp and tearDown methods of the class (if any) will be run before and after each generated test case. The setUp and tearDown methods do not run before the generator method itself, as this would cause setUp to run twice before the first test without an intervening tearDown.

Please note that method generators are not supported inunittest.TestCasesubclasses.


Previous topic

Basic usage

Next topic

Finding and running tests

This Page



转载于:https://my.oschina.net/cqlcql/blog/654276