Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Utilizing Resources during unit tests [closed]

Im new to Unit testing with VS2010 & C#. I'm trying to load a file which should be tested. Is there a maven like approach? Or whats best practice?

How should i store and load my test resources?

My approach would be to create a new folder in the test-project like

+MyTestProject
+-Properties
+-....
+-Resources
 +- MyFile.xml
 +- OtherFile.xls
 +-...
+...

// EDIT:

I'm trying to test my code against generated content, so i'm loking for a possibility to load this content as it is into my unit test framework [ClassInitialize()] and pass them for each unit test [TestInitialize()]. So i really want to load the files on disk. Mocking is also a good hint, but for this case, i prefer the other way.

like image 215
MemLeak Avatar asked Jan 31 '26 02:01

MemLeak


1 Answers

There are a couple of approaches you can use. One is to deploy the files as embedded resources and extract them at test-run time in a fixture/test class initialization method. This approach is very reliable (i.e., it doesn't matter where the test assembly is run), but is slightly more complicated (you need to mark the files as embedded resources, you need to learn about the naming convention used when embedding file-based resources into an assembly, and you need to familiarize yourself with Assembly.GetManifestResourceStream()).

Alternately, you can use "loose files" (i.e., not embedded). In this approach, you simply mark the file as "Copy Always," or "Copy if Newer" - this will result in the file being copied to the build output directory for your test assembly. If you are using NUnit you can stop there. If you are using MSTest (which is what comes with Visual Studio), you'll need to add a [DeploymentItem] attribute to your test class or method:

  [DeploymentItem("MyFile.xml"[, "someoptionalsubfolder"])]
  public void MyTest()
  {
      ...
  }

In both of the above, I recommend that you do adopt a folder structure/naming convention for your resources to help you keep track of them. Something like the below has worked well for me. It's cribbed from the folder structure that ASP.NET MVC uses and works pretty well. In this approach, you can have "shared" resources (i.e., something that is constant for a set of tests at the assembly or test class level) but each set of tests or even each test may have its own specific version of a resource (e.g., for testing "invalid data" responses or testing edge-case scenarios):

  +-Resources
    +-Shared
      - SomeGlobalResource.xml
    +-TestClass1
      +-Shared
        - MockData.xml
      +-TestCase1
        - SpecialVersionofMockData.xml
      +-TestCase2
        - MockDataMissingRequiredInformation.xml
      +-TestCase3
        - EtCetera.xml
      +-...
    +-TestClass2
      +-Shared
      +-TestCase1
      +-TestCase2
      +-...
    +-TestClass3
    ...

By the way, if you follow this approach, this greatly simplifies how you can use the DeploymentItem attribute under MSTest. Instead of driving yourself insane and maintaining an attribute for each resource file, you can simply place a single directive in your class that copies the entire Resource folder tree:

  [DeploymentItem("Resources\\", "Resources")]
  [TestClass]
  public class MyTestClass()
  {
      ...
  }

Edit: I should point out that you're still required to manage the paths to your test content - neither NUnit nor MSTest provide tooling to auto-generate paths based upon the currently executing test. This means you'll have to be vigilant about maintaining correct paths when copy-pasting any test code.

like image 60
Andy Hopper Avatar answered Feb 01 '26 14:02

Andy Hopper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!