软件测试Lab 1 Junit and Eclemma
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.
1 package T_main; 2 3 public class Tmain { 4 private static int one, two, three = 1; 5 6 public static void main(String[] args) { 7 check ( one , two , three ); 8 } 9 10 public static int check ( int a , int b , int c ){ 11 int maxn = Math.max(Math.max(a,b), c); 12 int x[]={a,b,c}; 13 int pow_sum = 0; 14 int min_sum = 0; 15 boolean flag = false; 16 for ( int i = 0 ; i < x.length ; i++ ) 17 { 18 if( x[i] != maxn || flag ){ 19 pow_sum += Math.pow(x[i], 2); 20 min_sum += x[i]; 21 } 22 if( x[i] == maxn ) flag = true; 23 } 24 //两边之和小于第三边,非法三角形 25 if( min_sum <= maxn ) return -1; 26 //等边三角形,返回1 27 else if ( a == b && b == c ) return 1; 28 //等腰三角形,返回2 29 else if( a== b || b == c || a == c ) return 2; 30 //直角三角形,返回3 31 else if( Math.pow(maxn, 2) == pow_sum ) return 3; 32 //普通三角形,返回0 33 return 0; 34 } 35 36 }
1 package T_main; 2 3 import static org.junit.jupiter.api.Assertions.*; 4 5 import org.junit.jupiter.api.Test; 6 7 class Ttest { 8 9 Tmain tmain = new Tmain(); 10 11 @Test 12 public void testCheck() { 13 int temp = tmain.check(2, 2, 5 ); 14 assertEquals( -1 , temp ); 15 temp = tmain.check(2, 2, 2); 16 assertEquals( 1 , temp ); 17 temp = tmain.check(2, 2, 3 ); 18 assertEquals( 2 , temp ); 19 temp = tmain.check(3, 4, 5); 20 assertEquals ( 3 , temp ); 21 temp = tmain.check(2, 3, 4); 22 assertEquals ( 0 , temp ); 23 } 24 }
1. The brief description that you install junit, hamcrest and eclemma.
安装JUnit和Hamcrest:在包上右键->new->JUnit Test Case
如果未安装junit将提示是否安装junit5,选择是即可,junit5自带hamcrest框架。
安装Eclemma:
Help -> Eclipse Marketplace
搜索Eclemma,点击install
2. The test result and coverage report (print screen) of your tests on triangle problem.
红色代表未执行,黄色代表条件没有完全执行,绿色代表执行过了。
增加测试用例: