有没有办法在 .net core 中对 F# 项目进行单元测试? [英] Is there a way to unit test F# projects in .net core?

查看:28
本文介绍了有没有办法在 .net core 中对 F# 项目进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个在 .net core 中运行的 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#.

我尝试修改从上面显示的 C#dotnet new"获取输出的 project.json 和测试文件,并添加我认为需要的位以使其与 F# 一起运行.它没有用.它在键入dotnet test"后构建甚至运行"测试,但即使不应该通过测试,它也始终通过.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