"Software Testing" uses JUnit and Eclemma for unit testing and coverage testing

Source: Internet
Author: User

1. Install JUnit and hamcrest under Eclipse.

  Pre-preparation: download JUnit and hamcrest jar packages to provide github download URLs

HTTPS://Github.com/junit-team/junit/wiki/download-and-install

After downloading the two jar packages, open Eclipse, create a new Java project, right-click on the Completed project folder and select the BuildPath option.

Then, in the popup window, do the following: select "Libraries", click "Add externaljars", select the download path of the two jar packages above, select the jar package. This completes the installation.

2. Install Eclemma under Eclipse

There are many ways to install Eclemma, and here is an online one-click installation method:

Open Eclipse, select Help->eclipsemarketplace

In the window that appears, select Search, fill in the "Eclemma" in the input box, click the Search button to display the search results, click Install directly at the search results.

  

After installation, restart Eclipse, when this button appears on the toolbar, the Eclemma installation is successful.

3. Write code to complete the detection of the triangle shape

According to the experimental requirements, this experiment requires testing code for a section of the code to detect a triangle shape: Given the length of the triangle three sides, to determine whether the triangle is equilateral triangle or isosceles triangle or ordinary triangles.

Write the triangle class, there are three member variables, representing the three sides of the triangle, according to the conditions of the triangle, first to determine whether three edges can form a triangle, if not, then return an error message, indicating that the input three edges can not form a triangle, is illegal input. If the judgment indicates that the three edges can form a triangle, then the specific shape can be judged according to the judging conditions of equilateral triangle and isosceles triangle. The specific code is as follows

 Public classTri { Public intA,b,c;  PublicTriintAintBintc) {         This. a=A;  This. b=b;  This. c=C; }     PublicString judgement () {if(a+b<=c | | a+c<=b | | b+c <=a) {            return"Not a triangle"; }Else{            if(a==b&&a==c) {                return"Equilateral"; }Else if(A = = c && B! =c) {                return"Isosceles"; }Else{                return"Scalene"; }        }    }    }

After the Triangle class is written, test functions are written, given the input and expected output, and tested by JUnit.

Importorg.junit.Test;Import Staticorg.junit.assert.*;ImportOrg.junit.Before; Public classMain { PublicTri t; @Before Public voidCreate () {T=NewTri (); } @Test Public voidTest () {T=NewTri (1,1,1); Assertequals ("Equilateral", T.judgement ()); T=NewTri (1,2,2); Assertequals ("Isosceles", T.judgement ()); T=NewTri (2,1,2); Assertequals ("Isosceles", T.judgement ()); T=NewTri (all); Assertequals ("Not a triangle", T.judgement ()); T=NewTri (3,4,5); Assertequals ("Scalene", T.judgement ()); }}
4. Test results

JUnit Test

Coverage test under Eclemma (code snippet)

  

"Software Testing" uses JUnit and Eclemma for unit testing and coverage testing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.