您的位置:首页 > 其它

软件测试入门 1—— junit 单元测试

2016-03-18 12:00 387 查看
一、实验主要内容:

1、



2、EclEmma安装

见: /article/6289399.html

二、对与 Junit 安装,使用 maven管理项目,方便jar包的版本管理,冲突管理等等。

三、实验demo

1、方法主类:

package cn.edu.tju.scs;

/**
* Hello world!
*
*/
public class Lab1
{

/**
* 判断是否为三角形,
* 若是,是否是等腰或者等边
* @param a
* @param b
* @param c
* @return   -1 :不是; 0:普通三角形;1 等腰三角形;2:等边三角形
*/
public static int judgeTri(int a,int b,int c){
System.out.println("参数: " + a + " " + b + " " + c);
int result = -1;
if(a <=0 || b <= 0 || c<= 0){
System.out.println("return -1 --------------: 不是三角形");
return result;
}
if(a + b > c && a + c > b && b + c > a){
result = 0;
if(a == b  && a == c){
System.out.println("return 2 --------------: 等边三角形");
result = 2;
}else if(a ==b || a == c || b== c){
System.out.println("return 1 --------------: 等腰三角形");
result = 1;
}else {
System.out.println("return 0 --------------: 普通三角形");
}
}else{
System.out.println("return -1 --------------: 不是三角形");
}
return result;
}
}


2、测试类:

package cn.edu.tju.scs;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static junit.framework.Assert.assertEquals;

/**
* Unit test for simple App.
*/
@RunWith(Parameterized.class)
public class Lab1Test {
int expected = 0;
int input1 = 0;
int input2 = 0;
int input3 = 0;

/**
* 判断是否为三角形,
* 若是,是否是等腰或者等边
* @return   -1 :不是; 0:普通三角形;1 等腰三角形;2:等边三角形
*/

@Parameterized.Parameters
public static Collection<Object[]> t(){
return Arrays.asList(new Object[][]{
{-1,0,0,0},
{-1,1,3,4},
{1,3,3,2},
{2,3,3,3},
{0,4,5,3},
{-1,3,0,-4},
{0,5,7,3},
{1,3,5,5}
});
}
public Lab1Test(int expected,int input1,int input2,int input3){
this.expected = expected;
this.input1 = input1;
this.input2 = input2;
this.input3 = input3;
}

@Before
public void before(){
System.out.println("测试开始 - - - - - - - - - - - - - - - - - - - - - - - ");
}

@Test
public void testJudgeTri(){
assertEquals(expected,Lab1.judgeTri(input1,input2,input3));
}

@After
public void afeter(){
System.out.println("测试结束 - - - - - - - - - - - - - - - - - - - - - - - \n\n");
}

}


3、测试结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: