具有自定义 AutoFixture.InlineAutoData 的 F# Theory 未显示在测试资源管理器中

F# Theory with custom AutoFixture.InlineAutoData doesn't show up in Test Explorer

我正在尝试使用 xUnit2 和 AutoFixture 在 F# 中编写一些单元测试,但我遇到了问题。我有一个具有继承自 InlineAutoData 的自定义属性的理论,测试资源管理器一直告诉我没有找到测试。如果我将 Theory 属性替换为 Fact 属性,它仍然不起作用,但如果我删除自定义 InlineData 属性,则会发现测试。测试用 F# 编写,但属性在另一个项目中用 C# 编写。

我发现这个 Whosebug 问题看起来很相似,但它并不能帮助我解决我的问题:AutoFixture in F# UnitTest Project Not Displaying Unit Tests in Test Explorer

单元测试声明如下:

[<Theory>]
[<SyntaxTreeInlineAutoData("Class/SingleClass.cs", 1)>]
[<SyntaxTreeInlineAutoData("Class/MultipleClass.cs", 2)>]
[<SyntaxTreeInlineAutoData("Class/NestedClass.cs", 2)>]
let ``Inspect_WhenVariousContexts_WithSuccess`` (count, tree : SyntaxTree) = 

这是属性声明:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class SyntaxTreeInlineAutoDataAttribute : InlineAutoDataAttribute
{
    #region Constructors

    public SyntaxTreeInlineAutoDataAttribute(string sourceFile, params object[] values) 
        : base(new SyntaxTreeAutoDataAttribute(sourceFile), values)
    {
    }

    #endregion
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class SyntaxTreeAutoDataAttribute : AutoDataAttribute
{
    #region Constructors

    public SyntaxTreeAutoDataAttribute() : this(null)
    {
    }

    public SyntaxTreeAutoDataAttribute(string sourceFile) 
        : base(new Fixture().Customize(new SyntaxTreeCustomization(sourceFile)))
    {
    }

    #endregion
}

[编辑]

该项目是 C# 项目的端口。单元测试在自定义属性下运行良好。该错误仅发生在用 F# 编写的测试中。

一切都已安装:xUnit2、xUnit.runners.visualstudio 和 AutoFixture。

感谢您的帮助。

我可以重现这个问题,至少在我的重现中,问题是版本冲突。大多数 Visual Studio 测试 运行 人员不会告诉您这一点,但是如果您尝试 运行 使用命令行 运行 单元测试,您会看到报告的实际错误。

在我的重现中,我通过将以下 app.config 文件添加到我的 F# 项目来解决问题:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="xunit.core" publicKeyToken="8d05b1bb7a6fdb6c" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.0.3179" newVersion="2.1.0.3179" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>