哪些单元测试框架可用于F# [英] What unit testing frameworks are available for F#
问题描述
我正在专门寻找允许我利用语言独特功能的框架.我知道 FsUnit .您会推荐其他东西吗,为什么?
I am looking specifically for frameworks that allow me to take advantage of unique features of the language. I am aware of FsUnit. Would you recommend something else, and why?
推荐答案
我自己的单元测试库取消引用,利用 F#引用将测试断言编写为简单的静态检查的F#布尔表达式,并自动生成不错的分步测试失败消息.例如,以下xUnit测试失败
My own unit testing library, Unquote, takes advantage of F# quotations to allow you to write test assertions as plain, statically checked F# boolean expressions and automatically produces nice step-by-step test failure messages. For example, the following failing xUnit test
[<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中有效
FsUnit and Unquote have similar missions: to allow you to write tests in an idiomatic way, and to produce informative failure messages. But FsUnit is really just a small wrapper around NUnit Constraints, creating a DSL which hides object construction behind composable function calls. But it comes at a cost: you lose static type checking in your assertions. For example, the following is valid in FsUnit
[<Test>]
let test1 () =
1 |> should not (equal "2")
但是使用Unquote,您可以获得F#的所有静态类型检查功能,因此等效的断言甚至都无法编译,从而阻止了我们在测试代码中引入错误
But with Unquote, you get all of F#'s static type-checking features so the equivalent assertion would not even compile, preventing us from introducing a bug in our test code
[<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)
会生成消息
Also, since quotations are able to capture more information at compile time about an assertion expression, failure messages are a lot richer too. For example the failing FsUnit assertion 1 |> should not (equal 1)
produces the message
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
会产生以下失败消息(也请注意更清洁的堆栈跟踪)
Whereas the failing Unquote assertion 1 <>! 1
produces the following failure message (notice the cleaner stack trace too)
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表达式和失败消息.
And of course from my first example at the beginning of this answer, you can see just how rich and complex Unquote expressions and failure messages can get.
使用纯F#表达式作为FsUnit DSL上的测试断言的另一个主要好处是,它非常适合开发单元测试的F#过程.我认为许多F#开发人员都是在FSI的帮助下开始开发和测试代码的.因此,从临时FSI测试到正式测试非常容易.实际上,除了对xUnit和NUnit的特殊支持(尽管也支持任何基于异常的单元测试框架)之外,所有Unquote运算符也都在FSI会话中工作.
Another major benefit of using plain F# expressions as test assertions over the FsUnit DSL, is that it fits very well with the F# process of developing unit tests. I think a lot of F# developers start by developing and testing code with the assistance of FSI. Hence, it is very easy to go from ad-hoc FSI tests to formal tests. In fact, in addition to special support for xUnit and NUnit (though any exception-based unit testing framework is supported as well), all Unquote operators work within FSI sessions too.
这篇关于哪些单元测试框架可用于F#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!