当前位置:网站首页>JUnit unit test

JUnit unit test

2022-06-26 16:33:00 &volume

Catalog

1. Test categories :

(1) Black box testing :

(2) White box testing :

2.Junit Use :

(1)@Test Use

1. Define a class

  2. Define another test class

3. Assertion operations

(2)@Before and @After

[email protected]

[email protected]

3. Code testing

4. test result

3.Junit summary  


1. Test categories :

(1) Black box testing :

No need to write code , Enter value for , See if the program outputs the expected value

Most of them are using black box testing , Is to test according to the code you write

(2) White box testing :

Need to write code , Pay attention to the specific execution process of the program

2.Junit Use :

Junit It's a white box test

(1)@Test Use

1. Define a class

First define the method in the class

package cn.junit;
/*
*  Calculator class 
* */
public class Calcuator {
/*
*  Add 
*/
   public int add(int a,int b)
   {
        return a+b;
   }
   /*
    Subtraction 
    */
   public int sub(int a,int b)
   {
       return a-b;
   }
}

  2. Define another test class

package cn.test;


import cn.junit.Calcuator;
import org.junit.Test;

public class CalculatorTest {
    /*
     test add Method 
     */
        @Test
    public void testAdd()
    {
        // Create calculator objects 
        Calcuator c=new Calcuator();
        // Calling method 
        int ad = c.add(1, 2);
        System.out.println(ad);
    }


}

Just added @Test    Because if there is no guide bag, it will become popular

  Click on the small light bulb next to it

Will automatically guide the package

3. Assertion operations

  We often don't output them during the test process , And will assert

Assert.assertEquals( Assertion value , Output value );
package cn.test;


import cn.junit.Calcuator;
import org.junit.Assert;
import org.junit.Test;

public class CalculatorTest {
    /*
     test add Method 
     */
        @Test
    public void testAdd()
    {
        // Create calculator objects 
        Calcuator c=new Calcuator();
        // Calling method 
        int ad = c.add(1, 2);
        Assert.assertEquals(3,ad);
        // System.out.println(ad);
    }


}

If the two values are different , Will pop up and make mistakes

Example of assertion failure :

Example of assertion success :

(2)@Before and @After

[email protected]

@Before To keep the code simple , De duplication operation

@Before At every @Test Block the code that must be executed before annotation , Conduct @Before annotation

Comments will be made in @Test Execute before

[email protected]

@After Existence and @Before It's the same , To keep the code simple , To repeat the operation

@After Is at the end of each @Test Block the code that will be executed after annotation , Conduct @After annotation

Comments will be made in @Test Automatically execute after annotation

3. Code testing

package cn.test;


import cn.junit.Calcuator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CalculatorTest {
  
    @Before
    public void testinit()
    {
        System.out.println("init...");
    }
    @After
    public void close()
    {
        System.out.println("close.....");
    }
    /*
       test add Method 
       */
        @Test
    public void testAdd()
    {
        // Create calculator objects 
        Calcuator c=new Calcuator();
        // Calling method 
        int ad = c.add(1, 2);
        //Assert.assertEquals(3,ad);
        System.out.println("testadd In progress ");
        //  System.out.println(ad);
    }
    /*
    *  test sub Method 
    * */
    @Test
    public void testsub()
    {
        Calcuator c=new Calcuator();
        int sub = c.sub(1, 2);
        System.out.println("testsub In progress ");
        //System.out.println(sub);
    }


}

4. test result

(1) test add Method

 (2) test sub Method

3.Junit summary  

(1). When defining a test method, the return value should be void type

(2). The parameter list is empty

(3). The verdict     If red appears, the test fails

                      If green appears, the test is successful

(4). Generally, we use assertion operations to process the results

           Assert.assertEquals( Assertion value , Output value );

(5)[email protected] Decorating methods are executed before testing methods

(6)[email protected] The decorated method will be executed after the test method is executed ( Whether the test is successful or not, it will be executed );

原网站

版权声明
本文为[&volume]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261626397666.html