代码地址:  https://github.com/newff/st-lab1

Tasks:

  1. Install Junit(4.12), Hamcrest(1.3) with Eclipse
  2. Install Eclemma with Eclipse
  3. Write a java program for the triangle problem and test the program with Junit.

a)       Description of triangle problem:

Function triangle takes three integers a,b,c which are length of triangle sides; calculates whether the triangle is equilateral(等边), isosceles(等腰), or scalene(斜角体). 

1. Install Junit(4.12), Hamcrest(1.3) with Eclipse

  To install jar package into Eclipse, we can use Maven or download the jar package the add to Eclipse. In my earlier blogs I have telled how to add jar package through Maven, in this passage I tell how to download and add directly.

  Online websites:

    Junit:         http://junit.org/junit4/

    hamcrest:  http://search.maven.org/#search|ga|1|g%3Aorg.hamcrest

  hamcrest-all.* is recommended.

  Choose the proper version and download it.

Open Eclipse, file->new->Java Project, name it "javaPro", it finished to new a new project. Then, on your "javaPro", right click->properties->Java Build Path->Libraries->Add External JARs, select the directory of your jar packages. click Apply and OK. You can see the two packages in your Referenced Library.

2. Install Eclemma with Eclipse

website:      http://www.eclemma.org/installation.html#marketplace

There are three options for us to install the Eclemma on this website, you can choose anyone of them. I chose the first one and then restart my eclipse, it was already installed.

In your "javaPro", help->Eclipse MarketPlace, search "Eclemma ",and then install. It finished to install the Eclemma.

3.Write a java program for the triangle problem and test the program with Junit. 

The java program is on the github. To test the program with junit, we have to right click the "Triangle.java", and then new->Junit Test Case,

Browse the functions that we  want to test, then finish.

Write the test function for the Triangle function, then, add some test cases, click the button to run and we will see the result.

4.Result of the test

When I test all of the four conditions, the coverage is 100%.

One error I have made in my lab is that when I judge whether it is a riangle, I wrote this"if((a+b>c && a-b<c) || (a+c>b && a-c<b) || (b+c>a && b-c<a))",this program get the wrong answer. Then I fixed the error and the program become right.

 5.Prolongation

What's more, I tried to use Parameters in my junit test.

These are my codes:

 package javaPro;

 import static org.junit.Assert.*;

 import java.util.Arrays;
import java.util.Collection; import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class)
public class TriangleTest {
private int a;
private int b;
private int c;
private int expected;
private Triangle triangle; public TriangleTest(int a, int b, int c, int expected){
this.a = a;
this.b = b;
this.c = c;
this.expected = expected;
}
@Before
public void setUp() throws Exception {
System.out.println("Before test");
triangle = new Triangle();
} @Parameters
public static Collection<Object[]>getData(){
return Arrays.asList(new Object[][]{
{1, 2, 3, 3},
{1, 1, 1, 0},
{2, 2, 3, 1},
{3, 4, 5, 2}
});
}
@Test
public void testTriangles() {
assertEquals (this.expected, triangle.triangles(a, b, c));
// assertEquals (0, triangle.triangles (1,1,1));
// assertEquals (1, triangle.triangles (2,2,3));
// assertEquals (2, triangle.triangles (3,4,5));
} }

And this is the result, I have also put my code on the Github.

I made a mistake that the name of the constructed function must be the same with the name of the class. It worked after I have fixed this mistake.

