First, let's take a stupid crash tutorial and don't ask why,follow Me, let's experience the thrill of unit testing!
first a new project called junit_test We write a calculator Class, a calculator class that can simply implement subtraction, square, and root, and then unit-test these functions. This class is not perfect, we deliberately kept some bug For demonstration, these bug
PackageAndycpp;PublicClassCalculator {PrivateStaticint result;//Static variables for storing run resultsPublicvoid Add (IntN) {result=result+N }Publicvoid Substract (Intn) {result=result-1;//Bug: The correct should be result=result-n;}Publicvoid Multiply (IntN) {//This method is not yet written.}Publicvoid Divide (IntN) {result=result/N }Publicvoid Square (int n "{result=n*N;} Span style= "color: #0000ff;" >public void squareroot (int N) {for (;;); //bug: Dead loop } public void clear () {result=0; // clear 0 public int GetResult () {return
The second step is to introduce the JUNIT4 Unit test package into this project: Right-click on the item, click "Properties",
java Build path "and then to the right, select" " tag, then click " add Library ... "button, as shown in
Then select JUnit4 in the new pop-up dialog and click OK, as shown in theJUNIT4 software package is included in our project.
The third step is to generateJunit test framework: eclipse new - JUnit Test case< Span style= "font-family: Song body; font-size:14pt; " >". As shown:
In the popup dialog box, make the appropriate selections as shown in:
After clicking "Next", the system will automatically list the methods contained in your class and choose the method you want to test. In this example, we only test the "add, subtract, multiply, divide" four methods. As shown in the following:
The system then automatically generates a new class calculatortest, which contains some empty test cases. You only need to make these test cases slightly modified to use.
The complete calculatortest code is as follows:
PackageAndycpp;ImportStatic org.junit.assert.*;ImportOrg.junit.After;ImportOrg.junit.Before;ImportOrg.junit.Ignore;ImportOrg.junit.Test;ImportOrg.junit.internal.runners.TestClassRunner;ImportOrg.junit.runner.RunWith; the @RunWith (Testclassrunner.Class)PublicClasscalculatortest {PrivateStatic Calculator calculator=NewCalculator (); @BeforePublicvoid SetUp ()ThrowsException {calculator.clear ();} @AfterPublicvoid TearDown ()ThrowsException {} @Test (timeout=1000)PublicvoidTestadd () {Calculator.add (2); Calculator.add (3); Assertequals (5, Calculator.getresult ()); } @TestPublicvoid Testsubstract () {calculator.add (10); Calculator.substract (2) @Test public void Testmultiply () {} @Test (expected = ArithmeticException. classpublic void Testdivide () {Calculator.add (8
Fourth step, run the test code: After modifying the code as described above, we right-click on the Calculatortest class and select "Run as - JUnit test" to run our tests as shown in:
The results of the operation are as follows:
Progress bar is red color indicates found error, the specific test results on the progress bar indicates "a total of 4 tests, of which 1 tests were ignored, one test failed."
At this point, we have fully experienced the methods of using JUnit in Eclipse .
Unit testing with JUNIT4 in Eclipse (beginner)