测试库 scoop.test

scoop:scoop.test:0.1.0 是普通的库 Cone,提供测试标记与断言。编译器根据标记为 Cone 生成测试入口,scoop test 构建并逐个运行用例。测试不依赖运行期反射或测试发现框架。

测试源码与依赖

测试位于 Cone 的 tests/ 目录(可用 [[test.sources]] 修改),测试库在 [test-dependencies] 中声明。scoop new 创建的项目已经包含这些配置和一个示例测试:

[test-dependencies]
"scoop:scoop.test" = "0.1.0"
  • 测试源码与生产源码属于同一个 Cone,可以访问 internal 声明;文件同样需要 package 声明。
  • 生产构建(scoop build/run)不读取测试源码与测试依赖,测试目录中的错误不影响生产构建;生产代码也看不到测试中的声明。
  • 测试标记只能用在测试源码中。断言函数是普通函数,也可以在普通程序中调用。

声明测试

package dev.example

import scoop.test.*

class Account(var balance: Long) {
    fun withdraw(amount: Long) {
        if (amount > balance) throw IllegalStateException(Some("insufficient funds"))
        balance -= amount
    }
}

@Test
private class AccountTests {
    var account = Account(0L)

    @BeforeTest
    fun setUp() {
        account = Account(100L)
    }

    @AfterTest
    fun tearDown() {
        println("teardown")
    }

    @Test
    fun `withdraw reduces the balance`() {
        account.withdraw(30L)
        assertEquals(70L, account.balance)
    }

    @Test
    fun `overdraft fails`() {
        val e = assertFailsWith<IllegalStateException> { account.withdraw(500L) }
        assertEquals(Some("insufficient funds"), e.message)
    }

    @Test
    @Ignore("not implemented yet")
    fun `interest is applied`() {
        fail(Some("todo"))
    }
}
  • 测试容器是标注 @Test 的顶层、具名、非泛型、可实例化的普通 class(不能是 abstract、sealed、嵌套、inner、struct、enum、interface 或 object),可以是 private 或 internal。它必须能以零个实参构造(允许默认参数)。
  • 测试用例是容器中直接声明并标注 @Test 的方法:没有参数、不是挂起函数、返回 Unit。方法名常用反引号写成一句话。
  • 每个容器至多有一个 @BeforeTest 和一个 @AfterTest 方法,形态要求与用例相同,名称任意。
  • @Ignore(reason) 必须与同一个方法上的 @Test 配对,被忽略的用例默认不运行。
  • 容器上的 @Test 不会自动把所有方法当作用例,未标注的方法是普通辅助方法。只发现当前 Cone 测试源码中直接声明的用例,不继承基类的用例。
  • 形态不合法、孤立的 @Ignore 或重复的 hook 都是编译错误。

用例的生命周期

选中的用例依次串行执行,每个用例都使用一个新的容器实例:

  1. 构造容器(包括默认参数、属性初始值与 init);
  2. 调用 @BeforeTest;
  3. 调用用例方法;
  4. 调用 @AfterTest。

构造失败时跳过其余步骤;@BeforeTest 失败时跳过用例方法,但仍执行 @AfterTest;用例方法失败时同样执行 @AfterTest。@AfterTest 至多执行一次,它自身的失败单独报告,不会覆盖之前的失败。任何 Throwable 都使用例失败;panic、原生崩溃与进程退出不属于可捕获的失败,由测试命令另行报告。

断言

断言在 debug 与 release 构建中都会执行。每个断言都有可选的 message: String? 参数,以及默认取调用位置的 location: SourceLocation 参数;在自己的辅助函数中转发断言时,显式传递 location 以保留原始位置(见源码位置)。

函数成立条件
assertTrue(condition) / assertFalse(condition)条件为真 / 为假
assertEquals(expected, actual) / assertNotEquals(expected, actual)expected.equals(actual) 为真 / 为假。适用于实现了 Equals<T> 的类型,也适用于只具备公开 equals 的类型(例如自动获得结构相等的 struct、enum 与 tuple)
assertSame(expected, actual) / assertNotSame(expected, actual)引用 identity 相同 / 不同,只用于引用类型
assertFailsWith<E> { … }代码块抛出 E 或其子类;返回捕获到的异常对象以便继续检查
fail(message): Nothing总是失败

断言失败时抛出 AssertionFailure。它继承 Throwable 而不继承 Exception,因此不会被测试代码中的 catch (e: Exception) 意外吞掉。它的字段包括:

  • kind:断言的名称,例如 "assertEquals";
  • message、location:用户消息与断言位置;
  • expected、actual:AssertionValue,值实现了 ToString 时为 Text(…),否则为 Unavailable,字符串化本身抛出异常时为 RenderingFailed(…);
  • cause:assertFailsWith 捕获到类型不符的异常时保存该异常。

比较函数本身抛出的异常原样传播,不会被转换为断言失败。

运行测试

$ scoop test
newcone
  dev.example.AccountTests [failed]
    withdraw reduces the balance (tests/basic.scoop:22:5) passed
    overdraft fails (tests/basic.scoop:28:5) passed
    deliberately failing (tests/basic.scoop:34:5) failed
      body at dev.example:newcone:0.1.0/tests/basic.scoop:36:9
      assertTrue: balance should be large
      expected: true
      actual: false
    interest is applied (tests/basic.scoop:39:5) ignored: not implemented yet
1 containers, 4 tests: 2 passed, 1 failed, 1 ignored, 0 not run
选项作用
--filter TEXT只运行完整名称(含容器名)包含 TEXT 的用例
--ignored只运行标注了 @Ignore 的用例
--list列出用例而不运行
--no-run只构建测试程序
--nocapture直接显示用例的 stdout/stderr,而不是在失败时汇总
--release、--workspace、-p group:name与 scoop build 相同的构建与成员选择

测试默认以 debug 配置构建,异常的栈快照默认开启。完整选项见 scoop test --help,编写测试的完整流程见用户指南。