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

深圳办公室装修招标优化网站排名公司

深圳办公室装修招标,优化网站排名公司,七台河新闻头条,dw可以用来做网站吗断言是测试用例的一部分,也是测试工程师开发测试用例的核心。断言通常集成在单元测试和集成测试中,断言分为硬断言和软断言。 硬断言是我们狭义上听到的普通断言:当用例运行后得到的[实际]结果与预期结果不匹配时,测试框架将停止测试执行并抛…

断言是测试用例的一部分,也是测试工程师开发测试用例的核心。断言通常集成在单元测试和集成测试中,断言分为硬断言和软断言。

硬断言是我们狭义上听到的普通断言:当用例运行后得到的[实际]结果与预期结果不匹配时,测试框架将停止测试执行并抛出断言错误。如下面的案例,测试执行在第一个失败时停止,即使测试中有更多的断言也不会继续执行。

import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;class HardAssertionTest {@Testvoid hardAssertion() {var person = Person.builder().name("John").phoneNumber(null).age(16).build();assertThat(person.getName()).isNotBlank();assertThat(person.getPhoneNumber()).isNotNull();assertThat(person.getAge()).isGreaterThan(18);}
}

第10行断言失败,则第11行和第12行的断言将不会被执行。

java.lang.AssertionError:
Expecting actual not to be null

通常来说,硬断言在断言失败情况下终止测试执行是无可厚非的。但是在某些情况下,我们想知道被断言的“对象”里哪些属性是不符合预期的,这样使用软断言将解决这个问题。

什么是软断言?

软断言:当一个测试用例中存在多个断言,当有一个断言失败时,会执行后续的断言。

支持软断言的工具通常使用下面的伪代码实现。

SoftAssertion softAssertion = new SoftAssertion()softAssertion.assertSomething...
softAssertion.assertAnotherThing...
softAssertion.assertTheLastThing...softAssertion.assertThenAll();

什么情况下使用软断言?

当测试用例有多个断言时候,应该使用软断言。因为知道所有断言是否与预期结果一致,可以减少多次运行测试以了解哪些不通过。

当测试用例有不止一条断言时,软断言就要到位了。

支持软断言的工具

TestNG

TestNG有一个SoftAssert类,它的功能与前面介绍的伪代码相同:它对断言进行分组,并在我们调用特定方法时立即进行验证。

来看一个软断言的案例:

public class SoftAssertTestNGTest {@Testpublic void testNGSoftAssertion() {var person = Person.builder().name("John").phoneNumber(null).age(16).build();SoftAssert softAssert = new SoftAssert();softAssert.assertEquals(person.getName(), "John");softAssert.assertNotNull(person.getPhoneNumber(), "Phone number cannot be null");softAssert.assertEquals(person.getAge(), 25, "Age should be equal");softAssert.assertAll();}
}

在例子中,我们断言:phoneNumber不能为空,年龄必须等于25。

  • 第8到10行使用softAssertion在断言方法之前,告诉代码它属于SoftAssertion类。可以使用TestNG支持的任何断言方法实现。
  • 第12行调用assertAll ()方法,该方法将运行与softAssertion引用关联的所有断言

我们可以看到显示一个断言失败,而TestNG没有停止测试执行,而是运行所有的断言,显示所有的失败。

java.lang.AssertionError: The following asserts failed:Phone number cannot be null
Expected :25
Actual   :16
<Click to see difference>

JUnit 5

JUnit 5使用assertAll()方法作为软断言方法。它不需要使用外部特定类,它已经是断言类的一部分。我们需要做的就是静态方式导入它。

例子:

import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;class SoftAssertionJunit5Test {@Testvoid softAssertionUsingJUnit5() {var person = Person.builder().name("John").phoneNumber(null).age(16).build();assertAll("person",() -> assertNotNull(person.getName(), "Name must not be null"),() -> assertNotNull(person.getPhoneNumber(), "Phone number should not be null"),() -> assertEquals(18., person.getAge(), "Age must be 18"));}
}
  • 第12行显示了使用两个参数的assertAll()方法
    • 一个标题
    • 一个流式的可执行的命令,即断言方法
  • 第13到15行显示了stream的用法,每个stream用于我们必须应用的任何断言

执行结果如下:

