Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Reference File in Unit Test?

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?

like image 644
chobo2 Avatar asked Jan 18 '17 21:01

chobo2


People also ask

Should unit tests read files?

Strictly speaking, unit tests should not use the file system because it is slow. However, readability is more important.

Where do I put unit test files?

Unit tests run against specific lines of code. So it makes sense to place them right next to that code. Integration tests run against many lines of code in many files. There is no single place that would make sense, so it's best to have them in a /tests directory.

How do I run a unit test File?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).

How do I create a unit test File?

On the File menu, select New > Project, or press Ctrl+Shift+N. On the Create a new project page, type unit test into the search box. Select the project template for the test framework that you want to use, for example MSTest Test Project or NUnit Test Project, and then select Next.


1 Answers

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.

like image 96
Igor Kustov Avatar answered Oct 26 '22 05:10

Igor Kustov