Truth:Google的断言和命题框架

阅读: 评论:0

Truth:Google的断言和命题框架

Truth:Google的断言和命题框架

Truth:Google的断言和命题框架

/
Truth 是一个测试框架,可以改进测试用例以及错误日志的可读性,而且可侦测型更强。不仅如此,它对自定义类型也有很好的扩展性。

Truth对测试命题有一套流畅的测试风格,并且具有良好的扩展性。Truth能够在IDE中完整运行命题(proposition)或者跟踪侦测等模式,在非真命题上有多种不同的反馈方式。Truth可以声明JUnit风格的常见操作,诸如:

  • Junit风格的假设(Assume,如果指定语句执行失败则直接跳过测试)
  • 断言(Assert,如果指定语句执行失败则中断测试)
  • 预期(Expect,持续完成测试,只是将错误和失败收集起来最后集中显示)。

Truth是一个开源项目,采用Apache 2.0开源许可。因此,可以在遵循本许可条款的前提下免费使用和修改。

示例

这是我们平常使用JUnit的场景

Java
1 2 3 4 import static org . junit . Assert . assertTrue ; Set <Foo> foo = . . . ; assertTrue ( foo . isEmpty ( ) ) ; // or, shudder, foo.size() == 0

如果出现异常, 这时候会打印出一些用处不大的异常堆栈信息,

Shell
1 2 3 4 java .lang .AssertionError      at org .junit .Assert .fail ( Assert .java : 92 )      at org .junit .Assert .assertTrue ( Assert .java : 43 )      . . .

使用Truth后:

Java
1 2 3 4 import static com . google . common . truth . Truth . assertThat ; Set <Foo> foo = . . . ; assertThat ( foo ) . isEmpty ( )

反馈信息:

Shell
1 2 3 org .truth0 .FailureStrategy $ThrowableAssertionError : Not true that   is empty      at org .truth0 .FailureStrategy .fail ( FailureStrategy .java : 33 )      . . .

Truth的命题方式(多多少少)接近英语,这样以来更加容易看懂,并且测试报告中显示的内容也更有意义。

使用Truth的其它方式

这里给大家提供两种使用Truth的方式。

首先,Truth的主要操作是围绕一个具有不同表现形式的动作(断言、假定、预计)来开展工作。由于assert是最常用的,又偏偏是Java的一个保留关键字,所以我们采用了类似Hamcrest和FEST的做法,创建了一个独立而简短的方法,以下是它的常见用法:

Java
1 assertThat ( someInt ) . isEqualTo ( 5 ) ;      // Convenience method to access assert_().that()

而在非assert需要对这个测试动作增加额外功能时,还可以使用一种更加纯净的调用方式,通过返回这个测试动作对象来进行自定义设置,再进行测试。

Java
1 2 3 assert_ ( ) . that ( someInt ) . isEqualTo ( 5 ) ; // Assert is a keyword in java, so no assert().that() assert_ ( ) . about ( . . . ) . that ( . . . ) . . .      // Extensibility (see later in this page) assume ( ) . that ( someValue ) . isNotNull ( ) ; // JUnit-style Assume behavior.

Truth的帮助文档里会使用assertThat(…)来作为所有的断言例子的操作方式,除非有必须进行一些方法定义的情况。当然,assert_().that(…)的方式也是完全可用的。

所有assert_()能够做到的操作,assume()也能做到。而且,所有能够拿到“测试动作”的方式也都能做到这些。

一些常见的命题

准备

assertThat 是Truth类中的一个方法,所以一开始记得要 import uth.Truth.assertThat

基本用法

Java
1 2 3 4 assertThat ( someInt ) . isEqualTo ( 5 ) ; assertThat ( aString ) . isEqualTo ( "Blah Foo" ) ; assertThat ( aString ) . contains ( "lah" ) ; assertThat ( foo ) . isNotNull ( ) ;

集合用法

Java
1 2 3 4 5 6 7 assertThat ( someCollection ) . contains ( "a" ) ;                        // contains this item assertThat ( someCollection ) . containsAllOf ( "a" , "b" ) . inOrder ( ) ;    // contains items in the given order assertThat ( someCollection ) . containsExactly ( "a" , "b" , "c" , "d" ) ; // contains all and only these items assertThat ( someCollection ) . containsNoneOf ( "q" , "r" , "s" ) ;        // contains none of these items assertThat ( aMap ) . containsKey ( "foo" ) ;                              // has a key assertThat ( aMap ) . containsEntry ( "foo" , "bar" ) ;                    // has a key, with given value assertThat ( aMap ) . doesNotContainEntry ( "foo" , "Bar" ) ;              // does not have the given entry

自定义错误信息

Java
1 2 3 4 5 6 7 8 9 // Reports: The subject is unexpectedly false assertThat ( myBooleanResult ) . isTrue ( ) ; // Reports: "hasError()" is unexpectedly false assertThat ( myBooleanResult ) . named ( "hasError()" ) . isTrue ( ) ; // Reports: My custom Message assert_ ( ) . withFailureMessage ( "My arbitrary custom message" )      . that ( myBooleanResult ) . named ( "hasError()" ) . isTrue ( ) ;

新对象,新命题

Truth对新的数据类型有很好的扩展性,开发者可以创建自定义的“主题(Subject)”,像下面这样

Java
1 2 3 // customType() returns an adapter (SubjectFactory). assert_ ( ) . about ( customType ( ) ) . that ( theObject ) . hasSomeComplexProperty ( ) ; // specialized assertion assert_ ( ) . about ( customType ( ) ) . that ( theObject ) . isEqualTo ( anotherObject ) ; // overridden equality

安装配置

要使用Truth,在maven中这样配置

XHTML
1 2 3 4 5 <dependency>    <groupId> uth </groupId>    <artifactId> truth </artifactId>    <version> 0.28 </version> </dependency>

或者直接通过下方的链接下载jar包,并将它加入到你测试工程的classpath中

1 http : //search.maven/remotecontent?filepath=org/truth0/truth/0.28/truth-0.28.jar

更多的信息

需要了解更多信息,点击查看Truth到底如何工作的,还可以查看更多内置已有的命题详情。

开发资源

  • 使用方法
  • FAQ
  • Javadoc

官方网站:/
开源地址:

本文发布于:2024-01-28 17:18:22,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17064335069002.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:断言   命题   框架   Truth   Google
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23