【问题标题】:F# Theory with custom AutoFixture.InlineAutoData doesn't show up in Test Explorer带有自定义 AutoFixture.InlineAutoData 的 F# Theory 未显示在测试资源管理器中
【发布时间】:2016-08-24 21:39:08
【问题描述】:

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

我发现这个 StackOverflow 问题看起来很相似,但它并不能帮助我解决我的问题: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。

感谢您的帮助。

【问题讨论】:

  • 你有没有发现的 C# 测试?你安装了 xUnit Visual Studio 运行器吗?
  • 我的 self-answered semi-related question 使用 F#、xUnit 和 VS2015(但不使用 AutoFixture)

标签: f# xunit.net autofixture


【解决方案1】:

我可以重现这个问题,至少在我的重现中,问题是版本冲突。大多数 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>

【讨论】:

  • 感谢您的快速回答。它适用于 appconfig。下次遇到问题时,我会尝试从命令行启动测试。
猜你喜欢
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 2020-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多