软件测试之实验一

1. First, I install three tools of JUnit and coverage tool.

  I build the path of the two jar files to my lab1 project according to the PPT.

And I use the online url to install the eclemma.

In the work with blank, I input the website of eclemma and choose the software to install, it just took me several minutes and was pretty convenient.

 2. I write the triangle class to judge wether it fuifull the conditions.

package stLab1;
public class riangle {    
    public String whichTri(int a, int b, int c){                
        int[] list = new int[]{a, b, c};
        for( int i = 0; i < 3; i++ ){
            for( int j = i; j < 3; j++ ){
                if( list[i] < list[j]){
                    int temp = list[i];
                    list[i] = list[j];
                    list[j] = temp;
                }
            }
        }
        a = list[0];
        b = list[1];
        c = list[2];
        if( c <= 0 || b + c <= a || a - c >= b || a - b >= c )
            return "noTri";        
        else if( a == b && b == c)
            return "eTri";
        else if( a == b || a == c || b ==c )
            return "iTri";
        else
            return "sTri";        
    }    
}

  And then I write a test class for parameterized test.

package stLab1;
import java.util.Arrays;
import java.util.Collection;

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

import stLab1.riangle;
import static org.junit.Assert.*;

public class TestStLab1 {
	@RunWith(Parameterized.class)
	public static class TestRiangle{
		private int a;
		private int b;
		private int c;
		private String expected;
		private riangle tri = null;
	
		public TestRiangle(int a,int b,int c, String expected){
			this.a = a;
			this.b = b;
			this.c = c;
			this.expected = expected;
		}
		
		@Before
		public void setUp(){
			tri = new riangle();
		}
		@Parameters
		public static Collection<Object[]> getData(){
			return Arrays.asList(new Object[][]{
					{1,1,1,"eTri"},
					{2,3,5,"noTri"},
					{3,5,3,"iTri"},
					{3,5,4,"sTri"},
					{-2,3,5,"noTri"}
			});
		}
		@Test
		public void testWhichTri() {
			assertEquals(this.expected, tri.whichTri(a, b, c));
		}		
	}
}

  

3.  Finally, I run it with five test cases and use the JUnit test method and coverage last lanunched. It passed the test with all five test cases.

And the coverage shows almost all the lines have been tested.

4. It's awesome to use these test tools to find faults in our codes. And with parameterized class I can test with many cases with only once running.

Also, I meet a problem that the "JUnit couldn't......", finally I set the test folder to the source folder and solve it.

原文地址:https://www.cnblogs.com/GameChina/p/5292368.html