测试驱动开发(TDD)与行为驱动开发(BDD)实践指南
1. 测试重配置与程序更新
在软件开发中,测试是确保代码质量的关键环节。最初,测试 FormatAmount 函数时出现了失败情况,如 TestFormatAmount2 测试失败,原因是 Dollar.go 中的 FormatAmount 函数无论传入什么值都只返回 "USD 2.00" 。
为了解决这个问题,我们需要重新配置测试以处理小数值。具体步骤如下:
- 添加新测试 :在 Dollar_test.go 中添加 TestFormatAmount3 测试,用于测试小数值 5.10。以下是更新后的代码:
package main
import (
"testing"
)
func TestFormatAmount(t *testing.T) {
ans := FormatAmount(2.00)
if ans != "USD 2.00" {
t.Errorf("FormatAmount(2.00) = %s; Should be 2.00", ans)
}
}
func TestFormatAmount2(t *testing.T) {
ans := FormatAmount(4.00)
if ans != "USD 4.00" {
t.Errorf
超级会员免费看
订阅专栏 解锁全文
5197

被折叠的 条评论
为什么被折叠?



