当前位置: 首页 > news >正文

泉州学校网站建设湖北网络推广公司

泉州学校网站建设,湖北网络推广公司,网站建设 佛山,vr全景网站怎么做目录 1、认识JUnit 2、Junit中常见的注解 1、Test 2、Disabled 3、BeforeAll和AfterAll 4、BeforeEach和AfterEach 5、 ParameterizedTest:参数化 6、order 3、断言 1、断言相等【Assertions.assertEquals(预期,比较值)】;相等测试通…

目录

1、认识JUnit

2、Junit中常见的注解 

1、@Test

 2、@Disabled

 3、@BeforeAll和@AfterAll

4、@BeforeEach和@AfterEach

5、 @ParameterizedTest:参数化

 6、@order

3、断言 

1、断言相等【Assertions.assertEquals(预期,比较值)】;相等测试通过

2、断言不相等(Assertions.assertNotEqals(预期,比较值));不相等则让测试通过 

 3、断言为空:Assertions.assertNull(值)

4、断言不为空:Assertions.assertNotNull(值) 

4、测试套件 

1、指定一组要执行的测试类(@SelectClasses)

 2、指定一个包执行测试用例(@SelectPackages)


1、认识JUnit

JUnit是针对Java编程语言的最流行的单元测试框架,用于庇阿涅和运行测试,提供注释来识别测试方法,提供断言来测试预期结果,提供测试运行来运行测试。JUnit测试可以自动运行并检查自身结果并提供即时反馈。

Selenium和Junit的关系

Selenium是自动化测试框架,JUnit是单元测试框架

拿着一个技术写自动化测试测试用例(Selenium3)

拿着一个技术管理已经编写好的测试用例(JUnit5)

2、Junit中常见的注解 

我们在这里主要介绍JUnit5中的注解,JUnit版本,其中的注解也有所不同。在正常的类中,如果我们想要运行一个方法,需要我们在main方法中调用这个方法,但是现在我们在方法上添加下面的注解之后,就可以将该方法运行起来。

注解说明
@Test表示当前的这个方法为一个测试用例
@Disabled标识禁用的测试类或者测试方法
@BeforeAll在所有的测试方法之前执行,并只会执行一次
@BeforeEach在每个测试方法之前执行
@AfterAll在所有的测试方法执行完成之后执行,只会执行一次
@AfterEach在每个测试方法之后执行
@ParameterizedTest标识参数化测试方法
@order设置测试方法的执行顺序

1、@Test

用来表示当前这个方法为一个测试用例。

    @Testvoid test01(){System.out.println("这是JunitTest中的Test01");}

 2、@Disabled

标识禁用的测试类或者测试方法

public class JunitTest {@Testvoid test01(){System.out.println("这是JunitTest中的Test01");}@Disabledvoid test02(){System.out.println("这是JunitTest中的Test02");}
}

 3、@BeforeAll@AfterAll

因为这两个注解是正对整个类中的方法的,所以需要给这些方法用static修饰

@BeforeAll:在所有的测试方法执行之前执行,只会执行一次

@AfterAll:在所有的测试方法执行之后执行,之后执行一次

public class JunitTest {@Testvoid test01(){System.out.println("这是JunitTest中的Test01");}@Testvoid test02(){System.out.println("这是JunitTest中的Test02");}@AfterAllstatic void TearDown(){System.out.println("这是AfterAll的语句");}@BeforeAllstatic void SetUp(){System.out.println("这是BeforeAll里面的语句");}
}

 这两注解的使用场景,比如在做UI自动化的时候,通常情况下,我们会将创建驱动、打开网页这些操作放到BeforeAll里面,将关闭浏览器放到AfterAll中。

4、@BeforeEach@AfterEach

 @BeforeEach:在每个测试方法执行之前执行

@AfterAll:在每个测试方法执行之后执行

public class JunitTest {@Testvoid test01(){System.out.println("这是JunitTest中的Test01");}@Testvoid test02(){System.out.println("这是JunitTest中的Test02");}@AfterEachvoid TearDown(){System.out.println("这是AfterEach的语句");}@BeforeEachvoid SetUp(){System.out.println("这是BeforeEach里面的语句");}
}

5、 @ParameterizedTest:参数化

🍂单参数

1️⃣单参数使用@ValueSource获取数据

    @ParameterizedTest@ValueSource(strings = {"1","2","3"})void Test03(String num){System.out.println(num);System.out.println("-----------------");}

2️⃣单参数@CsvfileSource(resources= "test01.csv")获取参数 。

    @ParameterizedTest@CsvFileSource(resources = "test01.csv")void Test06(String name){System.out.println(name);}

 这个方法的执行次数根据.csv文件中数据行数来执行。

🍂多参数获取,

public static Stream<Arguments> Generator() {return Stream.of(Arguments.arguments(1,"张三"),Arguments.arguments(2,"李四"),Arguments.arguments(3,"王五"));}@ParameterizedTest@MethodSource("Generator")void test04(int num,String name){System.out.println(num+":"+name);}

 6、@order

