1. Install junit

a)      Download “junit.jar”

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

add  "junit.jar" file.

    

2. Install hamcrest

a)      Download “hamcrest.jar”

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

add the downloaded jar file.

    

3. Install eclemma

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

     

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”

                 

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”

    

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

   

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

      

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.

  

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

  

    and now you can see the coverage all 100%.

  

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

First step of using junit---------Software Testing Lab 1---2016.03.18的更多相关文章

  1. Software Testing, Lab 1

    1.Install Junit(4.12), Hamcrest(1.3) with Eclipse 2.Install Eclemma with Eclipse 3.Write a java prog ...

  2. Software Testing Techniques LAB 02: Selenium

    1. Installing 1. Install firefox 38.5.1 2. Install SeleniumIDE    After installing, I set the view o ...

  3. FW:Software Testing

    Software Testing Testing with a Purpose Software testing is performed to verify that the completed s ...

  4. 101+ Manual and Automation Software Testing Interview Questions and Answers

    101+ Manual and Automation Software Testing Interview Questions and Answers http://www.softwaretesti ...

  5. Exploratory Software Testing

    最近找到去年上半年看过一本关于测试方面书籍的总结笔记,一直放在我的个人U盘里,当时是用Xmind记录的,现在重新整理下分享给大家了! James A.Whittaker [美] 詹姆斯·惠特克(软件测 ...

  6. 软件测试software testing summarize

    软件测试(英语:software testing),描述一种用来促进鉴定软件的正确性.完整性.安全性和质量的过程.软件测试的经典定义是:在规定的条件下对程序进行操作,以发现程序错误,衡量软件质量,并对 ...

  7. 读书笔记-Software Testing(By Ron Patton)

    Software Testing Part I:The Big Picture 1.Software Testing Background Bug's formal definition 1.The ...

  8. software testing

    Software Testing Software testing is the process of evaluation a software item to detect differences ...

  9. 探索式软件测试—Exploratory Software Testing

    最近找到去年上半年看过一本关于测试方面书籍的总结笔记,一直放在我的个人U盘里,当时是用Xmind记录的,现在重新整理下分享给大家了! James A.Whittaker [美] 詹姆斯·惠特克(软件测 ...

  10. 《The art of software testing》的一个例子

    这几天一直在看一本书,<The art of software testing>,里面有一个例子挺有感触地,写出来和大家分享一下: [问题] 从输入对话框中读取三个整数值,这三个整数值代表 ...

随机推荐

  1. linux系统主要常见目录结构

    linux系统的一切从“/”开始,并按照文件系统目录标准FHS采用树桩结构来存放文件 并定义了每个区域的用途.

  2. struts2与struts1整合,java.lang.InstantiationException, Exception occurred during processing request: null

    做了2个action,其中一个运行没有问题,另一个报错,看下面的报错信息,再看了看struts.xml,因为没有给GetBooks这个action配置actionform,所以就导致报null.下面是 ...

  3. 怎样用delphi关闭并重新启动 explorer.exe进程

    uses Tlhelp32; function KillTask(ExeFileName:string):integer; const PROCESS_TERMINATE = $0001; var C ...

  4. YTU 2607: A代码填空题--更换火车头

    2607: A代码填空题--更换火车头 时间限制: 1 Sec  内存限制: 128 MB 提交: 91  解决: 73 题目描述 注:本题只需要提交填写部分的代码,请按照C++方式提交. 假设火车有 ...

  5. Vim 新用法

    daw , delete a word cw , delete from cursor to the end then insert mode a word 移动: f ; Aa Oo Cc Ii S ...

  6. BZOJ 2956 模积和

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2956 题意:给出n和m.计算: 思路: i64 n,m; i64 cal(i64 m,i ...

  7. Create Entity Data Model

    http://www.entityframeworktutorial.net/EntityFramework5/create-dbcontext-in-entity-framework5.aspx 官 ...

  8. [转载] 推荐的C++书籍以及阅读顺序

    2014-06-17 转载自 oiramario 的文章 推荐的C++书籍以及阅读顺序 当读者有一定c/c++基础 推荐的阅读顺序: level 1 从<<essential c++> ...

  9. cdoj 1342 郭大侠与甲铁城 树状数组+离线

    郭大侠与甲铁城 Time Limit: 1500/800MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit St ...

  10. 函数ut_malloc_low

    /**********************************************************************//** Allocates memory. @retur ...