VSTS Unit Tests with "extra" files

Sometimes you need to have an external file when you're creating unit tests. Perhaps you are reading a known state into an object, or reading in some XML to insert test data into a database.

If you are reading this blog, you probably already tried adding these “extra” files into your VSTS Test Project, and perhaps even thought to change the file's “Copy to Output Directory” to “Copy Always”. This has the effect of copying your extra file to the bin/debug (or release) folder underneath your project as part of a build. What you may not know is that your tests don't actually run from this directory. When VSTS runs a local test, a directory is created below the <SolutionFolder>/TestResults folder. There are a couple of techniques you can use to ensure your extra files show up in this directory.

  1. If your external file is specific to a certain test, is only required when you run that test, and doesn't change based on other environmental variables, then you can add a DeploymentItem attribute to your test method. There are a few overloads, check the documentation.
  2. If your external file is more of a global configuration file that many tests rely on, or if the external file has multiple variations you can edit your Test Run Configuration. The menu option is Test > Edit Test Run Configurations > Local Test Run. Select the Deployment item on the left, and then add a reference to the external file.

    By default you only have 1 local test run configuration, but you can add more. The localtestrun.testrunconfig file is usually located under the Solution Items folder. You can right click this folder in Solution Explorer and select Add New Item. There is a category for Test Run Configuration, and a corresponding item as well.