Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 404
guidot Avatar asked Feb 05 '16 17:02

guidot


People also ask

How do you test a constructor in unit testing?

To test that a constructor does its job (of making the class invariant true), you have to first use the constructor in creating a new object and then test that every field of the object has the correct value. Yes, you need need an assertEquals call for each field.

Should a tester class have a constructor?

It depends on the scenario. If you have a test class, and for some weird reason if you need to create instance of it on another test class, you will need to use constructor. Otherwise test initialize more fits in the concept.

How do you test a unit test case in python?

If you're using PyCharm IDE, you can simply press ctrl+shift+F10 to run unittest module. Otherwise you can use command prompt to run this module. For example, we named the file for unit-testing as Basic_Test.py . So the command to run python unittest will be: $python3.

How do you use self assertRaises in Python?

There are two ways you can use assertRaises: using keyword arguments. Just pass the exception, the callable function and the parameters of the callable function as keyword arguments that will elicit the exception. Make a function call that should raise the exception with a context.


1 Answers

I would use unittest.TestCase's setUp() and tearDown() methods instead of init. Just do the same thing you're doing except use the setUp method.

like image 118
2achary Avatar answered Nov 15 '22 06:11

2achary