ST Lab1 junit test的更多相关文章

  1. 软件测试:lab1.Junit and Eclemma

    软件测试:lab1.Junit and Eclemma Task: Install Junit(4.12), Hamcrest(1.3) with Eclipse Install Eclemma wi ...

  2. Software Testing, Lab 1

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

  3. ST第三次作业Junit安装

    一.Junit安装 安装eclipse,右键“项目”文件——>java build path——>导入jar包junit-4.12.jar和hamcrest-all-1.3.jar包. 二 ...

  4. ST 单元测试之maven引入junit包

    按照上篇博客,已经完成了mavne以及eclipse的安装配置,新建好了一个maven项目. 接下来打开项目,双击打开pom.xml,可以看到如下所示, 点击下方的pom.xml,然后添加如下代码,即 ...

  5. 【原创】Junit4详解一:Junit总体介绍

    Junit是一个可编写重复测试的简单框架,是基于Xunit架构的单元测试框架的实例.Junit4最大的改进是大量使用注解(元数据),很多实际执行过程都在Junit的后台做完了,而且写test case ...

  6. JavaSE学习总结(十九)—— Java Web 综合应用(JSP、Servlet、IDEA、MySQL、JUnit、AJAX、JSON)

    一.使用Servlet+JDBC+MySQL+IDEA实现商品管理 1.1.创建项目 打开IntelliJ IDEA开发工具.点击Create New Project创建一个新的Web项目 选择Jav ...

  7. Junit参数化测试Spring应用Dubbo接口

    一.创建基础类. package com.tree.autotest; import org.junit.Before;import org.springframework.context.annot ...

  8. 软件测试技术第一次试验之——JUnit的安装与使用

    众所周知,在一个大型的软件项目中,测试是必不可少的.传统的测试方法往往要自己编写测试函数再结合测试用例进行验证,这样会显得比较繁琐.所以我们可以使用JUnit框架进行测试. 使用junit的好处就是这 ...

  9. 软件测试入门 1—— junit 单元测试

    一.实验主要内容: 1. 2.EclEmma安装 见: http://www.cnblogs.com/1995hxt/p/5291465.html 二.对与 Junit 安装,使用 maven管理项目 ...

随机推荐

  1. 用ant打包可运行的jar文件 (将第三方jar包放进你自己的jar包)

    http://blog.csdn.net/caiqcong/article/details/7618582 <span style="font-family:SimSun;font-s ...

  2. 微服务框架概览之 Netty

    Netty 是什么 Netty 提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序 Netty 架构图 为什么选择Netty 通过对Netty的分析,我 ...

  3. 编程练习------C/C++分别实现字符串与整数的转换

    C/C++分别实现字符串与整数的转换 前提:不使用 itoa 和 atoi. 方法一.C和C++通用的一种转换手段是: 1.整数转化为字符串:采用加'0',再逆序的办法,整数加'0'就会隐性转化成ch ...

  4. PHP反射之类的反射

    最近在琢磨如何用PHP实现站点的插件功能,需要用到反射,于是现学了一下,笔记如下: class Person { public $name = 'Lily'; public $gender = 'ma ...

  5. Jmeter生成html格式测试报告

    使用jmeter进行性能测试,运行完毕后生成html格式的测试报告,需要进行如下操作: 1.在C:\apache-jmeter-3.0\bin文件夹下的user.properties文本中添加如下信息 ...

  6. Swift 2.2 协议和代理

    一:代理 两个类之间的传值,类A调用类B的方法,类B在执行过程中遇到问题通知类A,这时候我们需要用到代理(Delegate). 比如:控制器(Controller)与控制器(Controller)之间 ...

  7. C语言 extern学习1

    没有头文件时,通过本文件内的函数声明来确定定义域,实现功能: //单文件测试 #include <stdio.h> /* 经测试,C语言环境下子函数默认是void型:所以可省略不写 为严谨 ...

  8. asp.net core mvc实现伪静态功能

    在大型网站系统中,为了提高系统访问性能,往往会把一些不经常变得内容发布成静态页,比如商城的产品详情页,新闻详情页,这些信息一旦发布后,变化的频率不会很高,如果还采用动态输出的方式进行处理的话,肯定会给 ...

  9. js设计模式--工厂模式

    工厂模式: 工厂模式的目的是为了创建对象,它经常是在类和类的方法中实现.简单的工厂模式是由一个方法来决定到底要创建哪类的实例,这些实例经常拥有相同的接口,这种模式在所实例化的类型在编译期并不确定,而是 ...

  10. C语言对齐

    "%-md":左对齐,若m比实际少时,按实际输出.(m为整数) "%md":右对齐,若m比实际少时,按实际输出. 我自己编了一个,给你看看实际效果: #incl ...