对下面三个类进行单元测试 ,组成套件测试。

public class Calculate {
public int add(int a, int b) {
return a + b;
} public int sub(int a, int b) {
return a - b;
}
}
public class Car {
public int getWheels() {
return 4;
}
}
public class  Rectangle{
public int getArea(int width,int height){
return width*height;
}
}

测试代码:建议把测试代码放在test源文件中

public class CalculateTest {
Calculate calculate; @Before
public void setUp() throws Exception {
calculate = new Calculate();
} @Test
public void testAdd() {
int result = calculate.add(12, 12);
assertEquals(24, result);
} @Test
public void testSub() {
int result = calculate.sub(12, 12);
assertEquals(0, result);
} }
public class CarTest {
Car car; @Before
public void setUp() throws Exception {
car = new Car();
} @Test
public void testGetWheels() {
int result = car.getWheels();
assertEquals(4, result);
} }
public class RectangleTest {
Rectangle rectangle;
@Before
public void setUp() throws Exception {
rectangle=new Rectangle();
} @Test
public void testGetArea() {
int result = rectangle.getArea(12, 2);
assertEquals(24, result);
}
}

测试套件代码:

@RunWith(Suite.class)
@SuiteClasses({ CalculateTest.class, CarTest.class, RectangleTest.class })
public class AllTests { }

请用下面的参数对Calculate类的add方法进行参数化的测试

{2,1,1},

{2,0,2},

{0,2,-2},

@RunWith(Parameterized.class)
public class CalculateTest2 {
Calculate calculate;
private int input1;
private int input2;
private int expected;
@Parameters
public static Collection prepareData() {
Object[][] object = { { -1, -2, -3 }, { 0, 2, 2 }, { -1, 1, 0 },
{ 1, 2, 3 } };
return Arrays.asList(object);
} public CalculateTest2(int input1, int input2, int expected) {
this.input1 = input1;
this.input2 = input2;
this.expected = expected;
} @Before
public void setUp() throws Exception {
calculate = new Calculate();
} @Test
public void testAdd() {
int result = calculate.add(input1, input2);
assertEquals(expected, result);
}
}

Junit 4.x 单元测试,参数化测试,套件测试 实例的更多相关文章

  1. junit高级篇(参数化、打包测试)-实例代码

    工程目录: 参数化测试,SquareTest.java: import static org.junit.Assert.*; import java.util.Arrays; import java. ...

  2. selenium测试套件

    1.测试套件测试套件,简单理解就是讲多个用例,装在一个容器里来同时执行完成. 2.测试套件分析 #coding=utf-8 import unittestimport BaiDuSearch,BaiD ...

  3. junit测试套件

    在实际项目中,随着项目进度的开展,单元测试类会越来越多,可是直到现在我们还只会一个一个的单独运行测试类,这在实际项目实践中肯定是不可行的.为了解决这个问题,JUnit 提供了一种批量运行测试类的方法, ...

  4. JUnit套件测试实例

    “套件测试”是指捆绑了几个单元测试用例并运行起来.在JUnit中,@RunWith 和 @Suite 这两个注解是用来运行套件测试. 下面的例子演示这两个单元测试:JunitTest1 和 Junit ...

  5. junit 测试套件Suite

    junit测试套件,就是可以运行一个测试类使得一个或一些测试类被junit测试运行 见代码: 测试套件类: import org.junit.runner.RunWith; import org.ju ...

  6. unittest单元测试框架之测试套件(三)

    1.测试套件(注意:测试用例先添加先执行,后添加后执行,由此组织与设定测试用例的执行顺序) addTests:添加多个测试用例 addTest:添加单个测试用例 import unittest fro ...

  7. 安卓CTS官方文档之兼容性测试套件简介-attach

    官方英文文档原文:https://source.android.com/compatibility/cts-intro.html Compatibility Test Suite  兼容性测试套件 H ...

  8. 安卓CTS官方文档之兼容性测试套件简介

    官方英文文档原文:https://source.android.com/compatibility/cts-intro.html Compatibility Test Suite 兼容性测试套件 Ho ...

  9. Spock测试套件入门

    目录 Spock测试套件 核心概念 整体认识 前置.后置 同junit的类比 Feature 方法 blocks 典型的用法 异常condition then和expect的区别 cleanup bl ...

随机推荐

  1. Python标准库:内置函数type(object)

    type(object) type(name, bases, dict) 本函数是返回对象的类型对象.仅仅有一个參数object时,直接返回对象的类型对象.假设仅仅是想推断一个对象是否属于某一个类的对 ...

  2. ASP.NET MVC 基于页面的权限管理

    菜单表 namespace AspNetMvcAuthDemo1.Models { public class PermissionItem { public int ID { set; get; } ...

  3. 极域电子教室卸载或安装软件后windows7无法启用触摸板、键盘

    我今天在win7上装了个极域电子教室,卸载后重启触摸板,键盘都不能用了?连口令都是用屏幕键盘来输入的.进去后看设备管理器,键盘和触摸板,前面都有黄色的告警,而且就是出现了鼠标代码为10的情况?不过吧鼠 ...

  4. Android 演示 Android ListView 和 github XListView(3-3)

    本文内容 环境 项目结构 演示 1:简单 XListView 演示 2:XListView + Fragment 演示 3:XListView + ViewPager + Fragment 本文三个演 ...

  5. 【转】Spring MVC处理静态资源

    优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...

  6. iOS 获取设备型号 ip6更新

    //获得设备型号 + (NSString *)getCurrentDeviceModel:(UIViewController *)controller { ]; size_t len; char *m ...

  7. message sent to deallocated instance

    在XCode的以前版本中,如果遇到了 [代码]c#/cpp/oc代码: 1 message sent to deallocated instance 0x6d564f0 我们可以使用info mall ...

  8. 带你走进EJB--将EJB发布为Webservice(1)

    Web service是一个平台独立,松耦合基于可编程的web的应用程序,可使用开放的XML标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布式的互操作的应用程序. 简单说Web servi ...

  9. 如何导入外部Git仓库到中国源代码托管平台(Git@OSC)

    git clone --bare http://git.rcrtm.com/git/dianli.git git clone --mirror https://git.oschina.net/cand ...

  10. Java本地运行中文正常,部署到Weblogic中文乱码

    在使用一个加密解密工具类的时候,在本地Main方法中运行正常,不会出现中文乱码,将其部署到Weblogic之后,控制台,Servlet中中出现中文乱码. 在Main方法运行时获取本地编码方式为UTF8 ...