当前位置:网站首页>一个 TDD 示例
一个 TDD 示例
2022-06-25 15:42:00 【GreyZeng】
作者:Grey
原文地址:一个 TDD 示例
参考文档
码农翻身-从零开始造Spring 中的《介绍TDD开发方式, 重构的方法》
TDD(Test-Driven Development,测试驱动开发)的流程是
写一个测试用例 -> 运行:失败 -> 写Just enough的代码,让测试通过 -> 重构代码保持测试通过,然后循环往复。
下面,我们通过一个简单的例子来说明 TDD 的流程
需求:写一个方法,返回小于给定值max的所有素数组成的数组
public static int[] getPrimes(int max);
先做一个简单的任务分解
- 边界条件:getPrimes(0) getPrimes(-1) getPrimes(2)应该返回什么?
- 正常输入:getPrimes(9) getPrimes(17) getPrimes(30)
首先,要创建一个测试类
import org.junit.Assert;
import org.junit.Test;
public class PrimeUtilTest {
@Test
public void getPrimes() {
int[] expected = new int[]{
};
Assert.assertArrayEquals(expected, PrimeUtil.getPrimes(0));
Assert.assertArrayEquals(expected, PrimeUtil.getPrimes(-1));
Assert.assertArrayEquals(expected, PrimeUtil.getPrimes(2));
}
}
运行这个测试用例,显示测试失败:

然后实现刚好满足需求的这部分代码
public class PrimeUtil {
public static int[] getPrimes(int max) {
if (max == 0 || max == -1 || max == 2) {
return new int[]{
};
}
return null;
}
}
重新运行测试用例,测试通过

然后增加测试方法
@Test
public void getPrimes2() {
Assert.assertArrayEquals(new int[]{
2, 3, 5, 7}, PrimeUtil.getPrimes(9));
Assert.assertArrayEquals(new int[]{
2, 3, 5, 7, 11, 13}, PrimeUtil.getPrimes(17));
Assert.assertArrayEquals(new int[]{
2, 3, 5, 7, 11, 13, 17, 19, 23, 29}, PrimeUtil.getPrimes(30));
}
运行这个测试,报错

然后再实现满足这个测试用例的方法
public class PrimeUtil {
public static int[] getPrimes(int max) {
if (max <= 2) {
return new int[]{
};
}
int[] newArray = new int[max];
int size = 0, k= 0;
for (int i = 2 ; i < max; i++) {
for ( k = 2 ; k < i/2+1; k++) {
if(i%k ==0) {
break;
}
}
if (k == i / 2+1){
newArray[size++] = i;
}
}
newArray = Arrays.copyOf(newArray,size);
return newArray;
}
}
再次运行单元测试,测试通过

最后,重构getPrimes方法
public static int[] getPrimes(int max) {
if (max <= 2) {
return new int[]{
};
}
int[] primes = new int[max];
int count = 0, j = 0;
for (int num = 2; num < max; num++) {
if (isPrime(num)) {
primes[count++] = num;
}
}
primes = Arrays.copyOf(primes, count);
return primes;
}
private static boolean isPrime(int num) {
int i ;
for (i = 2; i < num / 2 + 1; i++) {
if (num % i == 0) {
return false;
}
}
if (i == num / 2 + 1) {
return true;
}
return false;
}
重新运行单元测试,测试通过

源码
边栏推荐
- Common APIs and exception mechanisms
- 10 Super VIM plug-ins, I can't put them down
- Beginner bug set
- Catheon Gaming任命Activision Blizzard前亚太区负责人Mark Aubrey担任首席执行官
- XML usage and parsing of data storage and transmission files
- First knowledge of database
- AutoK3s v0.5.0 发布 延续简约和友好
- mysql整体架构和语句的执行流程
- The database records are read through the system time under the Android system, causing the problem of incomplete Reading Records!
- Inter thread synchronization semaphore control
猜你喜欢

Bombard the headquarters. Don't let a UI framework destroy you

GO语言-锁操作

Xinlou: un voyage de sept ans de Huawei Sports Health

Reverse series to obtain any wechat applet code

In the wechat environment, H5 jumps to the specified page of the applet

What processes are needed to build a wechat applet from scratch?

Don't underestimate the integral mall, its role can be great!

Go language - lock operation

Alvaria宣布客户体验行业资深人士Jeff Cotten担任新首席执行官

Preliminary understanding of JVM
随机推荐
Day21 multithreading
绕过技术聊'跨端'......
Native JS dynamically add elements
JS add custom attributes to elements
数字经济时代文化消费新特征
Cocoapods installation in 2021
Collection overview, array encapsulation
Day_ twelve
Linux-MySQL数据库之高级SQL 语句一
Overall MySQL architecture and statement execution process
Problems caused by using ApplicationContext to render layout
商城风格也可以很多变,DIY了解一下!
cmd。。。。。。
Catheon Gaming任命Activision Blizzard前亚太区负责人Mark Aubrey担任首席执行官
Flutter assembly
[issue 24] one year experience of golang to develop futu
The paid video at station B caused the up master to lose more than ten thousand fans
File operation, serialization, recursive copy
Create raspberry PI image file of raspberry pie
XML usage and parsing of data storage and transmission files