【问题标题】:How to add Test name and module to Test docstring in result?如何在结果中将测试名称和模块添加到测试文档字符串?
【发布时间】:2016-07-20 07:38:20
【问题描述】:

我正在使用django_noseNoseTestSuiteRunner 运行测试。目前,如果测试有文档字符串,它将打印在控制台上以进行测试(TestCase.ShortDescrption()),如果它是None 打印测试的名称(TestCase.id()),我想将TestCase.id() 添加到TestCase.ShortDescription() 所以无论文档字符串是否存在,都会打印 TestCase.id() 。

样本测试:

class Foo(unittest.TestCase):
      def test_bar_1(self):
          """ this is docstring 1"""
          pass

      def test_bar_2(self):
          """ this is docstring 2"""
          self.fail()

所以对于结果而不是

this is a docstring 1 ... ok
this is a docstring 2 ... Fail

我想要

test_bar_1(path.to.test.Foo) this is a docstring 1 ... ok
test_bar_2(path.to.test.Foo) this is a docstring 2 ... Fail

【问题讨论】:

    标签: python django python-2.7 unit-testing nose


    【解决方案1】:

    我相信,如果您为您的 nosetests 命令提供--with-id 选项,您就可以实现您想要的。

    http://nose.readthedocs.org/en/latest/plugins/testid.html

    【讨论】:

    【解决方案2】:

    我修改了TestCase.shortDescription() 的实现以返回TestCase.__str__TestCase._testMethodDoc,如下所示:

    class Foo(unittest.TestCase):
    
          def shortDescription(self):
              doc = self.__str__() +": "+self._testMethodDoc
              return doc or None 
    
          def test_bar_1(self):
              """ this is docstring 1"""
              pass
    
          def test_bar_2(self):
              """ this is docstring 2"""
              self.fail()
    

    所以nosetests -v <module-name>.py 的结果是:

    test_bar_1(path.to.test.Foo): this is a docstring 1 ... ok
    test_bar_2(path.to.test.Foo): this is a docstring 2 ... Fail
    

    注意:我很快就会为它添加一个鼻子插件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多