无法在 Visual Studio 中进行 运行 测试

Unable to run tests in Visual Studio

我创建了一个包含 2 个项目的解决方案,FSharpCalled(其中包含一个方法 "disBonjour",在另一个项目中转换字符串)、一个 F# 库和 FSharpTest,如下所示:

这是 FSharpCalled :

问题是我无法 运行 测试;如第一个屏幕截图所示,我收到一条错误消息,指出 "principal module is empty, so nothing will happen at execution".

我尝试修改一些属性但没有成功。

编辑:

这是结果...该死!

当您在 class 中使用 let 绑定时,let 绑定是私有的(即不是 public容易访问)。因此,您不能将其用作测试方法。

如果您想在 F# 中使用带有 class 的 NUnit,您必须使用 方法 修饰 [<Test>] 属性:

[<TestFixture>]
type TestClass() = 
    [<Test>]
    member this.SomeTest () =
        // Test goes here
        ()

上次我使用 NUnit 时,您还 [<TestFixture>] 属性添加到 class 本身,所以我也添加了它。一般情况下,我不用NUnit,所以不知道是否还需要。

模块

您还可以在模块中编写测试,在这种情况下,您可以可以使用[<Test>]属性装饰let绑定函数,因为在那如果它编译为 public,静态成员:

module MyTests =
    [<Test>]
    let ``a function in a module that works as a test`` () =
        // Test goes here
        ()

至少,我使用的测试运行程序 (TestDriven.net) 也会发现并运行该测试。我不知道所有其他测试运行器是否也会这样做。

有关使用 F# 进行单元测试的一般介绍,以及更多内容,您可以考虑观看我的 Pluralsight course called Unit Testing with F#

principal module is empty, so nothing will happen at execution

当您有一个没有 [<EntryPoint>] 的控制台项目时发生。因此,您需要声明这样一个主要方法或将项目更改为库项目。

更新

您是否使用任何第 3 方测试运行器(TestDriven、带有 nUnit 集成的 ReSharper,...)或 nUnit VS Test Adapter? VS 无法立即识别 nUnit 测试。