Team System Unit Tests and Deployment Items
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.
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.