首页 后端开发 Python教程 使用测试驱动开发 (TDD) 构建字符串计算器:分步指南

使用测试驱动开发 (TDD) 构建字符串计算器:分步指南

Jan 15, 2025 pm 06:09 PM

Building a String Calculator with Test-Driven Development (TDD): A Step-by-Step Guide

我们将使用测试驱动开发 (TDD) 方法在 Python 中实现字符串计算器。这意味着我们将在实现相应功能之前为每个功能编写测试。

您可以参考链接 https://osherove.com/tdd-kata-1 作为实施 TDD 的检查点。该链接提供了您可以遵循的分步说明。

入门

在项目文件夹中,创建两个文件:string_calculator.py 和tests/test_string_calculator.py。我们将逐步实现这些功能。首先,我们需要创建一个带有 add 方法的 StringCalculator 类。

第 1 步:空字符串应返回“0”

让我们使用单元测试库为我们的应用程序编写第一个测试。打开tests/test_string_calculator.py 文件并从以下代码开始:

import unittest
from string_calculator import StringCalculator

class TestStringCalculator(unittest.TestCase):
    """Test suite for the StringCalculator class."""

    def setUp(self):
        """
        Create a new instance of StringCalculator for each test.
        Can use static method to avoid creating a new instance.
        """
        self.calculator = StringCalculator()

    def test_empty_string_returns_zero(self):
        """
        Test case: Adding an empty string should return 0.
        Input: "" 
        Expected Output: 0
        """
        self.assertEqual(self.calculator.add(""), 0)
登录后复制
登录后复制
登录后复制

现在,让我们在 string_calculator.py 文件中实现 StringCalculator 类:

class StringCalculator:
    def add(self, numbers:str):
        if not numbers:
            return 0

登录后复制
登录后复制
登录后复制

要运行测试,请按照以下步骤操作:

  1. 确保您位于 string_calculator.py 和tests/test_string_calculator.py 文件所在的项目目录中。

  2. 打开终端或命令提示符。

  3. 运行以下命令来执行测试:

python -m unittest discover tests
登录后复制
登录后复制
登录后复制

此命令将自动发现并运行测试文件夹中的所有测试。

预期输出:

如果测试通过,您应该会看到类似这样的内容:


----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
登录后复制
登录后复制
登录后复制

如果一切设置正确并且测试用例通过,则意味着您处理空字符串的实现正在按预期工作。

第 2 步:将一两个数字相加应返回其总和

我们需要更新方法来处理输入字符串中只有一个数字或两个数字的情况,并且它应该返回它们的总和。对于空字符串,该方法应返回 0。

编写测试

打开tests/test_string_calculator.py 文件并添加以下测试用例以覆盖所有场景:

    def test_add_single_number(self):
        """
        Test case: Adding a single number should return the number itself.
        Input: "1"
        Expected Output: 1
        """
        self.assertEqual(self.calculator.add("1"), 1)

    def test_add_two_numbers(self):
        """
        Test case: Adding two numbers should return their sum.
        Input: "1,2"
        Expected Output: 3
        """
        self.assertEqual(self.calculator.add("1,2"),3)
登录后复制
登录后复制
登录后复制

实施准则

现在,更新 string_calculator.py 文件中的 add 方法来处理一两个数字的加法:

class StringCalculator:
    def add(self, numbers:str):
        if not numbers:
            return 0
        '''
        Split the string by commas, convert each value to an integer, 
        and sum them up
        '''
        numbers_list = map(int,numbers.split(',')) 
        return sum(numbers_list)
登录后复制
登录后复制
登录后复制

您可以按照前面的步骤再次测试代码。

第 3 步:处理多个号码

我们将编写一个测试用例来检查该方法是否可以处理用逗号分隔的多个数字。

编写测试

打开tests/test_string_calculator.py 文件并添加一个测试用例来处理多个数字:

import unittest
from string_calculator import StringCalculator

class TestStringCalculator(unittest.TestCase):
    """Test suite for the StringCalculator class."""

    def setUp(self):
        """
        Create a new instance of StringCalculator for each test.
        Can use static method to avoid creating a new instance.
        """
        self.calculator = StringCalculator()

    def test_empty_string_returns_zero(self):
        """
        Test case: Adding an empty string should return 0.
        Input: "" 
        Expected Output: 0
        """
        self.assertEqual(self.calculator.add(""), 0)
登录后复制
登录后复制
登录后复制

功能已经实现,我们可以继续测试代码,然后继续下一步。

步骤 4:处理数字之间的换行符

现在,我们需要增强 add 方法来处理除逗号之外的新行 (n) 作为数字之间的有效分隔符。

编写测试

打开tests/test_string_calculator.py 文件并添加一个测试用例来检查该方法是否正确处理新行作为分隔符:

class StringCalculator:
    def add(self, numbers:str):
        if not numbers:
            return 0

登录后复制
登录后复制
登录后复制

实施准则

