如何在结果中添加测试名称和模块到测试文档字符串?

时间:2016-03-31 17:54:27

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

我正在使用 @Override public void onMapReady(GoogleMap googleMap) { //TODO: Allow user to update this 1-normal 2-satellite 3-Terrain MapsInitializer.initialize(mContext); gMap = googleMap; gMap.getUiSettings().setAllGesturesEnabled(false); googleMap.setMapType(1); googleMap.getUiSettings().setMapToolbarEnabled(false); // If we have map data, update the map content. if (mMapLocation != null) { updateMapContents(); } } ' django_nose来运行测试。目前,如果测试具有docstring,它将打印在控制台上进行测试(NoseTestSuiteRunner),如果打印出TestCase.ShortDescrption()测试名称(None),我想要将TestCase.id()添加到TestCase.id(),以便打印TestCase.id()而不管文档字符串的存在。

样本测试:

TestCase.ShortDescription()

所以结果而不是

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

2 个答案:

答案 0 :(得分:1)

我修改了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

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

答案 1 :(得分:0)

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

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