How to refer to test files from Xunit tests in Visual Studio?

前端 未结 4 967
抹茶落季
抹茶落季 2021-02-03 21:34

We’re using Xunit for testing. We’re running our tests via the built-in Visual Studio 2013 Test Runner, using the Xunit plugin.

The issue is that some of the tests need

4条回答
  •  Happy的楠姐
    2021-02-03 21:56

    Okay, typical, just as soon as I post the question, I find the answer myself…

    The gist is that the copying (Shadow Copying) of assemblies seems to be done by the .NET framework, not by Visual Studio or by Xunit.

    We had been using Assembly.Location to locate the assembly file, and hence the test files. However, this was wrong, since it gave us the location of the Shadow-Copied assembles instead of the originals.

    Instead you should use Assembly.CodeBase to fetch the base assembly code location. However, this is a (File) URL, so it’s necessary to extract the path from the URL. The new (C#) code looks like this:

    var codeBaseUrl = new Uri(Assembly.GetExecutingAssembly().CodeBase);
    var codeBasePath = Uri.UnescapeDataString(codeBaseUrl.AbsolutePath);
    var dirPath = Path.GetDirectoryName(codeBasePath);
    return Path.Combine(dirPath, relativePath);
    

    …where relativePath is the path relative to the Bin\ directory.

提交回复
热议问题