1 What is xUnit
xUnit is a test framework based on test-driven development
pythonUnit for python testing
CppUnit for C++ testing
JUnit for java testing
2. What is JUnit
Junit is a framework for testing Java code.
3 Why use JUnit
Purposeful testing helps us avoid bugs in our code.
Four junit official website
jar package download address:
Five examples
1. Create the junit4Demo project with the eclipse project.
2. Create the caculate.java program
package com.imooc.util;
publicclassCaculate{
publicint add(int a,int b){
return a+b;
}
publicint subtract(int a,int b){
return a-b;
}
publicint multiply(int a ,int b){
return a*b;
}
publicint divide(int a,int b){
return a/b;
}
}
3. Import the jar package of Junit
选择
junit4Demo,右键【Build Path】->【Add library】->【JUnit】->【Next】->【JUnit4】->【Finish】
4. Create test code
Select the com.imooc.util package, right-click [new] -> [Junit Test Case].
Name it CaculateTest.
code show as below
package com.imooc.util;
importstatic org.junit.Assert.*;
import org.junit.Test;
publicclassCaculateTest{
@Test
publicvoid add(){
assertEquals(6,newCaculate().add(3,3));
}
}
5. Execute the test
Right click【
CaculateTest.java
】->【Run as】->【Junit test】
6. Execution results