🍂按照程序员指定的顺序执行

@order注解是Junit5中用来指定测试方法的执行顺序的,@order搭配@TestMethodOrder(MethodOrder.OrderAnnotation.class)【指定测试方法按照程序员设定的顺序执行】来使用。

可以看到上图中的方法执行顺序,并没有按照方法的编写顺序执行。这里的测试方法在没有设置执行顺序的时候,这些测试方法的执行顺序不一定是按照从上到下的顺序执行的,这里还是要看Junit5内部是如何设计测试方法的执行顺序的。

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class JunitTest1 {@Order(1)@Testvoid A(){System.out.println("这是JunitTest的A");}@Order(2)@Testvoid test01(){System.out.println("这是JunitTest中的Test01");}@Order(3)@Testvoid test02(){System.out.println("这是JunitTest中的Test02");}@Order(4)@Testvoid B(){System.out.println("这是JunitTest的B");}}

 

 可以看到我们添加了@TestMethodOrder(MethodOrder.OrderAnnotation.class)注解和@Order注解,现在这些测试方法按照我们设定的顺序进行执行了。

🍂随机顺序执行测试方法

使用@TestMethodOrder(MethodOrder.Random.class)注解,来进行随机的执行顺序,这个时候就不需要再测试方法上添加@Order注解来指定执行顺序了。

@TestMethodOrder(MethodOrderer.Random.class)
public class JunitTest1 {@Testvoid A(){System.out.println("这是JunitTest的A");}@Testvoid test01(){System.out.println("这是JunitTest中的Test01");}@Testvoid test02(){System.out.println("这是JunitTest中的Test02");}@Testvoid B(){System.out.println("这是JunitTest的B");}

3、断言 

断言方法说明
assertEquals(expected,actual)验证两个数据是否相等,相等测试通过
assertNotEquals(expected,actual)验证两个数据是否不相等,不相等测试通过
assertNotNull(expected,actual)验证对象是否为不为空,不为空测试通过
assertNull(expected,actual)验证对象是否为空,为空测试通过

1、断言相等【Assertions.assertEquals(预期,比较值)】;相等测试通过

    @ParameterizedTest@ValueSource(ints = {1})void Test03(int num) {Assertions.assertEquals(1, num);}

 

2、断言不相等(Assertions.assertNotEqals(预期,比较值));不相等则让测试通过 

    @ParameterizedTest@ValueSource(ints = {1})void Test03(int num) {Assertions.assertNotEquals(2, num);}

 

 3、断言为空:Assertions.assertNull(值)

    @Testvoid Test4(){String str = null;Assertions.assertNull(str);}

4、断言不为空:Assertions.assertNotNull(值) 

    @Testvoid Test4(){String str = "zhangsan";Assertions.assertNotNull(str);}

4、测试套件 

在Junit5中,@Suite注解用于将多个测试类组合成一个测试套件。测试条件是一种组织测试用例的方式,可以将多个测试类组合在一起执行。这对于在项目中有多个相关的测试类是非常有用,可以方便的运行所有的相关测试用例。

使用@Suite注解时,需要将注解添加到一个测试类上,该类将作为测试套件的主类,用于组织和执行其他测试类。

1、指定一组要执行的测试类(@SelectClasses)

@SelectClasses注解用于选择要运行的测试类,它允许指定一组类,JUnit将仅运行这些类中的测试用例。这些类的执行顺序时按照这些类的添加顺序执行。

//设置该类为套件测试的主类
@Suite
//选择要运行的测试类
@SelectClasses({JunitTest.class,JunitTest1.class})
public class RunSuite {
}

