Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative path on unit test project c#

I have a solution which has a console application and unit test.

var target = new Validator();
var targetFile = @"C:\Folder\SubFolder\AnotherSubFolder\Development\DEV\src\UnitTest\TargetFolder\file.xml";

bool actual = target.Validate(targetFile);
Assert.AreEqual(true, actual);

The Validator is located at the console application project, while my file.xml is located at the folder inside the unit test project.

This unit test is passing, however i want to make sure that when i check in this in to my tfs or build server, it can still locate my targetFile.

How am I gonna change the path of the targetFile?

Thanks in advance!

like image 387
Gerald Avatar asked Jul 05 '13 03:07

Gerald


1 Answers

If I understood your issue correctly, it is not good that product code(in console application) depends on an item in the test project, so I recommend you to move file.xml to the product code project.

To solve your path problem, I think the best approach is to use the Copy to Output Directory property as Copy always or Copy if newer, which you can find in the VS Properties window.

Check out the following steps.

  1. Make a folder if needed to put in file.xml in the product project(eg. Data folder).
  2. Put file.xml in the folder.
  3. Go to the VS Properties window for file.xml.
  4. Set Build Action to None and Copy to Output Directory to Copy always or Copy if newer.
  5. Build and do unit testing as

    Assert.IsTrue(File.Exists(@"Data\file.xml"));
    

    In this case, VS will automatically make the data folder with file.xml under output folder of test poject.

like image 129
Jin-Wook Chung Avatar answered Oct 27 '22 18:10

Jin-Wook Chung