vendredi 5 février 2016

Python unit test: testcase class with own constructor fails in standard library

I have this plain vanilla unit test, which works as expected, as long I leave out the constructor.

import sys
import unittest

class Instance_test(unittest.TestCase):

  def __init__(self):
     super(Instance_test, self).__init__()
     self.attribute = "new"

  def test_something(self):
     pass

  def test_other(self):
     self.assertTrue(True)
     pass

  def setUp(self):
     pass

  def tearDown(self):
     pass

def suite():
  return unittest.makeSuite(Instance_test, "test")

def main():
  runner = unittest.TextTestRunner(sys.stdout)
  runner.run(suite())

if __name__ == "__main__":
   main()

With the constructor in place in get this backtrace:


 Traceback (most recent call last):
   File "f:\gt\check.py", line 31, in  main()
   File "f:\gt\check.py", line 28, in main
      runner.run(suite())
   File "f:\gt\check.py", line 24, in suite
    return unittest.makeSuite(Instance_test, "test")
  File "C:\Python34\lib\unittest\loader.py", line 374, in makeSuite
    testCaseClass)
  File "C:\Python34\lib\unittest\loader.py", line 70, in loadTestsFromTestCase
    loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
  File "C:\Python34\lib\unittest\suite.py", line 24, in __init__
    self.addTests(tests)
  File "C:\Python34\lib\unittest\suite.py", line 60, in addTests
    for test in tests:
 TypeError: __init__() takes 1 positional argument but 2 were given

What's wrong and how else could I have a central attribute to be shared by different test_xxx methods?

Aucun commentaire:

Enregistrer un commentaire