实施简单的测试驱动开发
我正在尝试在以下测试方案中使用Python Unitsest测试计算器程序。但是,当我尝试使用 init 实例化“计算频道”时,它在底部给出以下错误消息。 我想使用 init 进行实例化。我正在使用Visual Studio代码。有人可以帮我吗?
这是“ calculator_test.py”的代码,
import unittest
import Calculator_steps as CalculatorClass
class TestCalculator(unittest.TestCase):
def __init__(self):
self.calculator = CalculatorClass.Calculator() # instantiate the CalculatorClass
def test_DecimalValues(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('3.55,8.76')
def test_NonNumeric(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('7,8,e')
def test_MoreThanThreeIntegers(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('5,7,2,8')
def test_MoreThan100(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('109,76,90')
以下是calculator_steps.py的代码
class Calculator:
def addNumbers(self, numberlist):
total = 0
if numberlist=="":
numberlist ="0"
numbers = numberlist.split(",")
if len(numbers)>3:
raise TypeError("Only three values can be entered")
for i in range(len(numbers)):
if int(numbers[i])>100:
raise TypeError("Only values less than 100 is allowed")
try:
total = total +int(numbers[i])
except ValueError:
TypeError("Only integers can be entered")
return total
。
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS C:\Users\mudit> py -m unittest Calculator_tests.py
Traceback (most recent call last):
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\__main__.py", line 18, in <module>
main(module=None)
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\main.py", line 100, in __init__
self.parseArgs(argv)
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\main.py", line 147, in parseArgs
self.createTests()
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\main.py", line 158, in createTests
self.test = self.testLoader.loadTestsFromNames(self.testNames,
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 220, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 220, in <listcomp>
suites = [self.loadTestsFromName(name, module) for name in names]
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 191, in loadTestsFromName
return self.loadTestsFromModule(obj)
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 124, in loadTestsFromModule
tests.append(self.loadTestsFromTestCase(obj))
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 93, in loadTestsFromTestCase
loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\suite.py", line 24, in __init__
self.addTests(tests)
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\suite.py", line 57, in addTests
for test in tests:
TypeError: TestCalculator.__init__() takes 1 positional argument but 2 were given
I am trying to test the calculator program using the python unittest on the following test scenarios. But when I try to instantiate the "CalculatorClass" using the init it gives the following error message in the bottom. I want to use init for instantiating. I am using Visual Studio Code. Could anyone help me with this?
This is the code of "Calculator_test.py"
import unittest
import Calculator_steps as CalculatorClass
class TestCalculator(unittest.TestCase):
def __init__(self):
self.calculator = CalculatorClass.Calculator() # instantiate the CalculatorClass
def test_DecimalValues(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('3.55,8.76')
def test_NonNumeric(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('7,8,e')
def test_MoreThanThreeIntegers(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('5,7,2,8')
def test_MoreThan100(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('109,76,90')
Following is the code of Calculator_steps.py
class Calculator:
def addNumbers(self, numberlist):
total = 0
if numberlist=="":
numberlist ="0"
numbers = numberlist.split(",")
if len(numbers)>3:
raise TypeError("Only three values can be entered")
for i in range(len(numbers)):
if int(numbers[i])>100:
raise TypeError("Only values less than 100 is allowed")
try:
total = total +int(numbers[i])
except ValueError:
TypeError("Only integers can be entered")
return total
Following is the error message
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS C:\Users\mudit> py -m unittest Calculator_tests.py
Traceback (most recent call last):
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\__main__.py", line 18, in <module>
main(module=None)
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\main.py", line 100, in __init__
self.parseArgs(argv)
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\main.py", line 147, in parseArgs
self.createTests()
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\main.py", line 158, in createTests
self.test = self.testLoader.loadTestsFromNames(self.testNames,
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 220, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 220, in <listcomp>
suites = [self.loadTestsFromName(name, module) for name in names]
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 191, in loadTestsFromName
return self.loadTestsFromModule(obj)
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 124, in loadTestsFromModule
tests.append(self.loadTestsFromTestCase(obj))
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 93, in loadTestsFromTestCase
loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\suite.py", line 24, in __init__
self.addTests(tests)
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\suite.py", line 57, in addTests
for test in tests:
TypeError: TestCalculator.__init__() takes 1 positional argument but 2 were given
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从
unittest.testcase
继承时,您确实不能覆盖__ INIT __
方法。相反,您要在每个测试运行之前要做的任何事情都可以放入设置
方法中,例如:要扩展,
unttstest
模块的设计方式是如此特殊事情发生在__ Init __
unittest.testcase
的方法中,并且该模块不支持覆盖__ init __ init __ init __
方法。从理论上讲,如果您在覆盖方法期间的某个时候调用__ INT __ INT __
方法,但是您为什么要这样做呢? UNITSEST模块的设计是为了覆盖__ INIT __
方法。您可以使用设置
和setupClass
方法在运行测试之前需要进行任何实例化When inheriting from
unittest.TestCase
, you do cannot overwrite the__init__
method. Instead, anything you want to do before each test runs can be put in thesetUp
method, like so:To expand, the
unitttest
module is designed in such a way that special things happen in the__init__
method ofunittest.TestCase
, and the module does not support overwriting the__init__
method. In theory, you could overwrite the__init__
method if you callsuper().__init__
at some point during the overwritten method, but why would you want to? The unittest module is designed so that you should never need to overwrite the__init__
method. You can use thesetUp
andsetUpClass
methods to do any instantiating needed before running your tests