我正在尝试第一次用unittest创建我的第一个自动化测试套件。

但是,当我尝试执行测试套件时,unittest会引发以下异常:

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

如果我将我的测试用例命名为以大写T开头,则unittest可以正确运行,但不会对我的代码应用任何测试。

我的代码如下:
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()

我究竟做错了什么?

最佳答案

尝试删除初始化函数。看来您不需要它。

关于Python Unittest引发未捕获的TypeError : __init__() takes 1 positional argument but 2 were given,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42790980/

10-12 18:16