测试===JUnit单元测试

测试

    • 一,测试分类
    • 二,单元测试Junit,你以为的junit只是@Test注解吗,shallow..
      • 优点:
      • 规范:
      • 断言:
      • 案例demo:
        • junit test case测试类创建,执行测试,结果反馈
        • junit test suite 套娃测试,suite套suite,suite套case
        • 当参数和结果有冲突时,测试具体某个方法

一,测试分类

测试细致分类:

  1. 白盒测试 具体的跑代码,结合功能。
  2. 黑盒测试,测试人员用,从界面来,看界面,功能有没达到要求,需要写文档,每个细节部分要记录到。
  3. 交叉测试,开发人员开发时,相互测试对方的功能。

功能测试:
1. 包括白盒,黑盒
2. 准备测试数据
3. 多环境测试:(测试环境(局域网数据),预发布环境(外网数据),正式环境)

  1. 相同操作系统、相同版本、相同的软件环境(运行环境、代码、jdk、tomcat、mysql…)
  2. 数据库数据不同(预发布环境的数据一般取是最近正式环境的数据)

自动化测试
使用工具,来测试产品。

性能测试(jmeter)、压力测试
响应速度、主要是模拟高并发场景

编写测试报告
各个具体业务流程,截图,具体时间,哪些地方有bug,哪些error,哪些failure记录下来!

bug跟踪系统
用来记录并跟踪bug,当前bug的数量,当前bug经过多长时间才被解决掉,等… 是一套这样的系统,有第三方的,也有公司自己研发的。

对bug数据进行统计,分析,解决。

二,单元测试Junit,你以为的junit只是@Test注解吗,shallow…

优点:

优点:junit包括junit case和junit suite。能够一次性的测试多个方法,或者多个单元测试类,并设置预期的结果。运行的结果是测试run了多少个方法,哪些error, 哪些failure了。

规范:

1.测试方法上必须使用@Test进行修饰
2.测试方法必须使用public void进行修饰,不能带任何的参数
3.新建一个源代码目录用来存放测试代码
4.测试类的包应该和被测试类保持一致
5.测试单元中的每个方法必须独立测试,测试方法间不能有任何的依赖
6.测试类使用Test作为类的后缀
7.测试方法使用test作为方法名的前缀

断言:

测试结果与设置预期的结果对比。

	
断言
//查看两个数组是否相等。
assertArrayEquals(expecteds, actuals)
//查看两个对象是否相等。类似于字符串比较使用的equals()方法
assertEquals(expected, actual)
//查看两个对象是否不相等。
assertNotEquals(first, second)
//查看对象是否为空。
assertNull(object)
//查看对象是否不为空。
assertNotNull(object)
//查看两个对象的引用是否相等。类似于使用“==”比较两个对象
assertSame(expected, actual)
//查看两个对象的引用是否不相等。类似于使用“!=”比较两个对象
assertNotSame(unexpected, actual)
//查看运行结果是否为true。
assertTrue(condition)
//查看运行结果是否为false。	
assertFalse(condition)
//查看实际值是否满足指定的条件
assertThat(actual, matcher)
fail()	让测试失败

案例demo:

junit test case测试类创建,执行测试,结果反馈

这里使用eclipse,java8

  1. 新建普通Java ee项目,并导入junit测试包,build path
  2. 导入



开始编写德莫,求和,除法