接下来,更新 string_calculator.py 文件中的 add 方法以将新行 (n) 作为分隔符处理。我们可以修改方法,将n替换为逗号,然后用逗号分割字符串。

这是 add 方法的更新代码:

python -m unittest discover tests
登录后复制
登录后复制
登录后复制

您可以按照step1.

中定义的先前步骤再次测试代码

第 5 步:处理自定义分隔符

在此步骤中,我们将进一步增强功能以​​允许自定义分隔符。例如,用户应该能够在字符串的开头指定自定义分隔符。例如:

  • 输入字符串可以以 // 开头,后跟自定义分隔符,例如 //;n1;2;3 应返回 6。
  • 我们将支持 //;n1;2;3 等分隔符。

编写测试

打开tests/test_string_calculator.py 文件并添加一个测试用例来处理自定义分隔符功能:


----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
登录后复制
登录后复制
登录后复制

实施准则

要处理自定义分隔符,请更新 add 方法以在输入字符串中查找分隔符。分隔符应在 //.

之后的字符串开头指定

这是更新后的添加方法:

    def test_add_single_number(self):
        """
        Test case: Adding a single number should return the number itself.
        Input: "1"
        Expected Output: 1
        """
        self.assertEqual(self.calculator.add("1"), 1)

    def test_add_two_numbers(self):
        """
        Test case: Adding two numbers should return their sum.
        Input: "1,2"
        Expected Output: 3
        """
        self.assertEqual(self.calculator.add("1,2"),3)
登录后复制
登录后复制
登录后复制

第 6 步:处理负数

在这一步中,我们需要修改add方法来处理负数。当传递负数时,它应该抛出异常,并显示消息“不允许使用负数”,并包含传递的负数。

编写测试

打开tests/test_string_calculator.py文件并添加一个测试用例来处理负数异常:

class StringCalculator:
    def add(self, numbers:str):
        if not numbers:
            return 0
        '''
        Split the string by commas, convert each value to an integer, 
        and sum them up
        '''
        numbers_list = map(int,numbers.split(',')) 
        return sum(numbers_list)
登录后复制
登录后复制
登录后复制

实施准则

现在,修改 add 方法以检查负数并使用适当的消息引发 ValueError。

这是更新后的添加方法:

def test_add_multiple_numbers(self):
    """
    Test case: Adding multiple numbers should return their sum.
    Input: "1,2,3,4,5"
    Expected Output: 15
    """
    self.assertEqual(self.calculator.add("1,2,3,4,5"), 15)
登录后复制
登录后复制

第 7 步:计算 Add 方法调用次数

在此步骤中,我们将向 StringCalculator 类添加一个名为 GetCalledCount() 的方法,该方法将返回 add() 方法被调用的次数。我们将遵循 TDD 流程,首先编写失败的测试,然后实现该功能。

编写测试

首先为 GetCalledCount() 方法添加一个测试用例。此测试应检查该方法是否正确计算调用 add() 的次数。

打开tests/test_string_calculator.py 文件并添加以下测试:

import unittest
from string_calculator import StringCalculator

class TestStringCalculator(unittest.TestCase):
    """Test suite for the StringCalculator class."""

    def setUp(self):
        """
        Create a new instance of StringCalculator for each test.
        Can use static method to avoid creating a new instance.
        """
        self.calculator = StringCalculator()

    def test_empty_string_returns_zero(self):
        """
        Test case: Adding an empty string should return 0.
        Input: "" 
        Expected Output: 0
        """
        self.assertEqual(self.calculator.add(""), 0)
登录后复制
登录后复制
登录后复制

实施准则

现在,在 StringCalculator 类中实现 GetCalledCount() 方法。此方法需要跟踪 add() 被调用的次数。

这是更新后的 StringCalculator 类:

class StringCalculator:
    def add(self, numbers:str):
        if not numbers:
            return 0

登录后复制
登录后复制
登录后复制

步骤 8 和 9:忽略大于 1000 的数字并处理任意长度的自定义分隔符

在这一步中,我们将实现两个要求:

  1. 总和中应忽略大于 1000 的数字。
  2. 自定义分隔符可以是任意长度,格式为 //[delimiter]n,并且该方法应该处理它们。

我们将首先针对这两个要求编写测试,然后在 StringCalculator 类中实现功能。

编写测试

为忽略大于 1000 的数字和处理任意长度的自定义分隔符添加以下测试。打开tests/test_string_calculator.py 文件并添加以下内容:

python -m unittest discover tests
登录后复制
登录后复制
登录后复制

实施准则

现在,实现 StringCalculator 类中的功能。这将包括:

  1. 忽略大于 1000 的数字。
  2. 处理任意长度的自定义分隔符。

这是更新后的 StringCalculator 类:


----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
登录后复制
登录后复制
登录后复制

步骤 10:多个分隔符支持

在这一步中,我们将修改 add() 方法以支持任意长度的多个分隔符。这将使我们能够处理格式为 //[delimiter1][delimiter2]n.

的多个分隔符的情况

编写测试

