【问题标题】:How to handle unit tests in F#?如何在 F# 中处理单元测试?
【发布时间】:2010-11-30 22:12:25
【问题描述】:

如何在 F# 中创建单元测试?我通常使用带有 [TestClass] 和 [TestMethod] 属性的 Visual Studio 的 UnitTest 部分,并使用测试视图来运行它们。我知道我可以创建一个脚本文件并运行它们,但我喜欢它目前的处理方式。

【问题讨论】:

标签: unit-testing f#


【解决方案1】:

在 VS2013 中你可以使用下面的。

open Microsoft.VisualStudio.TestTools.UnitTesting
[<TestClass>]
type testrun() = 
    [<TestInitialize>]
    member x.setup() =
        //your setup code

    [<TestMethod>]
    member x.yourTestName() =
        //your test code

提示:如果您正在寻找 UI 单元测试,那么您可以将此设置与 Canopy 一起使用。

【讨论】:

  • 这应该是公认的答案。这使用内置的单元测试功能。此外(不是很明显),这种方法需要添加对 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 的引用。
  • @alancnet 只有在您使用 Visual Studio 时才内置。
【解决方案2】:

我宁愿用FsUnit或者FsTest用F#写测试,感觉比OOxUnit风格的测试更自然。

EDIT 2014:我现在认为 FsUnit/FsTest 几乎是无用的语法糖。而“比 OO 更自然”并不意味着什么。几个月前,我写了我目前关于测试here 的想法(我建议阅读整个帖子)。

【讨论】:

    【解决方案3】:

    我使用xUnit.netTestDriven.Net(用于运行测试的 Visual Studio 插件,对“学生、开源开发人员和试用用户”免费)和我自己的开源 Unquote 库(其中也适用于 NUnit 和任何其他基于异常的断言框架)。效果很好,而且上手非常简单:

    1. 下载并安装 TestDriven.Net
    2. 下载xUnit.net,解压到任意位置,运行xunit.installer.exe与TestDriven.Net集成
    3. 下载Unquote,解压到任意位置
    4. 在您的解决方案中为单元测试创​​建一个项目
    5. 在您的单元测试项目中添加对 xunit.dll 和 Unquote.dll 的引用(来自解压缩的下载)
    6. 以下是单元测试项目中包含 xUnit.net / Unquote 样式单元测试的 .fs 文件的简单示例。

      module Tests
      open Swensen.Unquote
      open Xunit
      
      [<Fact>]
      let ``description of first unit test`` () =
          test <@ (11 + 3) / 2 = String.length ("hello world".Substring(4, 5)) @>
      
      [<Fact>]
      let ``description of second unit test`` () =
          let x = List.rev [1;2;3;4]
          x =? [4;3;1;2]
      
    7. 通过在解决方案资源管理器中右键单击项目并选择运行测试来运行项目中的所有单元测试。前面的两个示例测试都将失败,并在 Visual Studio 输出窗口中打印以下内容:

      ------ Test started: Assembly: Tests.dll ------
      
      Test 'Tests.description of second unit test' failed: 
      
      [4; 3; 2; 1] = [4; 3; 1; 2]
      false
      
          C:\Solution\Project\Tests.fs(12,0): at Tests.description of second unit test()
      
      Test 'Tests.description of first unit test' failed: 
      
      (11 + 3) / 2 = String.length ("hello world".Substring(4, 5))
      14 / 2 = String.length "o wor"
      7 = 5
      false
      
          C:\Solution\Project\Tests.fs(7,0): at Tests.description of first unit test()
      
      0 passed, 2 failed, 0 skipped, took 1.09 seconds (xUnit.net 1.7.0 build 1540).
      

    【讨论】:

      【解决方案4】:

      您可能想试试NaturalSpec。它是基于 NUnit 的 F# UnitTest-Framework。

      【讨论】:

        【解决方案5】:

        从 2.5 版开始,NUnit 允许您使用 static members as tests。此外,类级别的 TestFixtureAttribute 是only necessary for generic or classes with non-default constructors。 NUnit 也有一个backward-compatible convention,一个测试成员可能以单词“test”开头而不是使用 TestAttribute,所以你几乎可以用 NUnit > 2.5 编写惯用的 F#。

        更新 您可以在Cashel library 中看到一些没有 TestFixtureAttribute 的测试示例。我继续使用 TestAttribute,因为似乎很少有测试运行者在它不存在时正确地选择测试,因此 NUnit 帖子的一部分可能不正确或至少具有误导性。

        【讨论】:

        • Ryan,您想详细说明一下 F# 中的示例测试吗?还是指向显示没有 TestAttribute 或 TestFixtureAtrribute 的 F# 单元测试的站点的链接?
        • @David 我为我的 Cashel 测试添加了一个链接。
        【解决方案6】:

        查看fscheck。它是 Haskell 的 Quickcheck 的一个端口。 Fscheck 允许您指定函数必须满足的属性,然后它将根据“大量随机生成的案例”进行验证。

        这是使用 C# 等命令式语言无法轻松完成的事情。

        【讨论】:

        • @Mauricio:我同意可以在 OO 设置(另一个示例:RANDOOP)中执行 FsCheck 所做的(随机生成测试用例)。也许有一天我们会拥有一种规范语言(在我非常偏颇的观点中,它在功能设置中看起来更好),它可以生成随机生成的案例和覆盖案例,就像 Pex 所做的那样。
        • 现在 FsCheck 有了 C# 接口,因此您也可以在 C# 中使用 FsCheck 最重要的功能。
        【解决方案7】:

        试试XUnit.net

        【讨论】:

        • 同意。 (仅?)xUnit.NET 可以在静态方法中运行测试用例,所以如果你想留在主流的 .NET 单元测试框架中,那是最省力的。
        猜你喜欢
        • 1970-01-01
        • 2021-06-19
        • 2018-09-14
        • 2018-02-25
        • 2019-11-04
        • 2014-02-28
        • 2017-01-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多