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. Android APP安全评估工具 Drozer - 安装介绍

    一.Drozedr安装 注意事项:安装需要 JavaRuntime Environment (JRE) or Java Development Kit (JDK)环境, 没有安装的请先安装java环境 ...

  2. URAL 1097 Square Country 2 离散化

    一共才100个正方形,将所有正方形左下角和右上角的X坐标和Y坐标离散化,直接枚举新建公园的点的坐标即可. O(n^3)的时间复杂度. #include <cstdio> #include ...

  3. webServer xampp的安装及使用

    一.下载: 西装xampp1.73版本,其他版本可能会出错,为了你能顺利安装,最好用此版本: 二.安装: 1.安装路径,最好不要放到系统盘去[c:],建议非系统盘都可,尤其是早期的XAMPP版本可能默 ...

  4. Go语言学习笔记一(语法篇)

    国庆节七天假期,这段时间刚好项目那边催的不是很紧,基本上每天都是白天重构一下项目代码,晚上自己学习.(大概是因为容总那边的人都去度假了把项目进度放慢了吧.这两天“彩虹”姐姐也来凑热闹,据说还是直接从澳 ...

  5. [原]Water Water Search Problems&#39; Set~Orz【updating...】

    [HDU] [POJ] 作者:u011652573 发表于2014-4-30 10:39:04 原文链接 阅读:30 评论:0 查看评论

  6. 多个电脑共用一个ssh

    比如我们有多个设备,但不想每个设备上生成一个ssh key,然后去github或其他网站上添加,那样的话,ssh key会比较多,搞起来会比较乱,所以我们想在不同的设备上使用同一个ssh. 做法是,我 ...

  7. java生成带html样式的word文件

    参考:http://blog.csdn.net/xiexl/article/details/6652230 最近在项目中需要将通过富文本编辑器处理过的文字转换为Word,查了很久,大家通常的解决办法是 ...

  8. Projective Texture的原理与实现

    http://blog.csdn.net/xukunn1226/article/details/775644 Projective Texture是比较常见的一种技术,实现起来代码也就区区的不过百行, ...

  9. hdu 5091(线段树+扫描线)

    上海邀请赛的一道题目,看比赛时很多队伍水过去了,当时还想了好久却没有发现这题有什么水题的性质,原来是道成题. 最近学习了下线段树扫描线才发现确实是挺水的一道题. hdu5091 #include &l ...

  10. Winform——计算器

    namespace 计算器2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } pr ...