First step of using junit---------Software Testing Lab 1---2016.03.18

时间:2022-04-03 10:49:31

1. Install junit

a)      Download “junit.jar”

b)      In eclipse, Windows->Preferences->Java->Build Path->Classpath variables->New,

add  "junit.jar" file.

    First step of using junit---------Software Testing Lab 1---2016.03.18

2. Install hamcrest

a)      Download “hamcrest.jar”

b)      In eclipse, Windows->Preferences->Java->Build Path->Classpath variables->New,

add the downloaded jar file.

    First step of using junit---------Software Testing Lab 1---2016.03.18

3. Install eclemma

a)      In eclipse, Help->Eclipse Marketplace, find eclemma in the market and install it

     First step of using junit---------Software Testing Lab 1---2016.03.18

4. Build a new project

a)      Build a new project named “TestingLab1”

b)      Create a new class under “src” to write our source code---TestingLab1/src/cn/tju/st/Cal.java

c)      Create a new folder under this project to write the testing code---TestingLab1/test/cn/tju.st/test/Testing.java

d)      Import “junit.jar” and “hamrcrest” to the project.

i.          Click the right mouse button at the project and select “Propertise”.

ii.          Propertise->Java Build Path->Add Variables

iii.          Select “junit” and “hamrcrest”and click “OK”

                 First step of using junit---------Software Testing Lab 1---2016.03.18

5. Write the source code in Cal.java

package cn.tju.st;
public class Cal{
//calculate the triangle's type
public int triangle(int a, int b, int c){
if(a == b || b ==c || c == a){
if(a == b)
return 0; //equilateral
else
return 1; //isosceles
}
return 2; //scalene
}
}

6. Write the test code in Testing.java

a)      Use org.junit.Assert.assertEquals to test the source code with an expected result.

b)      Write “@Test” before the test function

package cn.tju.st.test;

import cn.tju.st.Cal;
import org.junit.*;
import static org.junit.Assert.*; public class Testing{
@Test
public void testEquilateral(){
assertEquals(0, new Cal().triangle(2, 2, 2));
} @Test
public void testIsosceles(){
assertEquals(1, new Cal().triangle(1, 2, 2));
assertEquals(1, new Cal().triangle(2, 1, 2));
} @Test
public void testScalene(){
assertEquals(2, new Cal().triangle(2, 3, 4));
}
}

7. Testing the source

a)      Click the right mouse button at the project and select “Coverage->Junit Test”

    First step of using junit---------Software Testing Lab 1---2016.03.18

8. Results

a)      When the code is executed, it becomes green

b)      When the code is not executed, it becomes red

c)      When the code is executed partly, it becomes yellow

d)      When we test by “testEquilateral()”,

@Test
public void testEquilateral(){
assertEquals(0, new Cal().triangle(2, 2, 2));
}

  In "Cal.java"

  i)      the first“if” just execute “a==b”, it becomes yellow.

  j)      the second “if” execute “a==b” and then “return ture”, but it doesn’t execute the other case(a !=b), it becomes yellow. If and only if        the“if”executes all the conditions(when it’s ture and when it’s false), it becomes green.

  k)     the others becomes red because of no executing

  First step of using junit---------Software Testing Lab 1---2016.03.18 

  l)      now you can see the coverage and it is not all 100%

      First step of using junit---------Software Testing Lab 1---2016.03.18

e)      When we test by “testEquilateral()” and “testIsosceles()” at the same time,

the second “if” executes “a==b”, get the true and false with different test case, the it becomes green.

  First step of using junit---------Software Testing Lab 1---2016.03.18

f)      When we test all 3 functions, the code becomes green, because all conditions have been concerned.

  First step of using junit---------Software Testing Lab 1---2016.03.18

    and now you can see the coverage all 100%.

  First step of using junit---------Software Testing Lab 1---2016.03.18

sourcecode link: https://github.com/xuexcy/SoftwareTestingLab1