当前位置:网站首页>Daily tests

Daily tests

2022-06-26 04:05:00 Code Xiaoyou

Classification of tests

1. White box testing

        Need to write code , Focus on the specific implementation process of the program

2. Black box testing

       Don't write code , Enter value for , See if the program can output the expected value

White box testing :

Junit Use

1. Define a test class ( The test case )

    Suggest :

       Test class name : The name of the class being tested Test 

       Package name :xxx.xxx.xx.test

2. Define test methods : Can run independently

   Suggest :

       Method name :test Test method name

       Return value :void

       List of parameters : Empty ginseng

3. Add to the method @Test

4. Import junit Depend on the environment

The verdict :

Red : Failure

green : success

Generally, we will use the punctuation operation to process the results

Assert.assertEquals( Expected results , Result of operation )

@Before:

   Modified methods are automatically executed before testing methods

@After:

   The modified method is automatically executed after the test method

1. Defined Calculator class

package cn.itcast.junit;

/**
 *  Calculator class 
 */
public class Calculator {

    /**
     *  Add 
     * @param a
     * @param b
     * @return
     */
    public int add(int a,int b){
        return a+b;
    }

    /**
     *  Subtraction 
     * @param a
     * @param b
     * @return
     */
    public int sub(int a,int b){
        return a-b;
    }
}

2. Defined test class

package test;


import cn.itcast.junit.Calculator;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 *  The test method 
 */
public class Calculatortest {
    /**
     *  Initialization method 
     *  Application for resources , All test methods are executed before execution 
     */
    @Before
    public void init(){
    }

    /**
     *  Resource release method 
     *  After all test methods are executed , The method is automatically executed 
     */
    @After
    public void close(){
    }


    @Test
    public void testadd(){

        Calculator c =new Calculator();

       int result =c.add(1,2);

        System.out.println(result);
        // Punctuation    Set a result value and compare it with the result value of the program 
        Assert.assertEquals(3,result);
    }

    @Test
    public void testsub(){
        Calculator c =new Calculator();

        int result =c.sub(1,2);

        System.out.println(result);

        Assert.assertEquals(3,result);
    }

}

原网站

版权声明
本文为[Code Xiaoyou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180540095262.html