首先添加一个测试用例来检查多个分隔符。打开tests/test_string_calculator.py 文件并添加以下测试:

    def test_add_single_number(self):
        """
        Test case: Adding a single number should return the number itself.
        Input: "1"
        Expected Output: 1
        """
        self.assertEqual(self.calculator.add("1"), 1)

    def test_add_two_numbers(self):
        """
        Test case: Adding two numbers should return their sum.
        Input: "1,2"
        Expected Output: 3
        """
        self.assertEqual(self.calculator.add("1,2"),3)
登录后复制
登录后复制
登录后复制

实施准则

现在,修改 add() 方法以处理多个分隔符。分隔符将在 [] 内部传递,我们需要支持处理 //[delimiter1][delimiter2]n.

格式的多个分隔符

这是支持此功能的更新后的 StringCalculator 类:

class StringCalculator:
    def add(self, numbers:str):
        if not numbers:
            return 0
        '''
        Split the string by commas, convert each value to an integer, 
        and sum them up
        '''
        numbers_list = map(int,numbers.split(',')) 
        return sum(numbers_list)
登录后复制
登录后复制
登录后复制

测试它

再次运行测试以验证一切正常,包括与旧格式的向后兼容性以及对新的多个分隔符格式的支持:

def test_add_multiple_numbers(self):
    """
    Test case: Adding multiple numbers should return their sum.
    Input: "1,2,3,4,5"
    Expected Output: 15
    """
    self.assertEqual(self.calculator.add("1,2,3,4,5"), 15)
登录后复制
登录后复制

预期输出

旧格式和新格式的测试都应该通过:

def test_add_numbers_with_newlines(self):
    """
    Test case: Adding numbers separated by newlines should return their sum.
    Input: "1\n2\n3"
    Expected Output: 6
    """
    self.assertEqual(self.calculator.add("1\n2\n3"), 6)
登录后复制

感谢您关注这个 TDD 系列!我希望你觉得它有用。

以上是使用测试驱动开发 (TDD) 构建字符串计算器:分步指南的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1659
14
CakePHP 教程
1416
52
Laravel 教程
1310
25
PHP教程
1258
29
C# 教程
1233
24
Python vs.C:申请和用例 Python vs.C:申请和用例 Apr 12, 2025 am 12:01 AM

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。 Python以简洁和强大的生态系统着称,C 则以高性能和底层控制能力闻名。

Python:游戏,Guis等 Python:游戏,Guis等 Apr 13, 2025 am 12:14 AM

Python在游戏和GUI开发中表现出色。1)游戏开发使用Pygame,提供绘图、音频等功能,适合创建2D游戏。2)GUI开发可选择Tkinter或PyQt,Tkinter简单易用,PyQt功能丰富,适合专业开发。

2小时的Python计划:一种现实的方法 2小时的Python计划:一种现实的方法 Apr 11, 2025 am 12:04 AM

2小时内可以学会Python的基本编程概念和技能。1.学习变量和数据类型,2.掌握控制流(条件语句和循环),3.理解函数的定义和使用,4.通过简单示例和代码片段快速上手Python编程。

您可以在2小时内学到多少python? 您可以在2小时内学到多少python? Apr 09, 2025 pm 04:33 PM

两小时内可以学到Python的基础知识。1.学习变量和数据类型,2.掌握控制结构如if语句和循环,3.了解函数的定义和使用。这些将帮助你开始编写简单的Python程序。

Python与C:学习曲线和易用性 Python与C:学习曲线和易用性 Apr 19, 2025 am 12:20 AM

Python更易学且易用,C 则更强大但复杂。1.Python语法简洁,适合初学者,动态类型和自动内存管理使其易用,但可能导致运行时错误。2.C 提供低级控制和高级特性,适合高性能应用,但学习门槛高,需手动管理内存和类型安全。

Python和时间:充分利用您的学习时间 Python和时间:充分利用您的学习时间 Apr 14, 2025 am 12:02 AM

要在有限的时间内最大化学习Python的效率,可以使用Python的datetime、time和schedule模块。1.datetime模块用于记录和规划学习时间。2.time模块帮助设置学习和休息时间。3.schedule模块自动化安排每周学习任务。

Python:探索其主要应用程序 Python:探索其主要应用程序 Apr 10, 2025 am 09:41 AM

Python在web开发、数据科学、机器学习、自动化和脚本编写等领域有广泛应用。1)在web开发中,Django和Flask框架简化了开发过程。2)数据科学和机器学习领域,NumPy、Pandas、Scikit-learn和TensorFlow库提供了强大支持。3)自动化和脚本编写方面,Python适用于自动化测试和系统管理等任务。

Python:自动化,脚本和任务管理 Python:自动化,脚本和任务管理 Apr 16, 2025 am 12:14 AM

Python在自动化、脚本编写和任务管理中表现出色。1)自动化:通过标准库如os、shutil实现文件备份。2)脚本编写:使用psutil库监控系统资源。3)任务管理:利用schedule库调度任务。Python的易用性和丰富库支持使其在这些领域中成为首选工具。

See all articles