标签归档:testcase

如何在带有鼻息测试的文件中指定一个测试?

问题:如何在带有鼻息测试的文件中指定一个测试?

我有一个名为test_web.py的文件,其中包含一个TestWeb类和许多名为test_something()的方法。

我可以像这样运行类中的每个测试:

$ nosetests test_web.py 
...
======================================================================
FAIL: checkout test
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/me/path/here/test_web.py", line 187, in test_checkout
...

但是我似乎无法运行单个测试。当在同一PWD中运行时,这些错误会给我“无此类测试”错误:

$ nosetests test_web.py:test_checkout
$ nosetests TestWeb:test_checkout

这有什么问题吗?

I have a file called test_web.py containing a class TestWeb and many methods named like test_something().

I can run every test in the class like so:

$ nosetests test_web.py 
...
======================================================================
FAIL: checkout test
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/me/path/here/test_web.py", line 187, in test_checkout
...

But I can’t seem to run individual tests. These give me “No such test” errors when run in the same PWD:

$ nosetests test_web.py:test_checkout
$ nosetests TestWeb:test_checkout

What could be wrong here?


回答 0

您必须这样指定:nosetests <file>:<Test_Case>.<test_method>

nosetests test_web.py:TestWeb.test_checkout

查看文件

You must specify it like so: nosetests <file>:<Test_Case>.<test_method>, or

nosetests test_web.py:TestWeb.test_checkout

See the docs


回答 1

您还可以指定一个模块:

nosetests tests.test_integration:IntegrationTests.test_user_search_returns_users

You can also specify a module:

nosetests tests.test_integration:IntegrationTests.test_user_search_returns_users

回答 2

像其他答案所建议的那样,在命令行上指定名称确实有效并且很有用。但是,当我正在编写测试时,经常会发现我只想运行我正在进行的测试,并且我必须在命令行上编写的名称变得很长而且写起来很麻烦。在这种情况下,我更喜欢使用自定义装饰器和标志。

我这样定义wipd(“进行中的装饰器”):

from nose.plugins.attrib import attr
def wipd(f):
    return attr('wip')(f)

这定义了一个装饰器@wipd,该装饰器将wip在其装饰的对象上设置属性。例如:

import unittest
class Test(unittest.TestCase):

    @wipd
    def test_something(self):
        pass

然后-a wip可以在命令行中使用,以将测试的执行范围缩小到标有的测试@wipd

注意名称…

我使用@wipd装饰器的名称,而不是@wip避免这种问题:

import unittest
class Test(unittest.TestCase):

    from mymodule import wip    
    @wip
    def test_something(self):
        pass

    def test_something_else(self):
        pass

import将使wip装饰成员类的,以及所有在课堂测试将被选中。该attrib插件检查测试方法的父类,以查看所选属性是否也存在,并且创建和测试的属性attrib在隔离的空间中不存在。因此,如果您使用进行测试,-a foo并且您的类包含foo = "platypus",那么该插件中的所有测试都将被选中。

Specifying names on the command line like the other answers suggest does work and is useful. However, when I’m in the midst of writing tests, I often find that I want to run just the test I’m working on, and the names that I would have to write on the command line get pretty long and cumbersome to write. In such case, I prefer to use a custom decorator and flag.

I define wipd (“work in progress decorator”) like this:

from nose.plugins.attrib import attr
def wipd(f):
    return attr('wip')(f)

This defines a decorator @wipd which will set the wip attribute on objects it decorates. For instance:

import unittest
class Test(unittest.TestCase):

    @wipd
    def test_something(self):
        pass

Then -a wip can be used at the command line to narrow the execution of the test to the ones marked with @wipd.

Note on names…

I’m using the name @wipd for the decorator rather than @wip to avoid this kind of problem:

import unittest
class Test(unittest.TestCase):

    from mymodule import wip    
    @wip
    def test_something(self):
        pass

    def test_something_else(self):
        pass

The import will make the wip decorator a member of the class, and all tests in the class will be selected. The attrib plugin checks the parent class of a test method to see if the attribute selected exists there too, and the attributes that are created and tested by attrib do not exist in a segregated space. So if you test with -a foo and your class contains foo = "platypus", then all tests in the class will be selected by the plugin.


回答 3

要运行多个特定测试,您可以将它们添加到命令行中,并以空格分隔。

nosetests test_web.py:TestWeb.test_checkout test_web.py:TestWeb.test_another_checkout

To run multiple specific tests, you can just add them to the command line, separated by space.

nosetests test_web.py:TestWeb.test_checkout test_web.py:TestWeb.test_another_checkout

回答 4

在我的测试中,使用模块名称指定测试不起作用

您必须指定的实际路径.py

nosetests /path/to/test/file.py:test_function

这与 nose==1.3.7

In my tests, specifying tests with module names do not work

You must specify the actual path to the .py:

nosetests /path/to/test/file.py:test_function

This with nose==1.3.7


回答 5

我的要求是在另一个Windows目录中的测试文件中运行单个测试-这是通过anaconda命令提示符完成的,如下所示:

通过以下方式进行了鼻子测试

(base) C:\Users\ABC\Documents\work\

但是test_MyTestFile.pyMethodsFile.py位于:

 (base) C:\Users\ABC\Documents\work\daily\

通过包含引号的路径来运行单个测试,如下所示:

(base) C:\Users\ABC\Documents\work>nosetests "daily\\test_MyTestFile.py:MyTestClass.test_add_integers"

test_MyTestFile.py看起来像这样:

import methodsFile
import unittest

class MyTestClass(unittest.TestCase):

    def test_add_integers(self):
        assert methodsFile.add(5, 3) == 8

    def test_add_integers_zero(self):
        assert methodsFile.add(3, 0) == 3

MethodsFile.py看起来像这样:

def add(num1, num2):
        return num1 + num2

My requirement was to run a single test in a test file that was in another windows directory – this was done from the anaconda command prompt as follows:

ran nosetests from:

(base) C:\Users\ABC\Documents\work\

but test_MyTestFile.py and methodsFile.py were in:

 (base) C:\Users\ABC\Documents\work\daily\

run single test by including path with quotes as follows:

(base) C:\Users\ABC\Documents\work>nosetests "daily\\test_MyTestFile.py:MyTestClass.test_add_integers"

test_MyTestFile.py looked like this:

import methodsFile
import unittest

class MyTestClass(unittest.TestCase):

    def test_add_integers(self):
        assert methodsFile.add(5, 3) == 8

    def test_add_integers_zero(self):
        assert methodsFile.add(3, 0) == 3

methodsFile.py looked like this:

def add(num1, num2):
        return num1 + num2