【问题标题】:How do we statically initialize test data before calling a test method?我们如何在调用测试方法之前静态初始化测试数据?
【发布时间】:2018-08-17 11:37:17
【问题描述】:

我们设置了以下测试。

Permutations.Tests.fsproj

<ItemGroup>
  <Compile Include="Permute1Tests.fs" />
  <Compile Include="Permute2Tests.fs" />
</ItemGroup>

Permute1Tests.fs

module Permute1Tests

open Xunit
open Permutations.Permute1

[<Theory>]
[<MemberData("permuteTestValues")>]
let ``permute`` (x, expected) =
    let actual = permute x
    Assert.Equal<List<int>>(expected, actual);

let permuteTestValues : obj array seq =
    seq {
        yield [| [0;1]; [[0;1]; [1;0]] |]
    }

Permute2Tests.fs

module Permute2Tests

open Xunit
open Permutations.Permute2

[<Theory>]
[<MemberData("removeFirstTestData")>]
let ``removeFirst`` (item, list, expected: List<int>) =
    let actual = removeFirst list item
    Assert.Equal<List<int>>(expected, actual)

let removeFirstTestData : obj array seq =
    seq {
        yield [| 0; [1;2;3;4]; [1;2;3;4] |]
    }

当我们运行dotnet test 时,这是错误:

System.InvalidOperationException:Permute2Tests.removeFirst 的测试数据返回 null。确保在调用此测试方法之前对其进行静态初始化。

奇怪的是,Permute1Tests.fs 运行时没有错误。它的测试通过了。而且,如果我们将ItemGroup 中的Permute1Test.fs 位置与Permute2Test.fs 交换,那么后者现在可以工作,而前者会出现错误。

我们如何在调用测试方法之前静态初始化测试数据?似乎ItemGroup 顺序在我们当前的方法中很重要,这使得我们当前的方法失败了。

以上代码的完整版is here

编辑:ILSpy 输出

Permute1Tests.fs.cs

// <StartupCode$Permutations-Tests>.$Permute1Tests
using <StartupCode$Permutations-Tests>;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;

internal static class $Permute1Tests
{
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    internal static readonly IEnumerable<object[]> permuteTestValues@12;

    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    [CompilerGenerated]
    [DebuggerNonUserCode]
    internal static int init@;

    static $Permute1Tests()
    {
        IEnumerable<object[]> permuteTestValues = 
            $Permute1Tests.permuteTestValues@12 = 
                (IEnumerable<object[]>)new Permute1Tests.permuteTestValues@14(0, null);
    }
}

Permute2Tests.fs.cs

// <StartupCode$Permutations-Tests>.$Permute2Tests
using <StartupCode$Permutations-Tests>;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;

internal static class $Permute2Tests
{
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    internal static IEnumerable<object[]> removeFirstTestData@15;

    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    [CompilerGenerated]
    [DebuggerNonUserCode]
    internal static int init@;

    public static void main@()
    {
        IEnumerable<object[]> removeFirstTestData = 
            $Permute2Tests.removeFirstTestData@15 = 
                (IEnumerable<object[]>)new Permute2Tests.removeFirstTestData@17(0, null);
    }
}

Permutations.Test.fsproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Permute1Tests.fs" />
    <Compile Include="Permute2Tests.fs" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
    <PackageReference Include="xunit" Version="2.3.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
    <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Permutations\Permutations.fsproj" />
  </ItemGroup>

</Project>

【问题讨论】:

  • 你能用ILSpy看一下编译后的代码,看看这两个模块是否在某些方面有所不同吗?如果这不能让您发现问题,请将 ILSpy 反编译结果发布到 C#。
  • 我可以确认这两个模块没有区别。最不寻常的是,如果我们交换 *.fsproj ItemGroup 列表中两个文件的顺序,那么错误会发生在相反的文件中。这证明模块之间的唯一区别是它们出现在 ItemGroup 列表中的顺序。 @FyodorSoikin
  • 能把ILSpy反编译的结果贴出来吗?
  • @FyodorSoikin 我已经添加了 ILSpy 输出,它确实提供了一些见解。
  • 几乎明白了!您还可以发布您的fsproj 文件的完整 内容吗?

标签: f# .net-core xunit


【解决方案1】:

为什么

这与您的程序集是“可执行文件”(即具有入口点的程序)而不是“库”这一事实有关。

F#编译可执行文件与库略有不同:在入口点所在的模块中,所有静态数据都在main函数内初始化,然后再执行其他所有操作;在所有其他模块中,静态数据在静态构造函数中初始化。我不确定这个决定背后的原因是什么,但这就是 F# 编译器的行为方式。

接下来,F# 编译器如何确定哪个模块包含入口点?非常简单:无论哪个模块是最后一个,这就是入口点所在的位置。想一想,这是唯一明智的选择:由于 F# 具有编译顺序,因此只有最后一个文件可以访问所有其他文件中的定义;因此,这就是入口点必须位于的位置。

因此,在您的示例中,无论列表中的最后一个模块,都以 main 函数结束,静态初始化代码位于该函数中。而且由于单元测试运行程序在执行测试之前不运行入口点,因此该模块中的静态数据保持未初始化。

解决方案 1:添加一个人工模块来包含入口点

正如您已经发现的那样,一种解决方案是添加一个只包含入口点的人工模块。这样,测试模块将不再是最后一个,不会包含入口点,因此其数据将在静态构造函数中初始化。

人工模块甚至不必有[&lt;EntryPoint&gt;] main 函数,它可以是这样的:

module Dummy
let _x = 0  // `do ()` would be even shorter, but that will create a warning

编译器无论如何都会添加一个入口点。

方案二:编译成netstandard2.0

如果您将目标从netcoreapp2.0 切换到netstandard2.0,您的程序集将被视为“库”而不是“可执行文件”,并且编译器不会添加入口点,也不会放置静态在里面初始化。

【讨论】:

    猜你喜欢
    • 2016-11-10
    • 2017-10-12
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多