我自己的单元测试库 Unquote 利用 F# quotations 允许您将测试断言编写为简单的、静态检查的 F# 布尔表达式,并自动生成漂亮的逐步测试失败消息。例如,以下失败的 xUnit 测试
[<Fact>]
let ``demo Unquote xUnit support`` () =
test <@ ([3; 2; 1; 0] |> List.map ((+) 1)) = [1 + 3..1 + 0] @>
产生以下失败消息
Test 'Module.demo Unquote xUnit support' failed:
([3; 2; 1; 0] |> List.map ((+) 1)) = [1 + 3..1 + 0]
[4; 3; 2; 1] = [4..1]
[4; 3; 2; 1] = []
false
C:\File.fs(28,0): at Module.demo Unquote xUnit support()
FsUnit 和 Unquote 具有相似的任务:允许您以惯用的方式编写测试,并生成信息丰富的失败消息。但是 FsUnit 实际上只是 NUnit 约束的一个小包装器,它创建了一个 DSL,它将对象构造隐藏在可组合的函数调用之后。但这是有代价的:你失去了断言中的静态类型检查。例如,以下在 FsUnit 中有效
[<Test>]
let test1 () =
1 |> should not (equal "2")
但是使用 Unquote,您可以获得 F# 的所有静态类型检查功能,因此等效的断言甚至无法编译,从而防止我们在测试代码中引入错误
[<Test>] //yes, Unquote supports both xUnit and NUnit automatically
let test2 () =
test <@ 1 <> "2" @> //simple assertions may be written more concisely, e.g. 1 <>! "2"
// ^^^
//Error 22 This expression was expected to have type int but here has type string
此外,由于引用能够在编译时捕获有关断言表达式的更多信息,因此失败消息也更加丰富。例如,失败的 FsUnit 断言 1 |> should not (equal 1) 会生成消息
Test 'Test.Swensen.Unquote.VerifyNunitSupport.test1' failed:
Expected: not 1
But was: 1
C:\Users\Stephen\Documents\Visual Studio 2010\Projects\Unquote\VerifyNunitSupport\FsUnit.fs(11,0): at FsUnit.should[a,a](FSharpFunc`2 f, a x, Object y)
C:\Users\Stephen\Documents\Visual Studio 2010\Projects\Unquote\VerifyNunitSupport\VerifyNunitSupport.fs(29,0): at Test.Swensen.Unquote.VerifyNunitSupport.test1()
而失败的 Unquote 断言 1 <>! 1 会产生以下失败消息(也请注意更清晰的堆栈跟踪)
Test 'Test.Swensen.Unquote.VerifyNunitSupport.test1' failed:
1 <> 1
false
C:\Users\Stephen\Documents\Visual Studio 2010\Projects\Unquote\VerifyNunitSupport\VerifyNunitSupport.fs(29,0): at Test.Swensen.Unquote.VerifyNunitSupport.test1()
当然,从我在这个答案开头的第一个示例中,您可以看到 Unquote 表达式和失败消息是多么丰富和复杂。
在 FsUnit DSL 上使用纯 F# 表达式作为测试断言的另一个主要好处是,它非常适合开发单元测试的 F# 流程。我认为很多 F# 开发人员都是在 FSI 的帮助下开发和测试代码开始的。因此,从临时 FSI 测试转到正式测试非常容易。事实上,除了对 xUnit 和 NUnit 的特殊支持(尽管也支持任何基于异常的单元测试框架),所有 Unquote 操作符也在 FSI 会话中工作。