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

如何网站建设网站网络平台建设及运营方案

如何网站建设网站,网络平台建设及运营方案,131美女做爰视频网站,学wordpress难不难断言是测试用例的一部分,也是测试工程师开发测试用例的核心。断言通常集成在单元测试和集成测试中,断言分为硬断言和软断言。 硬断言是我们狭义上听到的普通断言:当用例运行后得到的[实际]结果与预期结果不匹配时,测试框架将停止测试执行并抛…

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

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

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.dinnco.com/news/59344.html

相关文章:

  • 毕业设计 网站开发简单吗网络舆情监测与研判
  • 做韩国外贸网站百度搜索收录入口
  • 做asp网站需要的实验报告单百度竞价是什么
  • 公司设计图片大全百度seo搜索排名
  • 代办公司注册一般多少钱网络推广优化方案
  • 北京好的做网站的公司微信营销工具
  • 网站建设 h5网页设计与制作案例教程
  • wordpress手机访问主题seo网络培训机构
  • 怎么用云主机做网站优化网络培训
  • wordpress站点地址seo基础培训教程
  • 怎样制作时时彩网站做seo点击工具
  • 网站开发加维护需要多少钱网络口碑营销案例
  • 福州建网站哪家好seo工资水平
  • 做网站空间百度云和阿里云区别互联网平台有哪些
  • 公司建网站多少seo最新快速排名
  • txt电子书下载网站推荐百度问答怎么赚钱
  • 网站规划名词解释百度客户服务中心
  • 网站公安备号市场调研的方法有哪些
  • 东莞搭建网站要多少钱海曙seo关键词优化方案
  • 网站之家app谷歌浏览器直接打开
  • 网站建设视频vs站长统计app下载免费
  • 哪个网站可以查到竣工资料怎么做精准客户资源购买
  • 沧州网站制作重庆seo是什么
  • 无锡网站推广百度指数爬虫
  • 网易企业邮箱邮件保存多久汕头seo网站建设
  • php 自动做网站点击量河南seo网站多少钱
  • 网站管理助手哪个好用cms自助建站系统
  • 莆田市秀屿区建设局网站旅游景点推广软文
  • 网站织梦模板软件推广赚佣金渠道
  • 做网站的怎么赚钱国际网络销售平台有哪些