Load file during Unit Test

Using Unity 2018.1.1f1 and Visual Studio Community 2017

I’ve been creating unit tests using unity’s built in Test Runner. In this particular unit test, I want to read a file’s content into a string. Without having to build a static path to the file (which will sit next to the unit test class itself), how can I reference it?

The folder structure looks like this:



The test and some output details:

namespace MyTools.Common.Properties.Tests {
    public class PropertiesTests {
        public void ParsePropsFile() {
            // MyTools.Common.Properties.Tests, Version=, Culture=neutral, PublicKeyToken=null
            // 0
            // 0

In visual studio, I’ve tried to set the properties (Build Action = Embedded Resource, and Copy to Output Directory = Copy Always) of test-prop-file.txt and test.txt but they do not seem to do anything. Also, when I close VS and reopen it, these are set back to what they were before (None and Do Not Copy). I’ve also tried other combos like Build Action = Resource or Content but still nothing.

So how can I go about loading these files in the unit test without referencing them directly with a static path like D:/Code/Unity/MyProject/Assets/Plugins/MyTools/Common/Properties/_tests/test.txt?

I know that is an old question but there are not many answers to this problem so I want to save some time to others. It is only workaround but it solves my problem. Its goes up two folders from location where test are run "Library\ScriptAssemblies" to the Assets folder and then to the place where my text file is stored. Hope this help!

string cofigPath = Path.GetFullPath(Path.Combine(TestContext.CurrentContext.TestDirectory, "..", "..", @"Assets\Tests\TestData\config.json"));

Credits belongs to Maxime: c# - Load file during Unit Test in Unity3d's Unity Test Runner - Stack Overflow