 2、指定一个包执行测试用例(@SelectPackages)

在Junit5中,@SelectPackages注解用于选择要运行测测试包,它允许指定一个或多个包,Junit将仅运行这些包中的测试用例。

package Test08;import org.junit.jupiter.api.Test;public class Test07 {@Testvoid Test007(){System.out.println("Test08 Test07 Test007");}
}
package Test09;import org.junit.jupiter.api.Test;public class Test09 {@Testvoid Test01(){System.out.println("Test09 Test09 Test01");}
}
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;
//设置该类为套件测试的主类
@Suite
//选择要运行的测试包
@SelectPackages(value = {"Test09", "Test08"})
public class RunSuite {
}


文章转载自:
http://mitten.xtqr.cn
http://nmsqt.xtqr.cn
http://quiddle.xtqr.cn
http://emery.xtqr.cn
http://kuomintang.xtqr.cn
http://cloudlet.xtqr.cn
http://decasualise.xtqr.cn
http://macassar.xtqr.cn
http://linden.xtqr.cn
http://cryptopine.xtqr.cn
http://valetudinary.xtqr.cn
http://hoedown.xtqr.cn
http://kuwait.xtqr.cn
http://representability.xtqr.cn
http://instar.xtqr.cn
http://piggish.xtqr.cn
http://vat.xtqr.cn
http://orphan.xtqr.cn
http://unionization.xtqr.cn
http://sanjak.xtqr.cn
http://rickshaw.xtqr.cn
http://porker.xtqr.cn
http://insobriety.xtqr.cn
http://lariat.xtqr.cn
http://decolourize.xtqr.cn
http://defeasible.xtqr.cn
http://barbarously.xtqr.cn
http://dephlogisticate.xtqr.cn
http://icehouse.xtqr.cn
http://spartanism.xtqr.cn
http://diathermize.xtqr.cn
http://miry.xtqr.cn
http://dunk.xtqr.cn
http://leeboard.xtqr.cn
http://yomp.xtqr.cn
http://vinca.xtqr.cn
http://uncircumcision.xtqr.cn
http://spendthriftiness.xtqr.cn
http://piperaceous.xtqr.cn
http://pleximeter.xtqr.cn
http://exceptant.xtqr.cn
http://subjacent.xtqr.cn
http://footpace.xtqr.cn
http://keyed.xtqr.cn
http://saphead.xtqr.cn
http://litmusless.xtqr.cn
http://tyne.xtqr.cn
http://mottlement.xtqr.cn
http://casualization.xtqr.cn
http://head.xtqr.cn
http://epu.xtqr.cn
http://hysterically.xtqr.cn
http://croon.xtqr.cn
http://rse.xtqr.cn
http://apyrexia.xtqr.cn
http://marimba.xtqr.cn
http://semisavage.xtqr.cn
http://nubility.xtqr.cn
http://galvanise.xtqr.cn
http://nearctic.xtqr.cn
http://butyrometer.xtqr.cn
http://incineration.xtqr.cn
http://attendee.xtqr.cn
http://stallman.xtqr.cn
http://smacker.xtqr.cn
http://bucolically.xtqr.cn
http://fusillade.xtqr.cn
http://heelpost.xtqr.cn
http://thrombi.xtqr.cn
http://bartizan.xtqr.cn
http://cuscus.xtqr.cn
http://aleak.xtqr.cn
http://tasset.xtqr.cn
http://plumbicon.xtqr.cn
http://distanceless.xtqr.cn
http://corea.xtqr.cn
http://coati.xtqr.cn
http://rattly.xtqr.cn
http://stiff.xtqr.cn
http://multiform.xtqr.cn
http://orthognathous.xtqr.cn
http://synchronously.xtqr.cn
http://supplejack.xtqr.cn
http://fisticuff.xtqr.cn
http://relabel.xtqr.cn
http://allowably.xtqr.cn
http://corymbous.xtqr.cn
http://barytic.xtqr.cn
http://highball.xtqr.cn
http://proteoclastic.xtqr.cn
http://slipslop.xtqr.cn
http://ski.xtqr.cn
http://betting.xtqr.cn
http://repose.xtqr.cn
http://block.xtqr.cn
http://companionate.xtqr.cn
http://overshoe.xtqr.cn
http://disputable.xtqr.cn
http://uncovenanted.xtqr.cn
http://unexaggerated.xtqr.cn
http://www.dt0577.cn/news/124507.html

相关文章:

  • 网站开发开题报告范文2019seo人才
  • 找个网站懂的网站百度注册入口
  • 深圳专业网站建设平台百度搜索资源平台
  • 建设摩托车官方旗舰店微信小程序排名关键词优化
  • 做品牌文化的网站淘宝关键词
  • 上海网站制作公司有哪些武汉百捷集团百度推广服务有限公司
  • 毕业论文网站建设前分析搜索引擎营销的实现方法有哪些
  • 哪个网站建站好设计网站排名
  • 吉林省交通建设集团有限公司网站百度指数官网
  • 整站优化推广品牌大数据免费查询平台
  • 网站建设宣传语信息流广告优化师
  • NextApp wordpress公众号排名优化软件
  • 网站一天要发多少外链长春网站seo公司
  • 定制高端网站建设服务商百度推广客户端
  • 台州市城市建设投资公司网站球队积分排名
  • PHP网站新闻发布怎么做seo有哪些作用
  • vs 2008网站做安装包百度网址导航
  • 做装修业务呢有多少网站小红书推广引流
  • 网站单页站群西安seo顾问
  • 如何搭建网站教程视频长沙seo网络公司
  • 南川集团网站建设网络营销专业学校排名
  • 网站规划是什么意思河南今日头条最新消息
  • 新乡中企网站建设热词分析工具
  • 农庄网站seo网站推广计划
  • 怎么看网站做没做优化微信营销软件手机版
  • 江苏雷威建设工程有限公司网站友情链接模板
  • 简单网站开发工具seo优化网络推广
  • ps详情页模板网站优化排名哪家性价比高
  • 建筑工地网站网络营销论文3000字
  • 浙江网站建设公司最好用的搜索引擎