0

这是1的后续问题

我需要在一个循环中用两个给定的情况测试这个函数。但是,根据打印的结果,似乎只检查了第一次迭代?这有关系runner.run(unittest.makeSuite(MyTestCase))吗?

import unittest
from StringIO import StringIO
import rice_output

class MyTestCase(unittest.TestCase):
    def setUp(self):
        #####Pre-defined inputs########
        self.dsed_in=[1,2]
        self.a_in=[2,3]
        self.pb_in=[3,4]

        #####Pre-defined outputs########
        self.msed_out=[6,24]

        #####TestCase run variables########
        self.tot_iter=len(self.a_in)

def testMsed(self):
    for i in range(self.tot_iter):
        print i
        fun = rice_output.msed(self.dsed_in[i],self.a_in[i],self.pb_in[i])
        value = self.msed_out[i]
        testFailureMessage = "Test of function name: %s iteration: %i expected: %i != calculated: %i" % ("msed",i,value,fun)
        return self.assertEqual(round(fun,3),round(self.msed_out[i],3),testFailureMessage)

from pprint import pprint
stream = StringIO()
runner = unittest.TextTestRunner(stream=stream)
result = runner.run(unittest.makeSuite(MyTestCase))
print 'Tests run ', result.testsRun
print 'Errors ', result.errors

这是他的输出:

0
Tests run  1
Errors  []
[]
Test output
testMsed (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

有什么建议么?谢谢!

4

1 回答 1

3

删除return语句

def testMsed(self):
  for i in range(self.tot_iter):
    print i
    fun = rice_output.msed(self.dsed_in[i],self.a_in[i],self.pb_in[i])
    value = self.msed_out[i]
    testFailureMessage = "Test of function name: %s iteration: %i expected: %i != calculated: %i" % ("msed",i,value,fun)
    self.assertEqual(round(fun,3),round(self.msed_out[i],3),testFailureMessage)
于 2013-01-11T19:10:19.187 回答