软件测试—— junit 单元测试
Tasks:
- Install Junit(4.12), Hamcrest(1.3) with Eclipse
- Install Eclemma with Eclipse
- Write a java program for the triangle problem and test the program with Junit.
a) Description of triangle problem:
Function triangle takes three integers a,b,c which are length of triangle sides; calculates whether the triangle is equilateral, isosceles, or scalene.
EclEmma安装
1. 选择Help->Eclipse Marketplace->搜索EclEmma,Install;
2. 重启eclipse发现工具栏上出现Coverage图标,说明安装成功;
EclEmma使用
1. 新建一个项目,然后添加一个类,然后简单书写一些代码;
2. 右键项目->选择Coverage As->Java Application,可以得到如下结果:

3. 从运行结果可以看到,有多种颜色,其中
绿色表示代码被执行到
黄色表示代码部分执行到
红色表示代码没有被执行到
3.2 EclEmma检测覆盖率
1. 选择Window->Show View->Other->Java->Coverage可以看到代码执行的覆盖率;

2. 其中可以看到每一个类中代码被执行的百分比,见2,也可以看到整个项目代码被执行的百分比,见1;
3. 其中检测覆盖率可以用到单元测试中,查看单元测试覆盖率。
实验demo
主类:
package text2;
public class triangle {
public static int text1(int a,int b, int c) {
int array[];
array= new int[3];
array[0]= a;
array[1]= b ;
array[2]= c;
int temp;
for(int i = 0 ; i < 3 ; i++)
{
for(int j = 0 ; j < 2 ; j++)
{
if(array[j]>array[j+1])
{
temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
}
if(array[0]+array[1]>array[2])
{
if(array[0]==array[1] && array[0]==array[2])
return 3;
if(array[0]==array[1]||array[1]==array[2])
return 2;
if(array[0]*array[0]+array[1]*array[1]==array[2]*array[2])
return 4;
return 1;
}
return 0;
}
}
1代表三角形,0代表不构成3角形,2代表等腰三角形,3代表等边三角形,4代表直角三角形
------------在写博客的时候意识到没有判断是否是等腰直角三角形,如果是则先被判断为了等腰三角形。
测试类:
package text2;
import static org.junit.Assert.*; import java.util.Arrays;
import java.util.Collection; import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class)
public class ParameterTest {
int expected = 0;
int input1 = 0 ;
int input2 = 0;
int input3 = 0;
@Parameters
public static Collection<Object[] > t(){
return Arrays.asList(new Object[][]{
{0,3,1,2},
{0,4,2,2},
{2,2,2,1},
{3,1,1,1},
{4,3,4,5},
{1,6,8,7}
});
} public ParameterTest(int expected,int input1,int input2,int input3){
this.expected = expected;
this.input1 = input1;
this.input2 = input2;
this.input3 = input3;
}
@Test
public void test1(){
assertEquals(expected, triangle.text1(input1,input2,input3));
}
}
测试结果:

软件测试—— junit 单元测试的更多相关文章
- junit单元测试(keeps the bar green to keeps the code clean)
error是程序错误,failure是测试错误. junit概要: JUnit是由 Erich Gamma (设计模式的创始人)和 Kent Beck (敏捷开发的创始人之一)编写的一个回归测试框架( ...
- spring && Cobertura && maven &&junit 单元测试以及测试覆盖率
1. 目的: junit 单元测试,Cobertura 测试覆盖率报告 项目目录结构 2. maven 配置 <project xmlns= ...
- 解决Junit单元测试 找不到类 ----指定Java Build Path
做junit 单元测试时,发现怎么执行都是以前编译过得代码. 最后找到原因了, src/test/java 编译完的.class路径是 Default output folder Default ou ...
- JUnit单元测试框架的使用
http://blog.csdn.net/mao520741111/article/details/51462215 原文地址 http://www.open-open.com/lib/view/op ...
- Java 工具 JUnit单元测试
Java 工具 JUnit单元测试 @author ixenos 1.1. JUnit单元测试框架的基本使用 一.搭建环境: 导入junit.jar包(junit4) 二.写测试类: 0,一般一个 ...
- Spring框架中整合JUnit单元测试的方法
一. 步骤: 1. 拷贝jar包: 1. JUnit-4.9.jar和spring-test-4.2.4.RELEASE.jar ; 2. 替换原来的main函数: 1. 在测试类上使用注解方式替换: ...
- spring框架学习(三)junit单元测试
spring框架学习(三)junit单元测试 单元测试不是头一次听说了,但只是听说从来没有用过.一个模块怎么测试呢,是不是得专门为一单元写一个测试程序,然后将测试单元代码拿过来测试? 我是这么想的.学 ...
- 备忘:Junit单元测试
junit 目前测试都是在main方法中调用目前的结果都需要人工对比是否是想要的 1.使用Junit测试方法,绿色条条代表方法测试成功,没有bug,如果是红色条条代表有异常,测试不通过2.点击方法名. ...
- 单元测试系列:JUnit单元测试规范
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6762032.html Junit测试代 ...
随机推荐
- Async 和 Await的性能(.NET4.5新异步编程模型)
异步编程长时间以来一直都是那些技能高超.喜欢挑战自我的开发人员涉足的领域 — 这些人愿意花费时间,充满热情并拥有心理承受能力,能够在非线性的控制流程中不断地琢磨回调,之后再回调. 随着 Microso ...
- java-多态性
1 多态性 主要表现在上转型对象 2 强制类型转换 2.1 基本类型的强制类型转换 转换只能在数值间进行.包括整数型.字符型.浮点型.数值类型和布尔类型间不能转换. 2.2 引用类型变量转换成其子类型 ...
- for name in loop Shell
for NAME in joe jane juliedoADDRESS="$NAME@example.com"MESSAGE='Projects are due today!'ec ...
- webstorm 10 设置文件的默认编码
我在使用webstorm时,发现文件的默认编码是GBK 然后我找到了点击此处可以修改这个文件的编码,但是以后新建文件和项目默认生成的文件还是GBK, 设置项目文件的默认编码可以在 File----Se ...
- C语言每日一题之No.8
正式面对自己第二天,突然一种强烈的要放弃的冲动,在害怕什么?害怕很难赶上步伐?害怕这样坚持到底是对还是错?估计是今天那个来了,所以身体激素有变化导致情绪起伏比较大比较神经质吧(☆_☆)~矮油,女人每个 ...
- Spark工程开发前台技术实现与后台函数调用
Spark是一个通用的大规模数据快速处理引擎.可以简单理解为Spark就是一个大数据分布式处理框架.基于内存计算的Spark的计算速度要比Hadoop的MapReduce快上50倍以上,基于磁盘的计算 ...
- Mongodb(2)创建数据库,删除数据库,创建集合,删除集合,显示文档内容
显示所有数据库列表:show dbs > show dbs local .078GB runoob .078GB > 显示当前数据库:db > db runoob > 显示所有 ...
- ylbtech-LanguageSamples-XMLdoc
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-XMLdoc 1.A,示例(Sample) 返回顶部 “XML 文档”示例 本示例演示如 ...
- maven问题
pom.xml ... </dependencies> <repositories> <repository> <id>sf-nexus</id& ...
- Java注解教程及自定义注解
Java注解提供了关于代码的一些信息,但并不直接作用于它所注解的代码内容.在这个教程当中,我们将学习Java的注解,如何定制注解,注解的使用以及如何通过反射解析注解. Java1.5引入了注解,当前许 ...