如果setUpClass失败,如何查看失败测试的名称?

时间:2015-08-17 12:53:33

标签: unit-testing nose

请帮帮我。 如果setUpClass失败,如何查看失败测试的名称?

实施例

import unittest
import nose


class Test(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print "Execute setup class!"
        assert 10 != 10

    def test_1(self):
        """Test # 1"""
        pass

    def test_2(self):
        """Test # 2"""
        pass


if __name__ == "__main__":
    nose.run(argv=[" ", "work_temp.py", "--verbosity=2", "--nocapture"])

如果SetUp中的某些断言失败 - 我的输出结果不好(无法看到测试名称):

======================================================================
ERROR: test suite for <class 'tests.work_temp.Test'>
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/nose/suite.py", line 209, in run
    self.setUp()
.....................................................................
    return func()
  File "/home/temp/PycharmProjects/Safo/tests/work_temp.py", line 9, in setUpClass
    assert 10 != 10
AssertionError

----------------------------------------------------------------------
Ran 0 tests in 0.001s

FAILED (errors=1)

我希望看到类似的东西:

Test # 1 ... FAILED
Test # 2 ... FAILED

1 个答案:

答案 0 :(得分:0)

在为测试方法创建容器类时,抛出了一个断言异常,在这种情况下,唯一的逻辑行为是不运行底层测试。这就是鼻子所做的,但它符合python unittest specifications

  

如果在setUpClass期间引发异常,则不会运行该类中的测试,并且不会运行tearDownClass。

如果您愿意走出unittest库,可以通过修改代码来获得您想要的行为:

from nose.tools import with_setup

def setup_func():
    print "Execute setup class!"
    assert 10 != 10

@with_setup(setup_func)
def test_1():
    """Test # 1"""
    pass

@with_setup(setup_func)
def test_2():
    """Test # 2"""
    pass