在Hadoop中使用MRUnit进行单元测试

发表于:2013-9-09 11:00

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:逖靖寒    来源:51Testing软件测试网采编

  前提

  1. 了解JUnit4.x的使用。

  2. 了解Mock的概念在单元测试中的应用。

  3. 了解Hadoop中MapReduce的编程模型。

  如果您对Junit和Mock不了解,可以先阅读[翻译]Unit testing with JUnit 4.x and EasyMock in Eclipse - Tutorial 。

  如果您对Hadoop中MapReduce的编程模型不了解,可以先阅读Map/Reduce Tutorial 。

  介绍

  MRUnit是一款由Couldera公司开发的专门针对Hadoop中编写MapReduce单元测试的框架。

  它可以用于0.18.x版本中的经典org.apache.hadoop.mapred.*的模型,也能在0.20.x版本org.apache.hadoop.mapreduce.*的新模型中使用。

  官方的介绍如下:

  MRUnit is a unit test library designed to facilitate easy integration between your MapReduce development process and standard development and testing tools such as JUnit. MRUnit contains mock objects that behave like classes you interact with during MapReduce execution (e.g., InputSplit and OutputCollector) as well as test harness "drivers" that test your program's correctness while maintaining compliance with the MapReduce semantics. Mapper and Reducer implementations can be tested individually, as well as together to form a full MapReduce job.

  安装

  在目前Hadoop的发行版中,并没有默认包含MRUnit。你需要去Couldera公司的官网中去下载一个由他们再次发行的版本。

  推荐的版本为:hadoop-0.20.1+133.tar.gz 。

  下载这个文件后,你将在hadoop-0.20.1+133/contrib/mrunit目录中找到我们需要的jar包:hadoop-0.20.1+133-mrunit.jar。

  为了使用MRUnit,我们需要将hadoop-0.20.1+133-mrunit.jar和Junit4.x使用的jar包:junit.jar都添加到我们开发Hadoop程序项目的classpath中。

  示例

  代码是最好的文档,我们先看一个简单的map单元测试示例,代码如下:

package gpcuster.cnblogs.com;
import junit.framework.TestCase;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.lib.IdentityMapper;
import org.junit.Before;
import org.junit.Test;
import org.apache.hadoop.mrunit.MapDriver;
public class TestExample extends TestCase {
  private Mapper<Text, Text, Text, Text> mapper;
  private MapDriver<Text, Text, Text, Text> driver;
  @Before
  public void setUp() {
    mapper = new IdentityMapper<Text, Text>();
    driver = new MapDriver<Text, Text, Text, Text>(mapper);
  }
  @Test
  public void testIdentityMapper() {
    driver.withInput(new Text("foo"), new Text("bar"))
            .withOutput(new Text("foo"), new Text("bar"))
            .runTest();
  }
}

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号