本文介绍了哪些单元测试框架可用于 F#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在寻找能够让我利用该语言独特功能的框架.我知道 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 automaticallylet 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 |>不应该(等于 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 messageTest 'Test.Swensen.Unquote.VerifyNunitSupport.test1' failed: Expected: not 1 But was: 1 C:UsersStephenDocumentsVisual Studio 2010ProjectsUnquoteVerifyNunitSupportFsUnit.fs(11,0): at FsUnit.should[a,a](FSharpFunc`2 f, a x, Object y) C:UsersStephenDocumentsVisual Studio 2010ProjectsUnquoteVerifyNunitSupportVerifyNunitSupport.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 <> 1false C:UsersStephenDocumentsVisual Studio 2010ProjectsUnquoteVerifyNunitSupportVerifyNunitSupport.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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-07 15:01