0

Sorry if this question is stupid. I created an unittest class which needs to take given inputs and outputs from outside. Thus, I guess these values should be initiated. However, I met some errors in the following code:

CODE:

import unittest
from StringIO import StringIO

##########Inputs and outputs from outside#######
a=[1,2]
b=[2,3]
out=[3,4]
####################################
def func1(a,b):
    return a+b

class MyTestCase(unittest.TestCase):
    def __init__(self,a,b,out):
        self.a=a
        self.b=b
        self.out=out
    def testMsed(self):
        for i in range(self.tot_iter):
            print i
            fun = func1(self.a[i],self.b[i])
            value = self.out[i]
            testFailureMessage = "Test of function name: %s iteration: %i expected: %i != calculated: %i" % ("func1",i,value,fun)
            self.assertEqual(round(fun,3),round(value,3),testFailureMessage)

if __name__ == '__main__':
    f = MyTestCase(a,b,out)

from pprint import pprint
stream = StringIO()
runner = unittest.TextTestRunner(stream=stream, verbosity=2)
result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))
print 'Tests run', result.testsRun

However, I got the following error

Traceback (most recent call last):
  File "C:testing.py", line 33, in <module>
    result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))
  File "C:\Python27\lib\unittest\loader.py", line 310, in makeSuite
    return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass)
  File "C:\Python27\lib\unittest\loader.py", line 50, in loadTestsFromTestCase
    if issubclass(testCaseClass, suite.TestSuite):
TypeError: issubclass() arg 1 must be a class

Can anyone give me some suggestions? Thanks!

TTT
  • 4,354
  • 13
  • 73
  • 123
  • I just breifly overlooked the documentation. I can't see any reason why `result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))` should work. Do you have a link to some documentation you have used to set up this unittesting? – brunsgaard Jan 14 '13 at 00:09

1 Answers1

1

The root of the problem is this line,

result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))

unittest.makeSuite expects a class, not an instance of a class. So just MyTestCase, not MyTestCase(a, b, out). This means that you can't pass parameters to your test case in the manner you are attempting to. You should probably move the code from init to a setUp function. Either access a, b, and out as globals inside setUp or take a look at this link for information regarding passing parameters to a unit test.

By the way, here is the source file within python where the problem originated. Might be informative to read.

Community
  • 1
  • 1