AutoFixture in F# UnitTest Project Not Displaying

2019-02-23 18:01发布

问题:

I have a Visual Studio 2012 Project and the following NuGet Packages installed:

  • AutoFixture with Auto Mocking using Moq
  • Autofixture with xUnit.net data theories
  • AutoFixture
  • Moq
  • xUnit.net: Extensions
  • xUnit.net: Runners
  • xUnit.net

Given the following contrived Logger class (Logger.fs):

namespace FUnit

type public ILoggerContext =
    abstract member LogPath :string

type public LoggerContext (logPath :string) =
    member val LogPath = logPath with get, set

    interface ILoggerContext with
        member this.LogPath = this.LogPath

type public Logger () =
    member this.Log (context: ILoggerContext) value =
        System.String.Format("Logged {0} to {1}.", value, context.LogPath)

and the following unit test:

namespace FUnit.Test

open FUnit

type public Math_Add() = 

    [<Xunit.Extensions.Theory>]
    [<Ploeh.AutoFixture.Xunit.AutoData>]
    member this.``Should Output Value And Path`` (path :string) =
        let context = new LoggerContext(path)
        let logger = new Logger()

        let expected = System.String.Format("Logged value to {0}.", path)
        let actual = logger.Log context "value"

        Xunit.Assert.Equal<string>(expected, actual)

The Test Explorer does not recognize the unit test after ensuring I'm showing all tests and building the project. The project builds correctly with no errors or warnings in the Build, General, or Test output logs.

If I replace the current Theory and AutoData attributes with an Fact attribute, the test shows up.

Is AutoFixture supported in F# test projects? Can anyone else replicate this and know what I'm doing wrong?

回答1:

I think this is an issue with conflicting versions of Xunit.Extensions used by Ploeh.Autofixture.Xunit and the test runner.

If you run the tests using Xunit's runners then you should see an exception complaining about not being able to find a specific version of Xunit.Extensions.

You can fix this by adding binding redirects to your test projects app.config. Since you're using NuGet you can have it generate the binding redirects by running this in the Package Manager Console for your test project.

Add-BindingRedirect

You can then tweak the generated binding redirects in app.config to be more specific if you want.

Once you rebuild the project you should see the test appear in VS Test Explorer.