Resharper Unit Test Runner Can't Find Content Files

前端 未结 3 710
有刺的猬
有刺的猬 2021-01-11 15:49

I have some tests that rely on some files I have marked as \"Content\" and to \"Always Copy\". I\'m using the DeploymentItem attribute to make sure they get copied to the ou

3条回答
  •  心在旅途
    2021-01-11 16:25

    I have also struggled with NUnit-based tests where I have files in the test project which I want to to be read in as part of the test.

    Running via NCrunch works fine, but with Resharper, it cannot find the file as it's using another location (such as C:\Users\myuser\AppData\Local\JetBrains\Installations\ReSharperPlatformVs15_f6172a1d_000).

    After tearing my hair out, I finally found the solution. Instead of using

    Environment.CurrentDirectory
    

    or

    System.Reflection.Assembly.GetEntryAssembly().Location
    

    There is a built-in property in NUnit:

    TestContext.CurrentContext.TestDirectory
    

    Now, everything is consistent across NCrunch, ReSharper and the built-in Visual Studio Test Explorer! (Reminder: you do still have to set "Build Action" = "Content" and Copy to Output Directory" = "Copy Always")

    Hopefully there is an equivalent in other test libraries.

提交回复
热议问题