2012-06-09 154 views
0

我的问题很简单:我正在使用pymox进行一堆单元测试。当我添加一个失败的新测试时,大多数时候很多其他测试失败。我怎样才能防止这种情况发生?为什么使用mox失败的测试失败了其他测试?

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

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.Mox()对象相同的结果,也将清理的东西,在TearDown中,与相同的结果

回答

1

我建议把所有的测试成扩展TestCase,然后在拆卸方法添加在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() 
+0

这绝对精彩,谢谢! “__init__”事情做到了。我在开始时采用了相同的方法,而不是在那里写的“__init__”:'def setUp(self): self.mox = mox.Mox()'。现在我用你所说的取代了这个,我不再有“连锁故障”。你能解释为什么会发生?因为对我来说这两种方法看起来都一样。 –

+0

我使用setUp(),它一直工作得很好。 – 2013-10-06 17:17:14

0

您还可以使用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