Software Testing, Lab 1

时间:2023-03-10 01:30:45
Software Testing, Lab 1

1、Install Junit(4.12), Hamcrest(1.3) with Eclipse

Software Testing, Lab 1

2、Install Eclemma with Eclipse

Software Testing, Lab 1

Software Testing, Lab 1

3、Write a java program for the triangle problem and test the program with Junit.

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.

Triangle.java:

 package cn.st.lab1;

 public class Triangle {
public static boolean isTriangle(int a, int b, int c) {
if(a+b>c && a+c>b && b+c>a && Math.abs(a-b)<c &&
Math.abs(a-c)<b && Math.abs(b-c)<a && a>0 && b>0 && c>0) {
return true;
}
else {
return false;
}
}
public static boolean isEquilateral(int a, int b, int c) {
if(isTriangle(a,b,c)) {
if(a == b && a == c) {
return true;
}
}
return false;
}
public static boolean isIsosceles(int a, int b, int c) {
if(isTriangle(a,b,c)) {
if(a == b || a == c || b == c) {
return true;
}
}
return false;
} public static void Triangle (int a, int b, int c) {
if(isTriangle(a,b,c)) { //输出以下内容,则为三角形
if(isEquilateral(a,b,c)) {
System.out.println("Equilateral");
} //如果是等边,只输出等边,不输出等腰
else if(isIsosceles(a,b,c)) {
System.out.println("Isosceles");
}
else {
System.out.println("Scalene");
}
}
else {
System.out.println("NotATriangle");
}
}

testTriangle.java:

 package cn.st.lab1;

 import static org.junit.Assert.*;

 import org.junit.*;

 public class testTriangle {

     Triangle t;
@Before
public void setUp(){
t = new Triangle();
} @Test
public void testIsEquilateral() {
assertEquals("testIsEquilateral", true, t.isEquilateral(6,6,6));
}
@Test
public void testIsIsosceles() {
assertEquals("testIsIsosceles", true, t.isIsosceles(5,6,6));
}
@Test
public void testIsTriangle() {
assertEquals("testIsTriangle", true, t.isTriangle(5,6,7));
}
@Test
public void testTriangle() {
t.Triangle(3, 4, 5);
t.Triangle(3, 4, 4);
t.Triangle(4, 4, 4);
t.Triangle(1, 1, 5);
}
}

实验结果:

Software Testing, Lab 1

Software Testing, Lab 1

Software Testing, Lab 1

代码请见github:https://github.com/fogmisty/SoftwareTest