org.opentest4j.AssertionFailedError: Phone number should not be null ==> expected: not <null>at org.example.SoftAssertionJunit5Test.lambda$softAssertionUsingJUnit5$1(SoftAssertionJunit5Test.java:17)org.opentest4j.AssertionFailedError: Age must be 18 ==>
Expected :18.0
Actual   :16.0at org.example.SoftAssertionJunit5Test.lambda$softAssertionUsingJUnit5$2(SoftAssertionJunit5Test.java:18)org.opentest4j.MultipleFailuresError: person (2 failures)org.opentest4j.AssertionFailedError: Phone number should not be null ==> expected: not <null>org.opentest4j.AssertionFailedError: Age must be 18 ==> expected: <18.0> but was: <16.0>

AssertJ

AssertJ断言库提供了软断言的不同方法,并可以创建自己的断言:

  • 调用assertAll ()
  • 使用autocloseableoftassertion
  • 使用静态方法assertsoft

我们可以使用所有这些不同的方法来应用它,并在AssertJ页面上查看所有示例。这里可以看到assertsoft静态方法的使用案例。

import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;class SoftAssertionTest {@Testvoid softAssertionUsingAssertJ() {var person = Person.builder().name("John").phoneNumber(null).age(16).build();SoftAssertions.assertSoftly(softly -> {softly.assertThat(person.getName()).isNotBlank();softly.assertThat(person.getPhoneNumber()).isNotNull();softly.assertThat(person.getAge()).isGreaterThan(18);});}
}

异常输出如下:

java.lang.AssertionError:
Expecting actual not to be null
at SoftAssertionTest.lambda$assertJSoftAssertion$0(SoftAssertionTest.java:16)java.lang.AssertionError:
Expecting actual:16
to be greater than:18
at SoftAssertionTest.lambda$assertJSoftAssertion$0(SoftAssertionTest.java:17)org.assertj.core.error.AssertJMultipleFailuresError:
Multiple Failures (2 failures)
-- failure 1 --
Expecting actual not to be null
at SoftAssertionTest.lambda$assertJSoftAssertion$0(SoftAssertionTest.java:16)
-- failure 2 --
Expecting actual:16
to be greater than:18
at SoftAssertionTest.lambda$assertJSoftAssertion$0(SoftAssertionTest.java:17)
  • AssertionErrors列表及其完整的堆栈信息
  • 带有断言失败的摘要信息
http://www.zhongyajixie.com/news/16025.html

相关文章:

  • 做网站怎么不被找到网络营销的特征
  • 商城网站开发报价网络服务器的功能
  • 网站怎么加内容吗淘宝关键词挖掘工具
  • 互联网招商项目什么是seo如何进行seo
  • 英文网站建设方案模板高校企业推广方案
  • 做网站的越来越少了宁波seo外包快速推广
  • 济宁企业做网站东莞seoseo关键词排名优化
  • 兰州注册公司代办机构哪家好china东莞seo
  • 做静态网站用什么软件西安霸屏推广
  • 中外政府网站建设比较哪个公司网站设计好
  • 做nba直播网站好推广引流平台app大全
  • 做网站安全认证刚刚刚刚刚刚刚刚刚刚刚刚刚刚
  • wordpress网站建设教程视频百度推广费用
  • 网站备案方法nba交易最新消息
  • 做哪类视频网站需要视频牌照海外广告投放渠道
  • 即买即送的网站有哪些交易平台官网
  • 网站做淘宝客需要什么网站建设是干嘛的
  • 门户网站建设文案传统营销方式有哪些
  • 做网站要到公安局去备案么百度浏览器下载官方免费
  • 手机体验网站网络推广发展
  • 哪家做的网站有利于百度推广百度统计app下载
  • 住房建设部官方网站办事大厅优化网站搜索
  • 网站建设推广招代理加盟苏州新闻今天最新消息新闻事件
  • wordpress 站点换域名百度推广的方式有哪些
  • 基于iview的网站开发模板优秀网站设计案例
  • 做vi 设计国外网站明星百度指数排行
  • 怎么给婚恋网站做情感分析比优化更好的词是
  • 中信建设网站百度网站提交了多久收录
  • 公司做的局域网网站怎么登陆美国搜索引擎浏览器
  • 西安关键词网站排名企业网站首页