Truth:Google 的断言和命题框架

3,287 阅读3分钟
原文链接: hao.jobbole.com

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

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

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

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

示例

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

importstaticorg.junit.Assert.assertTrue;

Setfoo=...;

assertTrue(foo.isEmpty());// or, shudder, foo.size() == 0


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

java.lang.AssertionError

atorg.junit.Assert.fail(Assert.java:92)

atorg.junit.Assert.assertTrue(Assert.java:43)

...


使用Truth后:

importstaticcom.google.common.truth.Truth.assertThat;

Setfoo=...;

assertThat(foo).isEmpty()


反馈信息:

org.truth0.FailureStrategy$ThrowableAssertionError:Nottruethat  isempty

atorg.truth0.FailureStrategy.fail(FailureStrategy.java:33)

...


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

使用Truth的其它方式

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

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

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


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

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 static com.google.common.truth.Truth.assertThat

基本用法

assertThat(someInt).isEqualTo(5);

assertThat(aString).isEqualTo("Blah Foo");

assertThat(aString).contains("lah");

assertThat(foo).isNotNull();

集合用法

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

自定义错误信息

// 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)”,像下面这样

// customType() returns an adapter (SubjectFactory).

assert_().about(customType()).that(theObject).hasSomeComplexProperty();// specialized assertion

assert_().about(customType()).that(theObject).isEqualTo(anotherObject);// overridden equality

安装配置

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

com.google.truth

truth

0.28


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

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

更多的信息

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

开发资源

官方网站:google.github.io/truth/
开源地址:github.com/google/trut…