How to use external data in unit tests?

I have many unit tests that need a lot of txt, data, html files, etc. Externally saving these files makes it easy to update test cases by adding new test cases, etc.

However, the presence of dependencies in Unit Tests brings a lot of headache in different systems and in different test groups.

What are the best practices?

  • External storage and relative linking of these files into code? (causing problems in some test runners, or requires additional configuration)
  • Attach all of these files to Unit Test dll and read from there (simplifies test creation).
  • Saving location in hard code (obviously causing so much trouble checking code elsewhere)

How do you solve this problem?

+3
source share
2 answers

I use a local folder in my test project and get test files with code like:

public static FileInfo GetTestFileInfo(string fileName)
{
    var dir = AppDomain.CurrentDomain.BaseDirectory;
    return new FileInfo(dir + @"\..\..\TestData\" + fileName);
}

Oh yes, I use MbUnit.

+3
source

My practice was to inject test resources into unit-test assemblies and pull them out using GetManifestResourceStream .

NUnit testing is device-oriented in any case, so once you have a device (i.e. a specific set of resources), adding additional tests is easy.

0
source

Source: https://habr.com/ru/post/1711095/


All Articles