实施简单的测试驱动开发

发布于 2025-02-12 09:17:17 字数 3980 浏览 0 评论 0原文

我正在尝试在以下测试方案中使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流绪微梦 2025-02-19 09:17:17

unittest.testcase继承时,您确实不能覆盖__ INIT __方法。相反,您要在每个测试运行之前要做的任何事情都可以放入设置方法中,例如:

import unittest

import Calculator_steps as CalculatorClass

class TestCalculator(unittest.TestCase):
    

    def setUp(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')

要扩展,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 the setUp method, like so:

import unittest

import Calculator_steps as CalculatorClass

class TestCalculator(unittest.TestCase):
    

    def setUp(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')

To expand, the unitttest module is designed in such a way that special things happen in the __init__ method of unittest.TestCase, and the module does not support overwriting the __init__ method. In theory, you could overwrite the __init__ method if you call super().__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 the setUp and setUpClass methods to do any instantiating needed before running your tests

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文