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

在线网站建设平台哪个好怎么利用互联网推广

在线网站建设平台哪个好,怎么利用互联网推广,端午节网站建设,如何设置自己的网址断言是测试用例的一部分,也是测试工程师开发测试用例的核心。断言通常集成在单元测试和集成测试中,断言分为硬断言和软断言。 硬断言是我们狭义上听到的普通断言:当用例运行后得到的[实际]结果与预期结果不匹配时,测试框架将停止测试执行并抛…

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

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

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.hkea.cn/news/105379/

相关文章:

  • 邯郸哪有做网站的河南百度推广公司
  • 我是做环保类产品注册哪些浏览量大的网站推销自己的产品比较好呢西安网站seo优化公司
  • 网页传奇游戏排行昆明网络推广优化
  • 商城模板网站模板网站软文是什么
  • 校园网站推广方案怎么做网站排名推广工具
  • 深圳罗湖企业网站建设报价网络媒体发稿平台
  • 用别人公司域名做网站线下推广的渠道和方法
  • php mysql的网站开发外贸推广平台
  • 济南网站建设认可搜点网络能百度指数有三个功能模块
  • 网上商城网站建设意义在线代理浏览网页
  • 网站图片切换代码百度下载并安装最新版
  • 微信公众平台号申请注册入口杭州seo公司
  • 本周实时热点新闻事件seo文章代写一篇多少钱
  • 旺店通app手机企业版下载网站seo如何优化
  • 宝山区建设用地事务所网站网络公司有哪些
  • 用sql做简单的博客网站大连谷歌seo
  • 新手怎么学做网站就业培训机构有哪些
  • magento网站建设搭建网站步骤
  • 营销网站如何实现差异化南京seo公司
  • 服务器托管是啥搜索引擎优化排名培训
  • 山西手机网站建设网址大全123
  • b2c平台有哪些平台网址新区seo整站优化公司
  • WordPress突然全站404网站如何添加友情链接
  • 复制别人网站做第一站seo短视频网页入口引流下载
  • 基层建设论文收录在哪个网站百度统计api
  • 购买主机可以做网站吗楚雄今日头条新闻
  • 深圳专业网站建设公司哪家好宁波网络营销公司
  • ps做电商网站流程图百度图片识别搜索引擎
  • 做电影网站程序好用武汉网站建设推广公司
  • 如何做b2c网站下列关于友情链接说法正确的是