Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Unittest throws uncaught TypeError: __init__() takes 1 positional argument but 2 were given

I am trying to create my first automated test suite with unittest for the first time.

However, when I try to execute my test suite, unittest throws the following exception:

Traceback (most recent call last):

File "testing_pos_line.py", line 31, in <module>
    unittest.main()
  File "C:\Users\barnej78\AppData\Local\Continuum\Anaconda3\lib\unittest\main.py", line 93, in __init__
    self.parseArgs(argv)
  File "C:\Users\barnej78\AppData\Local\Continuum\Anaconda3\lib\unittest\main.py", line 140, in parseArgs
    self.createTests()
  File "C:\Users\barnej78\AppData\Local\Continuum\Anaconda3\lib\unittest\main.py", line 144, in createTests
    self.test = self.testLoader.loadTestsFromModule(self.module)
  File "C:\Users\barnej78\AppData\Local\Continuum\Anaconda3\lib\unittest\loader.py", line 123, in loadTestsFromModule
    tests.append(self.loadTestsFromTestCase(obj))
  File "C:\Users\barnej78\AppData\Local\Continuum\Anaconda3\lib\unittest\loader.py", line 92, in loadTestsFromTestCase
    loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
  File "C:\Users\barnej78\AppData\Local\Continuum\Anaconda3\lib\unittest\suite.py", line 24, in __init__
    self.addTests(tests)
  File "C:\Users\barnej78\AppData\Local\Continuum\Anaconda3\lib\unittest\suite.py", line 57, in addTests
    for test in tests:
TypeError: __init__() takes 1 positional argument but 2 were given

If I misname my testcases to begin with a capital T, unittest runs correct, but does not apply any tests to my code.

My code is as follows:

import unittest
from regional_pos_line import Regional_pos_line as Rpl
class Testpos_line__split_columns_callback(unittest.TestCase):
    def __init__(self):
        pass
    def test_UT22_replace_end_row(self):
        self.line.split_columns_callback(self, 4, self.junk_function, 4, replace = True)
        self.assertEqual(self.line.data, [0,1,2,3,5])
    def test_UT23_replace_mid_row(self):
        self.line.split_columns_callback(self, 1, self.junk_function, 4, replace = True)
        self.assertEqual(self.line.data, [0,5,2,3,4])
    def test_UT24_replace_start_row(self):
        self.line.split_columns_callback(self, 0, self.junk_function, 4, replace = True)
        self.assertEqual(self.line.data, [5,1,2,3,4])
    def test_UT25_no_replace_end_row(self):
        self.line.split_columns_callback(self, 5, self.junk_function, 4, replace = False)
        self.assertEqual(self.line.data, [0,1,2,3,4,5])
    def test_UT26_no_replace_mid_row(self):
        self.line.split_columns_callback(self, 1, self.junk_function, 4, replace = True)
        self.assertEqual(self.line.data, [0,5,1,2,3,4])
    def test_UT27_no_replace_start_row(self):
        self.line.split_columns_callback(self, 0, self.junk_function, 4, replace = True)
        self.assertEqual(self.line.data, [5,0,1,2,3,4])
    def setUp(self):
        self.line = Rpl([0,1,2,3,4])
    def tearDown(self):
        del self.line
    def junk_function(self, x):
        return 5
if __name__ == '__main__':
    unittest.main()

What am I doing wrong?

like image 357
Jeremy Barnes Avatar asked Mar 14 '17 16:03

Jeremy Barnes


People also ask

How do you fix takes 1 positional argument but 2 were given?

To solve this ” Typeerror: takes 1 positional argument but 2 were given ” is by adding self argument for each method inside the class. It will remove the error.

When () takes 2 positional arguments but 3 were given?

The Python "TypeError: takes 2 positional arguments but 3 were given" occurs for multiple reasons: Forgetting to specify the self argument in a class method. Forgetting to specify a third argument in a function's definition. Passing three arguments to a function that only takes two.

Does unittest run in parallel?

By default, unittest-parallel runs unit tests on all CPU cores available.

What does unittest main () do?

Internally, unittest. main() is using a few tricks to figure out the name of the module (source file) that contains the call to main() . It then imports this modules, examines it, gets a list of all classes and functions which could be tests (according the configuration) and then creates a test case for each of them.


1 Answers

Try removing init function. It looks like you don't need it.

like image 127
KHL Avatar answered Oct 06 '22 00:10

KHL