为什么使用 mox 的失败测试也会失败其他测试



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

例如,我

有一个简单的脚本,我有两个单元测试:

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()

通常,两个测试都会通过,但是如果我在第二次测试中偷偷输入拼写错误,则在运行测试时会得到以下输出:

$ ./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。我搜索了很多,没有找到其他有同样问题的人。所以我想我错过了一些很明显的东西......

附加信息:

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

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

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()
你也可以

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

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

相关内容

  • 没有找到相关文章