使用unittest.TestResult?
问题说明
我只用了很短一段时间的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
? - 为什么不打印
blep
和blap
(运行的开始和结束)?
- Why does it say
0 tests
? - Why are
blep
andblap
(start and end of run) not printed?
更一般的说明:
-
在涉及TestResult,TestRunner,TestLoader等问题时,有人可以指出一本说明正确使用"/良好实践"的优秀教程/书.我得到了"TDD with Python",但没有.似乎没有任何解释.
-
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
正确答案
哇,没有回应.我很惊讶.
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
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20