测试驱动开发:理念、实践与挑战
1. 测试驱动开发实践示例
在软件开发中,测试驱动开发(TDD)是一种重要的开发方法。下面以 FootballTeam 类为例,详细介绍 TDD 的实践过程。
1.1 初始测试与实现
首先,运行测试,发现测试失败后,对 FootballTeam 类进行修改,使测试通过。以下是实现 compareTo() 方法以识别较弱球队的代码:
public int compareTo(FootballTeam otherTeam) {
if (gamesWon > otherTeam.getGamesWon()) {
return 1;
}
else if (gamesWon < otherTeam.getGamesWon()) {
return -1;
}
return 0;
}
当所有测试通过后,我们可以继续进行相等性测试。
1.2 相等性测试
以下是进行相等性测试的代码:
@Test
public void teamsWithSameNumberOfMatchesWonShouldBeEqual() {
FootballTeam teamA = new FootballTeam(2);
FootballTeam teamB = new Footb
超级会员免费看
订阅专栏 解锁全文
176万+

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



