是否可以在.net core中对F#项目进行单元测试? [英] Is there a way to unit test F# projects in .net core?
问题描述
我正在尝试创建一个在.net核心中运行的F#单元测试项目。
I am trying to create an F# unit test project that runs in .net core.
dotnet new -t xunittest
将为C#创建一个xunit测试项目,但是对于F#不存在这样的等效项目。
will create an xunit test project for C#, but no such equivalent exists for F#.
我试图修改project.json和测试文件,这些文件从上面显示的C# dotnet new中获取输出,并添加我认为需要使其与F#一起运行的位。没用在输入 dotnet测试后,它会构建甚至运行一个测试,但是即使该测试不应该通过,测试也始终会通过。
I tried modifying the project.json and test file that get output from the C# "dotnet new" shown above, and add the bits I thought would be needed to make it run with F#. It didn't work. It builds and even "runs" a test after typing "dotnet test", but the test ALWAYS passes even when it shouldn't.
我在安装程序中缺少某些内容吗? ,还是我可以采用另一个(也许是完全不同的)选项来创建F#测试项目?再次,我特别专注于使其与.net core一起使用。
Am I missing something with the setup, or is there another (perhaps completely different) option I can pursue in order to create an F# test project? Again, I am specifically focused on making it work with .net core.
project.json
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true,
"compilerName": "fsc",
"compile": {
"includeFiles": [
"Tests.fs"
]
}
},
"dependencies": {
"System.Runtime.Serialization.Primitives": "4.3.0",
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-*"
},
"tools": {
"dotnet-compile-fsc": "1.0.0-preview2.1-*"
},
"testRunner": "xunit",
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
},
"Microsoft.FSharp.Core.netcore": "1.0.0-alpha-160629"
},
"imports": [
"dotnet5.4",
"portable-net451+win8"
]
}
}
}
Tests.fs
module Tests
open Xunit
[<Fact>]
let ``Should Fail`` =
Assert.False true
dotnet测试的输出
xUnit.net .NET CLI test runner (64-bit .NET Core win10-x64)
Discovering: FSTests
Discovered: FSTests
=== TEST EXECUTION SUMMARY ===
FSTests.dll Total: 0
SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.
推荐答案
您的测试不是函数,而是值。编译为IL作为属性或字段(取决于原因)。 xUnit寻找要执行的方法,它会跳过属性和字段(正确的是:它将如何执行它们?)
Your test is not a function, but a value. Compiled to IL as a property or field (depending on reasons). xUnit looks for methods to execute, it skips properties and fields (and rightly so: how would it execute them?)
为什么它不是函数?因为它没有参数。这是F#的事情:如果您有参数,那么您就是一个函数。否则-值。
Why is it not a function? Because it doesn't have a parameter. It's an F# thing: if you have parameters, you're a function. Otherwise - value.
只要给它一个参数,它将成为一个函数。没有要添加的有意义的参数吗?添加一个 unit
一个-然后它将作为无参数静态方法编译为IL。
Just give it a parameter, it will become a function. Don't have any meaningful parameters to add? Add a unit
one - then it'll be compiled to IL as a parameterless static method.
[<Fact>]
let ``Should Fail`` () =
Assert.False true
这篇关于是否可以在.net core中对F#项目进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!