Software Testing Experiment (1) Basic Application and Learning of Junit4 Testing

1. The purpose of the experiment:

Through experiments, learn JUnit, so that students can master the basic operation of Junit, and conduct relatively simple unit tests.

2. Experimental requirements and content:

(1) Design and implement a program to find the area of ​​a triangle. The equilateral triangle is calculated by the general formula for calculating the area of ​​the triangle, and the arbitrary triangle is calculated by Heron's formula (S=√p(pa)(pb)(pc) ) where, p=(a+b+c)/2).
(2) Use Junit to test according to the written code.

3. Experimental steps

1. First of all, understand the content of the experiment, and according to the requirements of the experiment, write a program to calculate the area of ​​a triangle under myeclpise or eclpise.
2. Use the logic coverage in the white box testing technology to design test cases and form simple test case documents.
3. Use the equivalence class division in black box testing technology to design test cases and form simple test case documents.
4. Use Junit to write test classes.
5. Use the data-driven test class execution of test cases, and you can use methods such as parameterized testing and packaged testing for automated testing.
6. Analyze the test data and modify the program according to the test results.
7. To further strengthen the Junit testware during the experiment is application and knowledge learning.

Process:

1. Create a java project JUnitText
我使用的Eclipse,在左侧Package Explorer(包资源管理器)右键,新建Java Project,输入项目名,Finish即可。
2. Create the java class calTriArea
public class calTriArea {
    public boolean judge(double a,double b,double c){
        boolean flag = false;
        if(a + b > c && a + c > b && b + c > a){
            flag = true;
        }
        if(a <= 0 || b <= 0 || c <= 0){
            flag = false;
        }
        return flag;
    }
    public double cal(double a,double b,double c) {
        double area = -1.0;
        if(judge(a, b, c)){
            if(a == b && b == c){
                area = Math.sqrt(3) / 4 * a * a ; 
            }else{
                double p = (a + b + c)/2;
                area = Math.sqrt( (p * ( p - a ) * ( p - b ) * ( p - c ) ) );
            }
            return area;
        }
        return area;
    }
}
3. Import the JUnit4.jar package into the project

Right-click on the project name and select the bottom—Properties—>Java Build Path—>Libraries—>Add Libraries—>JUnit–>Next–>Finish

4. Write a JUnit4 test class

Right-click on the class to be tested, select new-Junit Text Case-Next-select the method you need to test-Finish, and then start filling in the blank test.
This test class is too redundant. To use parameterized tests, I still don’t know it very well. I will add it next time.

import static org.junit.Assert.*;

import org.junit.Test;

public class calTriAreaTest {
    private static calTriArea cal = new calTriArea();   
    @Test
    public void testJudge() {//不能构成三角形 正数 
        assertEquals(false, cal.judge(2, 1, 3));
    }
    @Test
    public void testJudge1() {//可以构成三角形
        assertEquals(true, cal.judge(3, 4, 5));
    }
    @Test
    public void testJudge2() {//不能构成三角形 负数
        assertEquals(false, cal.judge(-3, -4, -5));
    }
    @Test
    public void testCal() {//等边三角形
        assertEquals(3.89711431,cal.cal(3, 3, 3),0.00001);
    }
    @Test
    public void testCal1() {//一般三角形
        assertEquals(6.0,cal.cal(3, 4, 5),0.00001);
    }
    @Test
    public void testCal2() {//不能构造三角形  正数
        assertEquals(-1.0,cal.cal(2, 1, 3),0.00001);//浮点数比较是以差值小于某个精度表示,比较两个浮点数,要加上第三个参数,表示精度
    }
    @Test
    public void testCal3() {//不能构造三角形 负数 
        assertEquals(-1.0,cal.cal(-2, -1, -3),0.00001);
    }
}
5. Test Results

Run the test class, run the test —
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325421591&siteId=291194637