当前位置:网站首页>一个 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;
}
重新运行单元测试,测试通过

源码
边栏推荐
- Take you to the open source project of smart home: the preliminary configuration of zhiting home cloud and home assistant+ homebridge
- Navicat premium 15 for MAC (database development tool) Chinese version
- Navicat Premium 15 for Mac(数据库开发工具)中文版
- Day_ ten
- Consumer and producer cases of inter thread synchronization (condition variable)
- 什么是骨干网
- Flutter assembly
- Data type variable operator
- Process control and method
- Linux-MySQL数据库之高级SQL 语句一
猜你喜欢

Reading mysql45 lecture - index continued

Day_ 05

10 Super VIM plug-ins, I can't put them down

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

Unity技术手册 - 生命周期内大小(Size over Lifetime)和速度决定大小(Size by Speed)

Understanding of reflection part

Describe your understanding of the evolution process and internal structure of the method area

mysql整体架构和语句的执行流程

The third day of mysql45

Bugly hot update usage
随机推荐
Create raspberry PI image file of raspberry pie
The style of the mall can also change a lot. DIY can learn about it!
Dart syntax
20省市公布元宇宙路线图
Uncover gaussdb (for redis): comprehensive comparison of CODIS
Go development team technical leader Russ Cox sends a document to share go's version control history
Unity技术手册 - 生命周期旋转RotationOverLifetime-速度旋转RotationBySpeed-外力ExternalForces
Overall MySQL architecture and statement execution process
GO语言-什么是临界资源安全问题?
Preliminary understanding of JVM
不要小看了积分商城,它的作用可以很大!
This article will help you understand the common concepts, advantages and disadvantages of JWT
有哪些新手程序员不知道的小技巧?
Native JS dynamically add elements
Practice of geospatial data in Nepal graph
Catheon gaming appointed mark Aubrey, former Asia Pacific head of Activision Blizzard, as CEO
Inter thread synchronization semaphore control
The release of autok3s v0.5.0 continues to be simple and friendly
Linux-MySQL数据库之高级SQL 语句一
Don't underestimate the integral mall, its role can be great!