如何在nose2

时间:2016-11-10 11:20:08

标签: python unit-testing nose2

我希望在How to run specific test in Nose2之后使用unittest.TestCase运行nose2的子类中包含的单个测试,但它似乎并不适用于我。我使用以下示例脚本,我将其命名为mickey_mouse_test.py

import unittest

class TestMickeyMouse(unittest.TestCase):
    def test_1plus1is2(self):
        self.assertTrue(1+1 == 2)

    def test_to_uppercase(self):
        self.assertEqual("hello".upper(), "HELLO")

if __name__ == "__main__":
    unittest.main()

如果我在同一目录中运行nose2 mickey_mouse_test,它将运行模块中的所有测试:

kurt@kurt-ThinkPad:~/Documents/Scratch$ nose2 mickey_mouse_test
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

但是,如果我尝试只运行test_to_uppercase,我会收到错误:

kurt@kurt-ThinkPad:~/Documents/Scratch$ nose2 mickey_mouse_test.test_to_uppercase
E
======================================================================
ERROR: mickey_mouse_test.test_to_uppercase (nose2.loader.LoadTestsFailure)
----------------------------------------------------------------------
AttributeError: module 'mickey_mouse_test' has no attribute 'test_to_uppercase'

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

如果我使用-s选项,我仍然会收到错误,尽管是另一个错误:

kurt@kurt-ThinkPad:~/Documents/Scratch$ nose2 -s mickey_mouse_test.test_to_uppercase
E
======================================================================
ERROR: mickey_mouse_test.test_to_uppercase (nose2.loader.LoadTestsFailure)
----------------------------------------------------------------------
OSError: /home/kurt/Documents/Scratch/mickey_mouse_test.test_to_uppercase is not a directory

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

我也试过阅读"指定要运行的测试" http://nose2.readthedocs.io/en/latest/usage.html中的部分,其中声明了' Python对象部分'应该是一个点缀名称'。我不明白为什么在这种情况下,mickey_mouse_test.test_to_uppercase不是一个虚线名称'。任何想法为什么这不起作用?

1 个答案:

答案 0 :(得分:0)

以下是一种仅使用test_to_uppercase而不使用nose2Running single test from unittest.TestCase via command line之后)的方法:

kurt@kurt-ThinkPad:~/Documents/Scratch$ python mickey_mouse_test.py TestMickeyMouse.test_to_uppercase
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK