Clover has the following advantages:
- Quickly and accurately detects whether the test overwrites all paths in the code
- It can be integrated into multiple Ides, such as Eclipse, NetBeans, and jBuilder.
- Statistics and analysis results can be exported in multiple formats, such as PDF and HTML.
The following describes how to install the Clover plug-in Eclipse to measure the code test coverage rate.
1. Install the Clover plug-in
Open Help-> Install New Software... in the top menu of Eclipse, and click Add on the right in the pop-up window to Add the network download address of the Clover plug-in.
After you click OK, the system will find the Clover plug-in on the internet. After the result is displayed, check Clover 3.1.12 and click Next to perform the normal installation process. You need to download the Clover plug-in during the installation process, it may be slow.
2. Enter the License Key
Clover is not a free software. We have a 30-day free trial. You can register at http://my.atlassian.com/and obtain the license Key.
- Open Preferences-> Clover-> License
- Click the Paste button to Paste the License Key, and then OK
3. Enable Clover in Java
Suppose we already have a project named CloverTest, right-click the project name, and find the Clover option, Enable.
Then there will be four Clover tabs on the interface.
- Coverage Explorer
- Test Run Explorer
- Test Contribtions
- Clover Dashboard
4. Compile the jUnit test code
Calculator. java
Package com. raysmond; public class Calculator {public int add (int a, int B) {return a + B;} public int subtract (int a, int B) {return a-B ;} public int multiply (int a, int B) {return a * B;} public int divide (int a, int B) {return a/B;} public void unCoveredMethod () {// do nothingSystem. out. println ("uncovered method... ");}}
TestCalculator. java
Package com. raysmond. test; import static org. junit. assert. *; import org. junit. test; import com. raysmond. calculator; public class TestCalculator {Calculator calc = new Calculator (); @ Testpublic void testAdd () {assertEquals (10, calc. add (4, 6) ;}@ Testpublic void testSubtract () {assertEquals (-2, calc. subtract (4, 6) ;}@ Testpublic void testMultiply () {assertEquals (24, calc. multiply (4, 6);} @ Testpublic void testDivide () {// The divide method is correct. In fact, there is a problem with the test code. divide (40, 5 ));}}
On the interface, we can see that the background of some methods is red, which means the code is not covered in the Clover test, because we have not run the test code yet.
5. Run the test and view the Clover statistics and analysis results.
In the running result, we can see the jUnit test statistics and detailed Clover statistics.
On the editing page, the code covered by the test and passed the test is marked in green, while the code that has not passed the test is marked in red. Meaningless code, of course, the Clover will cleverly ignore it.