测试工具之如何使用JUnit进行单元测试
白羽 2018-05-18 来源 :网络 阅读 1216 评论 0

摘要:本文将带你了解测试工具JUnit如何进行单元测试,希望对大家学测试工具有所帮助


布置JUnit环境


  对于需要使用JUnit进行单元测试的maven项目,添加对JUnit相关jar包的依赖,打开这个project的pom.xml文件,引入以下dependency


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>

  具体要引用哪个版本的JUnit,在version节点配置,scope节点配置一般情况是test,二般情况下也配置的是test,我们引用JUnit只是为了在测试阶段去使用,软件的运行其实是不需要依赖JUnit的,软件的打包部署发布,也不需要将JUnit相关的库带上。


  maven-surefire-plugin


  在surefire的眼中,它看到的JUnit只有三个版本,分别是

  · JUnit 3.8.x

  · JUnit 4.x (surefire提供串行执行的provider)

  · JUnit 4.7 (surefire提供支持并行执行的provider)

  surefire使用哪种provider,要看项目中依赖的JUnit的项目,以及pom.xml中对surefire插件的配置,比如说是否配置了并行执行(parellel),选择的策略有以下伪代码演示:

  if the JUnit version in the project >= 4.7 and the parallel attribute has ANY value

  use junit47 provider

  if JUnit >= 4.0 is present

  use junit4 provider

  else

  use junit3.8.1

  一般为了避免不必要的奇奇怪怪的麻烦,都直接指定surefire的provider,前提是要提前知道使用的JUnit版本


<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.19.1</version>
</dependency>
</dependencies>
</plugin>

   

  进行测试


  首先编写一个待测试的类

package io.beansoft.junit.practice;
/**
*
*
* @author beanlam
* @date 2016年12月8日 下午4:19:19
* @version 1.0
*
*/
public class Calculator {
public int evaluate(String expression) {
int sum = 0;
for (String summand : expression.split("\\+"))
sum += Integer.valueOf(summand);
return sum;
}
}

   

  然后在src/test/java下编写测试类

package io.beansoft.junit.practice;
import org.junit.Assert;
import org.junit.Test;
/**
*
*
* @author beanlam
* @date 2016年12月9日 下午12:02:11
* @version 1.0
*
*/
public class CalculatorTest {
@Test
public void evaluatesExpression() {
Calculator calculator = new Calculator();
int sum = calculator.evaluate("1+2+3");
Assert.assertEquals(6, sum);
}
}

   

  邮件点击项目,选择run as,选择maven test,运行结果如下所示

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building junit-practice 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ junit-practice ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\eclipse-neon\workspace\junit-practice\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ junit-practice ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ junit-practice ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\eclipse-neon\workspace\junit-practice\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ junit-practice ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ junit-practice ---
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running io.beansoft.junit.practice.CalculatorTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - in io.beansoft.junit.practice.CalculatorTest
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.066 s
[INFO] Finished at: 2016-12-09T12:08:06+08:00
[INFO] Final Memory: 10M/219M
[INFO] ------------------------------------------------------------------------

   

  可以看到测试案例通过

  本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标软件测试之测试工具频道!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程