Team System Unit Tests and Deployment Items

Published 24 February 07 03:24 PM

Just a quick tip in case you haven't yet encountered this yet. In the course of my development rhythm, I use both Visual Studio Team Edition for Software Testers and TestDriven.Net as test harnesses for my unit tests. This is primarily due to speed. As I'm sure you're aware, the VSTE test engine takes a good bit longer to initialize than TD.NET and because my programming style is very TDD in nature, I like to run my tests more frequently, and from the test code itself. Now, I ran into a situation today where I wanted to do some testing on an Xml schema that I wrote. Therefore, I have the following test.

 
   1:  [WorkItem(93), TestMethod]    
   2:  public void testGetAssetTypeSchema() {    
   3:      XmlSchema assetTypeSchema = 
   4:        ((IXmlSerializable) new AssetType("temp")).GetSchema();    
   5:      
   6:      XmlReaderSettings xmlSettings = new XmlReaderSettings();    
   7:      xmlSettings.CloseInput = true;    
   8:      xmlSettings.IgnoreComments = true;    
   9:      xmlSettings.IgnoreWhitespace = true;    
  10:      xmlSettings.Schemas.Add(assetTypeSchema);   
  11:      xmlSettings.ValidationFlags = 
  12:        XmlSchemaValidationFlags.ProcessIdentityConstraints;   
  13:      xmlSettings.ValidationType = ValidationType.Schema;   
  14:      xmlSettings.ValidationEventHandler += 
  15:        new ValidationEventHandler(settings_ValidationEventHandler);   
  16:     
  17:      XmlReader xrdr = XmlReader.Create("Workstation.xml", xmlSettings);   
  18:      XmlDocument xdoc = new XmlDocument();   
  19:      xdoc.Load(xrdr);   
  20:  }

My "Workstation.xml" file sits in the project directory along with everything all of the rest of my code files. I run my test using TD.NET and I get a file IO exception – my Xml file could not be located. Oh – woops! I forgot to change the "Copy to Output Directory" property. This ensures that when I build my project, the Xml file will always get copied into the output directory along with my assemblies. To do this, I select my Xml file in the solution explorer, and look at the properties window. I can then find the "Copy to Output Directory" setting and change it to "Copy always".

Now, at this point, my test will run just fine with TD.NET because that test harness runs my tests "in place". However, VSTE creates an entirely different folder structure to manage test runs and test results. Therefore, when I run my test with VSTE, my test fails again. To correct the problem, I need to add one more attribute.

[WorkItem(93), DeploymentItem("Workstation.xml"), TestMethod] 
public void testGetAssetTypeSchema() { ... }

The "DeploymentItemAttribute" will cause the VSTE test engine to copy my Xml file from the assembly output folder to the test deployment folder – and therefore the relative path I specify in my test still works flawlessly.

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Team System News said on February 27, 2007 9:40 AM:

Eric Lee on Streams and Branches Part 1. Brian Harry on More TFS Channel 9 Videos. Howard Dierking...

# Mike Clark said on August 8, 2007 6:41 AM:

That does seem to work, under the condition that the files you're using for testing are in the root of your test.  If your test files are in a Subfolder such as "\SomeProject_UnitTests\DummyFiles\Workstation.xml", there are problems.

Setting the "Copy Always=true" will move the Workstation.xml file to:

\TestResults\USER_MACHINE_DATETIME\Out

instead of:

\TestResults\USER_MACHINE_DATETIME\Out\DummyFiles

This causes the VSTE tests to fail, as the file is not located at ".\\Dummyfiles\\Workstation.xml"

The actual output of the test project can have a post-build step such as:-

xcopy "$(ProjectDir)DummyFiles\*.*" "$(TargetDir)DummyFiles\*.*" /Y

Which will output the correct structure... causing the testst to pass within nUnit.

So I guess my question would be... do you know of a way to tell VSTE that "Copy-Always" should keep the directory structure of the project?

# Mike Clark said on August 9, 2007 6:36 AM:

I've come up with a solution to my problem (described above)...

In the test I specify a relative path of ".\\Files\\Workstation.xml"

Then, in the VSTE unit-tests project I have a structure similar to:-

\SomeTest.cs

\Consumables\Files\Workstation.xml

You can then double click on the .testrunconfig file within VSTE.  Inside there's a "Deployment" section.  You can point this to the "Consumables folder.  Running the tests in VSTE will then build:-

\TestResults\USER_MACHINE_DATETIME\Out\myproject.dll

\TestResults\USER_MACHINE_DATETIME\Out\Files\Workstation.xml

Which allows the tests to run fine in VSTE.

Then, in the unit-test project "properties", I specify a "post-build" of:-

xcopy "$(ProjectDir)Consumables\Files\*.*" "$(TargetDir)Files\*.*" /Y

Which creates:-

\myproject\bin\Debug\myproject.dll

\myproject\bin\Debug\Files\Workstation.xml

Allowing for the unit tests to be run outside of VSTE.  (To do this manually I copy Microsoft.VisualStudio.QualityTools.Resource.dll and Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll into my nunit bin folder).  CruiseControl.net also works happily in this configuration.

:)

# Mike Clark said on August 23, 2007 5:14 AM:

Also, remember to add the /E switch to the post-build step of your unit test project, otherwise subfolders (and their content) required for the tests will not be copied:-

xcopy "$(ProjectDir)Consumables\Files\*.*" "$(TargetDir)Files\*.*" /Y /E

# Onur Yılmaz said on May 21, 2008 12:15 AM:

Only adding  " [TestMethod, DeploymentItem("örnekKaynakKodHatali1.txt")] " on my test case didn 't work. And when I ran my test case, VSTS couldn't find my *.txt file.

So I double-clicked "localtestrun.testrunconfig" file in VSTS and from "Deployment" tab I selected this file expilicity.

This method works for me.

# IvoG said on July 28, 2008 10:46 AM:

When you want items deployed to directories use the DeploymentItem attribute with 2 parameters, u can use the second parameter to specify a directory where the deploymentitem needs to be copied to.

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

About hdierking

I am currently the Editor-in-Chief for MSDN Magazine. I joined Microsoft in 2006 as a product planner with the certification team at Microsoft Learning. Prior to that, I spent my career as a developer and later as an architect. My main technology passions include pretty much anything on language theory, agile development, and service-oriented architecture.
Page view tracker