package cn.bitqian.demo;/*** @author echo lovely* @date 2020年11月13日 下午6:55:22*/public class MyMath {// sumpublic int add(int a, int b) {return a + b;}// dividepublic int division(int a, int b) {if (b == 0)return b;return a / b;}}

创建junit测试类,包名和src下面的一样,类名在原类名上+Test



上面的下一步

package cn.bitqian.demo;// 静态导入类,可直接用里面的静态方法
import static org.junit.Assert.*;import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;/*** junit test class* @author echo lovely* @date 2020年11月13日 下午6:59:03*/public class MyMathTest {private MyMath math = new MyMath();/*** 1. 测试方法 无参无返* 2. 方法前面加上@Test注解* * 运行测试方法,选中方法,右键run as Junit test 或者 debug as Junit test* 直接右键,会运行所有有@Test注解的方法* Runs 测试了几个方法* Errors 测试方法有问题* Failures 测试方法没达到预期的要求*/// 类加载时执行@BeforeClasspublic static void beforeClass() {System.out.println("init...");}// 每个有@Test方法执行前执行@Beforepublic void before() {// 如获取mybatis的session工厂System.out.println("method before...");}// 每个方法执行之后执行@Afterpublic void after() {// 如关闭mybatis的session工厂System.out.println("method after...");}// 类所有的方法执行完后执行@AfterClasspublic static void afterClass() {System.out.println("all is done...");}// 1秒 内必须出结果,否则测试失败@Test(timeout=1000)public void testAdd() {System.out.println("测试了math类的加法");// 预测值int expected = 2 + 3;// 实际值int actual = math.add(2, 3);// 断言Assert.assertEquals(expected, actual);}@Testpublic void testDivision() {System.out.println("测试了math类的除法");// Errorsint expected = 5 / 0;int actual = math.division(5, 2);assertEquals(expected, actual);}}

右键运行测试

控制台

junit test suite 套娃测试,suite套suite,suite套case

这个能一次性的测试更多方法。


结构

package cn.bitqian.suite;import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;import cn.bitqian.demo.MyMathTest;/*** suite 测试套件 套娃* 测试套件就是组织测试类一起运行* 写一个测试套件的入口类,这个类不包含其他的方法* @author echo lovely* @date 2020年11月13日 下午7:16:57*/// 运行器
@RunWith(Suite.class)
// 哪些测试类要包含, 会运行对应类的内容
@SuiteClasses({MyMathTest.class})
public class MyMathTestSuite {}
package cn.bitqian.suite;import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;import cn.bitqian.demo.MyMathTest2;/*** 测试套件2  套测试类 MyMathTest2* @author echo lovely* @date 2020年11月13日 下午7:16:57*/@RunWith(Suite.class)
@SuiteClasses({MyMathTest2.class})
public class MyMathTestSuite2 {}
package cn.bitqian.suite;import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;/*** 总套件,套娃,套两个Suite* @author echo lovely* @date 2020年11月13日 下午7:24:08*/@RunWith(Suite.class)
@SuiteClasses({ MyMathTestSuite.class, MyMathTestSuite2.class })
public class AllTests {}

运行 AllTests
在这里插入图片描述

当参数和结果有冲突时,测试具体某个方法

package cn.bitqian.demo;import java.util.Arrays;
import java.util.Collection;import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;/*** 测试MyMath类中的add方法* @author echo lovely* @date 2020年11月13日 下午8:08:17*/@RunWith(Parameterized.class)
public class MyMathAddTest {// 预期值int excepted = 0;// 参数1int input1 = 0;// 参数2int input2 = 0;public MyMathAddTest(int excepted, int input1, int input2) {super();this.excepted = excepted;this.input1 = input1;this.input2 = input2;}@Parameterspublic static Collection<Object[]> t(){return Arrays.asList(new Object[][]{{4,2,2},{11,9,2},{8,6,2},{1,-6,7}//   res v1 v2});}@Testpublic void testAdd(){MyMath myMath = new MyMath();Assert.assertEquals(this.excepted,myMath.add(this.input1, this.input2));}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/420524.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

iOS IAP教程

1. 创建应用首先进入iTunes Connect然后按下 Manage Your Applications接下来按下Add New Applicationbutton创建应用2. 在应用中创建IAP创建应用之后&#xff0c;在Manage Your Applications中点应用的图示&#xff0c;进入应用就会看到上图画面点击Manage In App Purchases就…

mybatis plus 新增,修改

simple curd1. mybatis-plus2. 以用户表为例&#xff0c;德莫1. mybatis-plus mybatis的升级版&#xff0c;curd的奇淫技巧。 MyBatis-Plus&#xff08;简称 MP&#xff09;是一个 MyBatis 的增强工具&#xff0c;在 MyBatis 的基础上只做增强不做改变&#xff0c;为简化开发、…

Linux中fork的秘密

linux中fork&#xff08;&#xff09;函数详解 一、fork入门知识 一个进程&#xff0c;包括代码、数据和分配给进程的资源。fork&#xff08;&#xff09;函数通过系统调用创建一个与原来进程几乎完全相同的进程&#xff0c;也就是两个进程可以做完全相同的事&#xff0c;但如果…

mybatis-plus 使用乐观锁修改

title乐观锁与悲观锁解决方案code测试乐观锁与悲观锁 乐观锁&#xff1a;十分乐观&#xff0c;总是认为不会出现问题&#xff0c;无论干什么&#xff0c;都不会去上锁。如果出现了问题&#xff0c;就再次更新值测试。 悲观锁&#xff1a;十分悲观&#xff0c;认为总是出现问题…

EasyUI 在aspx页面显示高度不正常解决办法

<body class"easyui-layout"><form id"form1" runat"server"><table id"dg" class"easyui-datagrid"></table></form> </body> </html>这样写的时候&#xff0c;datagrid显示就不…

WPF中的动画——(四)缓动函数

缓动函数可以通过一系列公式模拟一些物理效果&#xff0c;如实地弹跳或其行为如同在弹簧上一样。它们一般应用在From/To/By动画上&#xff0c;可以使得其动画更加平滑。 var widthAnimation new DoubleAnimation() { From 0, To 320, Duration Tim…

mybatis-plus 查询,删除

title查询 单值&#xff0c;多个主键&#xff0c;条件分页查询物理删除&#xff0c;逻辑删除mybatis-plus 新增&#xff0c;修改查询 单值&#xff0c;多个主键&#xff0c;条件 Testvoid queryOne() {// 查询单个userUser user userMapper.selectById(1);System.out.println(…

C#获取txt记事本内容,防止乱码情况

StreamReader sr new StreamReader("E:\人名.txt", Encoding.GetEncoding("GB2312"));string readText sr.ReadToEnd(); 转载于:https://www.cnblogs.com/blog4xy/p/4059363.html

mybatis高级查询,批量新增

reviewsql脚本实体类sql watch outmappermapper test之前的比较分散&#xff0c;自己用。。。sql脚本 -- auto-generated definition create table stu_info (stu_id int auto_incrementprimary key,stu_name varchar(255) null,stu_age int(255) null,stu_gende…

Java对存储过程的调用方法

本文将介绍Java怎样实现对存数过程的调用方法&#xff0c;作者用了几个样例进行了具体的说明&#xff0c;简单明了&#xff0c;很适合刚開始学习的人。 一、Java怎样实现对存储过程的调用&#xff1a; A&#xff1a;不带输出參数的 create procedure getsum n int 0<--此处…

mybatis-plus 代码生成器

生成entity -> mapper-> service ->controller所有的接口&#xff0c;实现&#xff0c;一键完成。 1. 轮子 <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope>…

电脑报错找不到msvcr100.dll,无法继续执行代码如何修复

在电脑使用过程中&#xff0c;我们经常会遇到一些错误提示&#xff0c;其中之一就是MSVCR100.dll丢失。那么&#xff0c;MSVCR100.dll到底是什么&#xff1f;它的作用是什么&#xff1f;为什么会丢失呢&#xff1f;本文将详细介绍MSVCR100.dll的定义、作用以及丢失的原因&#…

windows下面虚拟主机

1. wondows xp apmserv 5.2.6 端口8088 2.httpd.config文件 1. ServerRoot      "D:/APMServ/APMServ5.2.6/Apache" Listen        8088 ServerName     127.0.0.1:8088 DocumentRoot   "D:/APMServ/APMServ5.2.6/www/" (原文未变)…

QueryWrapper查询

QueryWrapper, mybatisplus 中封装了大量的查询方法。用于高级查询。传入表的列和查询的值&#xff0c;就能反射对应的sql. 简化了查询。 更多api&#xff0c;查询使用&#xff0c;看&#xff1a; https://baomidou.com/guide/wrapper.html package cn.bitqian;import cn.bit…