首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么有mox的失败测试也不能通过其他测试呢?

为什么有mox的失败测试也不能通过其他测试呢?
EN

Stack Overflow用户
提问于 2012-06-09 15:24:31
回答 2查看 1.9K关注 0票数 0

我的问题很简单:我正在使用pymox进行大量单元测试。当我添加一个失败的新测试时,大多数情况下,很多其他测试也会失败。我怎样才能防止这种情况发生呢?

例如,我有一个简单的脚本,其中有两个单元测试:

代码语言:javascript
复制
def test_main_returnsUnknown_ifCalculator_returnsMinus1(self):
    m=mox.Mox()
    m.StubOutWithMock(check_es_insert,"getArgs")
    check_es_insert.getArgs(\
        'Nagios plugin for checking the total number of documents stored in Elasticsearch')\
        .AndReturn({ 'critical' : 7, 'warning' : 5, 'address' : 'myhost:1234', 'file' : '/tmp/bla'})
    ################
    #some other mocking here, not relevant, I think
    ################
    m.ReplayAll()
    #now let's test
    check_es_docs.main()
    #verify and cleanup
    m.UnsetStubs()
    m.VerifyAll()
    m.ResetAll()
def test_main_doesWhatPrintAndExitSays_inNormalConditions(self):
    m=mox.Mox()
    m.StubOutWithMock(check_es_insert,"getArgs")
    check_es_insert.getArgs(\
        'Nagios plugin for checking the total number of documents stored in Elasticsearch')\
        .AndReturn({ 'critical' : 7, 'warning' : 5, 'address' : 'myhost:1234', 'file' : '/tmp/bla'})
    ################
    #some other mocking here, not relevant, I think
    ################
    m.ReplayAll()
    #now let's test
    check_es_docs.main()
    #verify and clean up
    m.UnsetStubs()
    m.VerifyAll()
    m.ResetAll()

通常,这两个测试都会通过,但是如果我在第二个测试中输入了一个错误,那么在运行测试时就会得到这个输出:

代码语言:javascript
复制
$ ./check_es_docs.test.py
FE
======================================================================
ERROR: test_main_returnsUnknown_ifCalculator_returnsMinus1 (__main__.Main)
If it can't get the current value from ES, print an error message and exit 3
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./check_es_docs.test.py", line 13, in test_main_returnsUnknown_ifCalculator_returnsMinus1
    m.StubOutWithMock(check_es_insert,"getArgs")
  File "/usr/local/lib/python2.7/dist-packages/mox-0.5.3-py2.7.egg/mox.py", line 312, in StubOutWithMock
    raise TypeError('Cannot mock a MockAnything! Did you remember to '
TypeError: Cannot mock a MockAnything! Did you remember to call UnsetStubs in your previous test?

======================================================================
FAIL: test_main_doesWhatPrintAndExitSays_inNormalConditions (__main__.Main)
If getCurrent returns a positive value, main() should print the text and exit with the code Calculator.printandexit() says
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./check_es_docs.test.py", line 69, in test_main_doesWhatPrintAndExitSays_inNormalConditions
    check_es_docs.main()
  File "/home/radu/check_es_docs.py", line 25, in main
    check_es_insert.printer("Total number of documents in Elasticsearch is %d | 'es_docs'=%d;%d;%d;;" % (result,result,cmdline['warning'],cmdline['critical']))
  File "/usr/local/lib/python2.7/dist-packages/mox-0.5.3-py2.7.egg/mox.py", line 765, in __call__
    return mock_method(*params, **named_params)
  File "/usr/local/lib/python2.7/dist-packages/mox-0.5.3-py2.7.egg/mox.py", line 1002, in __call__
    expected_method = self._VerifyMethodCall()
  File "/usr/local/lib/python2.7/dist-packages/mox-0.5.3-py2.7.egg/mox.py", line 1060, in _VerifyMethodCall
    raise UnexpectedMethodCallError(self, expected)
UnexpectedMethodCallError: Unexpected method call.  unexpected:-  expected:+
- printer.__call__("Total number of documents in Elasticsearch is 3 | 'es_docs'=3;5;7;;") -> None
?                           -

+ printer.__call__("Total nuber of documents in Elasticsearch is 3 | 'es_docs'=3;5;7;;") -> None

----------------------------------------------------------------------
Ran 2 tests in 0.002s

FAILED (failures=1, errors=1)

第一个测试应该通过,没有错误,因为它没有被改变一点。check_es_insert.getArgs()不应该是MockAnything实例,而且我也没有忘记调用UnsetStubs。我搜索了很多次,但没有找到其他有同样问题的人。所以我想我错过了一些很明显的东西..。

更多信息:

testing

  • check_es_insert是
  • check_es_docs是另一个脚本,从它导入了很多东西--
  • --我尝试在VerifyAll()后面放UnsetStubs(),结果相同--
  • --我尝试过从SetUp方法初始化mox.Mox()对象,并将清理的内容放在TearDown中,结果一样--
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-11 18:53:29

我建议将所有测试放到扩展TestCase的测试类中,然后在tearDown方法中添加一个UnsetStubs:

代码语言:javascript
复制
from unittest import TestCase
import mox

class MyTestCasee(TestCase):
  def __init__(self, testCaseName):
    self.m = mox.Mox()
    TestCase.__init__(self, testCaseName)

  def tearDown(self):
    self.m.UnsetStubs()


def test_main_returnsUnknown_ifCalculator_returnsMinus1(self):
  self.m.StubOutWithMock(check_es_insert,"getArgs")
  check_es_insert.getArgs(\
    'Nagios plugin for checking the total number of documents stored in Elasticsearch')\
    .AndReturn({ 'critical' : 7, 'warning' : 5, 'address' : 'myhost:1234', 'file' : '/tmp/bla'})
  ################
  #some other mocking here, not relevant, I think
  ################
  self.m.ReplayAll()
  #now let's test
  check_es_docs.main()
  #verify and cleanup
  self.m.VerifyAll()

def test_main_doesWhatPrintAndExitSays_inNormalConditions(self):
  self.m.StubOutWithMock(check_es_insert,"getArgs")
  check_es_insert.getArgs(\
      'Nagios plugin for checking the total number of documents stored in Elasticsearch')\
      .AndReturn({ 'critical' : 7, 'warning' : 5, 'address' : 'myhost:1234', 'file' : '/tmp/bla'})
  ################
  #some other mocking here, not relevant, I think
  ################
  self.m.ReplayAll()
  #now let's test
  check_es_docs.main()
  #verify and clean up
  self.m.VerifyAll()
  self.m.ResetAll()
票数 1
EN

Stack Overflow用户

发布于 2013-05-30 15:50:27

您还可以使用mox.MoxTestBase,它设置self.mox并在tearDown上调用VerifyAll()。

代码语言:javascript
复制
class ClassTestTest(mox.MoxTestBase):
  def test():
    m = self.mox.CreateMockAnything()

    m.something()
    self.mox.ReplayAll()
    m.something() # If this line is removed the test will fail
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10962196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档