• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用unittest.TestResult?

用户头像
it1352
帮助1

问题说明

我只用了很短一段时间的unittest.我正在使用Jython 2.7.10最终版本"

I've only been using unittest for a short time. I am using Jython 2.7.10 "final release"

在解释TestResult的Python 2.7文档中,它说:

In the Python 2.7 docs explaining TestResult it says:

TestResult类的以下方法用于维护 内部数据结构,并且可以在子类中扩展以支持 其他报告要求.这在以下方面特别有用 测试时支持交互式报告的构建工具 正在运行.

The following methods of the TestResult class are used to maintain the internal data structures, and may be extended in subclasses to support additional reporting requirements. This is particularly useful in building tools which support interactive reporting while tests are being run.

startTest(测试)... stopTest(测试)... startTestRun()... stopTestRun()¶

startTest(test) ... stopTest(test) ... startTestRun() ... stopTestRun()¶

这就是我想要做的...但是我不知道您如何使用TestResult.这是SSCCE ...

That's what I want to do... but I can't work out how you use TestResult. Here's an SSCCE...

import unittest

class TestResultX( unittest.TestResult ):
    def startTest( self, test ):
        print( '# blip')
        unittest.TestResult.startTest( self, test )
    def stopTest( self, test ):
        print( '# blop')
        unittest.TestResult.stopTest( self, test )
    def startTestRun( self ):
        print( '# blep')
        unittest.TestResult.startTestRun( self )
    def stopTestRun( self ):
        print( '# blap')
        unittest.TestResult.stopTestRun( self )

class TestCaseX( unittest.TestCase ):
    def test_nonsense(self):
        print( '# wotcha' )
        self.assertTrue( False )

    def run( self, test_result=None ):
        print( '# spoons starting...')

        test_result = TestResultX()
        unittest.TestCase.run( self, test_result )

        print( '# ...spoons ended, tr %s' % ( test_result,  ) )

unittest.main()

结果:

# spoons starting...

----------------------------------------------------------------------
Ran 0 tests in 0.015s

OK
# blip
# wotcha
# blop
# ...spoons ended, tr <__main__.TestResultX run=1 errors=0 failures=1>

问题:

  • 为什么说0 tests?
  • 为什么不打印blepblap(运行的开始和结束)?
  • Why does it say 0 tests?
  • Why are blep and blap (start and end of run) not printed?

更一般的说明:

  1. 在涉及TestResult,TestRunner,TestLoader等问题时,有人可以指出一本说明正确使用"/良好实践"的优秀教程/书.我得到了"TDD with Python",但没有.似乎没有任何解释.

  1. Can someone possibly point to a good tutorial/book explaining "proper use"/"good practice" when it comes to TestResult, TestRunner, TestLoader, etc. I got "TDD with Python", but it doesn't seem to explain any of this.

有人可以告诉我为什么似乎经常使用unittest2代替unittest吗?

Can someone possibly tell me why unittest2 often seems to be used instead of unittest?

附录

addendum

在Omar Diab努力查看源代码之后,我尝试了以下方法:

Following Omar Diab's efforts at looking at the source code I tried this:

def run( self, *args, **kvargs ):
    result = self.defaultTestResult()
    startTestRun = getattr(result, 'startTestRun', None)
    logger.info( '# calling superclass run... startTestRun? %s' % ( startTestRun, ))
    unittest.TestCase.run( self, *args, **kvargs  )
    logger.info( '# ... superclass run ended')

不幸的是,每个test_XXX方法都给出了:

Unfortunately each test_XXX method then gave:

# calling superclass run... startTestRun? <bound method TestResult.startTestRun of <unittest.result.TestResult run=0 errors=0 failures=0>>

setUp for test_that_stuff_happened (__main__.xx_FT)

tearDown for test_that_stuff_happened (__main__.xx_FT)
end tearDown...
. # ... superclass run ended

正确答案

#1

哇,没有回应.我很惊讶.

Wow, no responses. I'm surprised.

如果您希望在运行开始和运行结束时进行操作,那么大多数人无疑会自己解决这个问题:

This is a hack which most people could no doubt work out for themselves, if you want stuff to happen at the start of the run and end of the run:

TestCase的子类,根据:

Subclass TestCase, as per:

def setUp( self ):
    if not hasattr( unittest.TestCase, 'app' ):
        unittest.TestCase.app = MyApp()
        def shutdown_func():
            pass # do any end-of-run stuff here
        atexit.register( shutdown_func )
        pass # do any start-of-run stuff here
    self.app = unittest.TestCase.app

然后从这个子类中创建所有TestCases子类...

Then make all your TestCases subclass from this one...

关键是,如果您希望这种情况发生,则您的应用程序仅构建一次.当然,要负责确保每个连续的setUp是原始的". 显然,您可以改用setUpClass,但随后您将无权访问TestCase实例.

The point being, if you want this to happen, that your app is only constructed once. Handling the responsibility of ensuring that it is "pristine" for each successive setUp is up to you of course. Obviously you could use setUpClass instead but then you don't have access to the TestCase instance.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfkjhi
系列文章
更多 icon
同类精品
更多 icon
继续加载