8

I am making some unit tests and I have a JSON file with some data in it. I am writing some unit tests that take that file and use that data as well.

So this data would be used live and for unit tests.

I don't want to maintain two copies if possible so I am wondering how can I reference this file?

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
chobo2
  • 83,322
  • 195
  • 530
  • 832
  • You could embed it as a resource in the "live" assembly. Kinda ugly, but it'd be available from both places and you wouldn't have to worry about the filesystem. – adv12 Jan 18 '17 at 21:42
  • 1
    Possible duplicate of [Unit testing: how to access a text file?](http://stackoverflow.com/questions/1805012/unit-testing-how-to-access-a-text-file) – JuanR Jan 18 '17 at 21:42

4 Answers4

10

I think that you are looking for the "Add as a Link" feature in the Visual Studio's Add -> Existing Item... dialog: enter image description here

Then you need to set the "Copy to Output Directory" parameter for this file to any value from these:

  • Copy always
  • Copy if newer

I.e. enter image description here

More details you can find in this MSDN article.

Igor Kustov
  • 3,228
  • 2
  • 34
  • 31
  • 1
    In Visual Studio 2017 the file is already added to the project automatically, when it is placed in the project directory. In this case only step 2 is necessary. It can be accessed by `Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "data.json")` – kap Apr 07 '19 at 20:59
  • 2
    @kap that is only true for the new VS Project types that utilize the CPS SDK project infrastructure. That is all of the newer .Net Standard and .Net Core projects. The older project types like the .Net Framework library & etc.., they will not automatically add new files that show up. You still have to manually include them into the project. – John C Apr 10 '19 at 06:35
  • 1
    you can also add this manually to your .csproj like this: Always – heug Aug 27 '19 at 20:32
5

I normally use:

[TestMethod]
[DeploymentItem(@"MyProject.Tests\TestFiles\file.txt")]
public void MyTest()
{
    var myfile= "file.txt";

    Assert.IsTrue(
        File.Exists(myfile),
        "Deployment failed: {0} did not get deployed.",
        myfile
        );
}

Then specify the file in the TestSettings.Settings file in the Deployment section.

This way, the unit test will work in Visual Studio and also from the command line.

Alfie
  • 2,341
  • 2
  • 28
  • 45
peval27
  • 1,239
  • 2
  • 14
  • 40
  • 1
    I am using nunit, not sure if they have this DeploymentItem – chobo2 Jan 18 '17 at 22:15
  • @chobo2 in that case you can have a look here: [link](https://dotnetchris.wordpress.com/2008/12/31/switching-from-ms-test-to-nunit-testing/) – peval27 Jan 18 '17 at 22:19
1

In Visual Studio right-click your project and choose 'Add->Existing Item'. Notice the 'Add' button is a drop-down button. One of the choices is 'Add As Link'. This will add the file to your project without copying it. On the file properties you can choose 'Copy if newer' for 'Copy to Output Directory'. You can then consume the file in your test without maintaining two copies.

Kip Morgan
  • 728
  • 3
  • 12
0

One option is to use a post-build step to copy the file to where it needs to be.

Also check out this article on how to deploy test files: https://msdn.microsoft.com/en-us/library/ms182475.aspx

Dave
  • 597
  • 7
  • 17