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

源码
边栏推荐
- In the wechat environment, H5 jumps to the specified page of the applet
- 10 Super VIM plug-ins, I can't put them down
- Day_ ten
- _ 19_ IO stream summary
- 赫尔辛基交通安全改善项目部署Velodyne Lidar智能基础设施解决方案
- Activation and value transfer of activity
- AutoK3s v0.5.0 发布 延续简约和友好
- 绕过技术聊'跨端'......
- The paid video at station B caused the up master to lose more than ten thousand fans
- Precautions for function default parameters (formal parameter angle)
猜你喜欢

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

Day_ thirteen

Resolve the format conflict between formatted document and eslint

Navicat Premium 15 for Mac(数据库开发工具)中文版

The first day of reading mysql45

Reverse series to obtain any wechat applet code

绕过技术聊'跨端'......

AutoK3s v0.5.0 发布 延续简约和友好
Inter thread synchronization semaphore control

Geographic location data storage scheme - redis Geo
随机推荐
Deadlock, thread communication, singleton mode
Bypass technology to talk about 'cross end'
Gold three silver four, an article to solve the resume and interview
Data type variable operator
Stop "outsourcing" Ai models! The latest research finds that some "back doors" that undermine the security of machine learning models cannot be detected
Create raspberry PI image file of raspberry pie
20省市公布元宇宙路线图
XML usage and parsing of data storage and transmission files
Power representation in go language
Day_ eleven
The style of the mall can also change a lot. DIY can learn about it!
Lecun predicts AgI: big model and reinforcement learning are both ramps! My "world model" is the new way
Uncover gaussdb (for redis): comprehensive comparison of CODIS
Bugly hot update usage
In the wechat environment, H5 jumps to the specified page of the applet
Webgl and webgpu comparison [4] - uniform
Xinlou: un voyage de sept ans de Huawei Sports Health
Dart syntax
心楼:华为运动健康的七年筑造之旅
First knowledge of database