Team System Unit Test and Output Directories

14 07 2008

I always use NUnit to write my unit tests but now at Intergen I also use Microsoft Visual Studio Team System Unit Tests. Today I was stuck by a very basic problem and here it is.

I have a XML file in my project which is added to the output directory when compiled.

When I was running my unit tests against this, Visual Studio threw me this error saying that it couldn’t find the file!

(click to enlarge)

I was really surprised as my XML file is said to copy to output directory and the test fails saying that there is no such file. Checked my Test Solution’s output directory and found that there is indeed the Templates folder and the XML file.

So, whats the problem? Why is my test failing?

Well, the VSTE has a separate directory where it stores all its test results. The directory can be found in your root directory of your solution under the name TestResults. The TestResults also have various folders for each test run you do. If you refer back to the error image that is attached above, you could get the exact directory name. So, the VSTE did not copy the Templates directory to this ‘dynamic’ (for each test run there will be a directory created : correct me if am wrong here) directory. How do we do this then?

The answer is with the Local Test Run Config’s Deployment settings. You can find the local test run config in your solution browser.

Double click the file to open the properties dialog and go to the Deployment section.

Specifying a file or directory here would be included along with the test results :)

My test doesnt fail now 8)

Another way to do this (if its a single file : if you know how to add for a directory, please do leave your comment :) ) is to specify along with your test method,

[TestMethod, DeploymentItem(@"Templates\BirthInputTemplate.xml")]

public void Test_BuildInputXML()
{
}

About these ads

Actions

Information

14 responses

14 07 2008
Heiko Hatzfeld

Hello…

Another option I use for XML Files is to embedd them inside my Test classes.

I have a very “strict” view on using external resources in a unit test. If someone else “can” mess with the resource, then it is “external”. By using an embedded resource, you can make sure that noone can tamer with your XML file, and the data you test against will be the data you expect.

I know its a bit “picky”, but we had some problems with this (like ppl not fetching the latest XML out of the repository, since it was no exceutable… doh…)

But the point is, an “external” (-> Filesystem) resource caused the test to fail. So keep your testdata close to the test. And the closest I could think of, was embedding it inside my test.dll…

14 07 2008
Heiko Hatzfeld

Hello…

Another option you could use is to embed the XML-Files inside your test-dll. This would remove the dependency on the filesystem as an external resource.

I am using this option myself, since we already had some trouble with external XML files. The use of an embedded resource also ensures that there is no simple way for someone to modify your testdata, and cause the test to fail. And using an internal XML file is also very simple. You just have to get the stream of the embedded resource, and you are done with it.

But you should be aware that you get the stream from the “correct” assembly. I refactored that code out to my TestUtils class, and I had to change the logic for getting the resource. (from CurrentAssembly to Callingassembly)

Example Code to access the XML:

public static XmlDocument GetXMLDocument(string ResourceName)
{
System.IO.Stream stream =
(Assembly.GetCallingAssembly().GetManifestResourceStream(ResourceName));
if (stream == null)
throw new ArgumentException(“Unable to open the embedded Resource!”);
XmlDocument tmpXMLDoc = new XmlDocument();
tmpXMLDoc.Load(stream);
stream.Dispose();
return tmpXMLDoc;
}

Example Usage (“Complicated constuctor”, since the reader also accepts a XMLstring):

FakeDataReader dr = new FakeDataReader(FakeDataReader.GetXMLDocument(“Foo.Bar.Data.xml”));

14 07 2008
Pieter

Nice one Chaks

14 07 2008
Chaks

Thanks Pieter :)

14 07 2008
Chaks

@Heiko, Interesting points! Will try those tomorrow at work :)

27 08 2008
Vinodh

Very nice article. Thank you. I do have a question though. I changed the Local Test Run Config’s Deployment settings to include a directory (Using “Add Directory” button) by name “Templates”. However I don’t see the “Templates” folder being created within the OUT directory (C:/Harvest/Care/Regence/Qpex/QuotingProcess/TestResults/c618783_L68204 2008-08-26 14_45_04/Out). Instead the files within the Templates folder are directly being copied to the OUT folder. I want the files to be within the Templates folder and the templates folder being copied to the OUT directory. Is there a way of doing this. Any help is highly appreciated.

Thanks,
Vinodh

22 10 2008
Suresh Gopalakrishnan

I am using a resource file that contains messages (as strings). That’s giving me trouble while I do unit tests. The actual application runs fine and is able to fetch messages from the embedded resource. I followed the recommendations in this article. I’ve set the build action for this resource to “Embedded Resource”. I’ve also “Deployed” the resource as recommended above. I also tried using GetCallingAssembly() instead of GetExecutingAssembly().

I’m still having issues in running the Unit Tests and still getting the exception:

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure \”xxxxxxxx.UI.Messages.resources\” was correctly embedded or linked into assembly \”xxxxxxx Lab Partner\” at compile time, or that all the satellite assemblies required are loadable and fully signed.

Could someone help me out??? I’ve been struggling at least for 8 hours now!

12 01 2009
LazyLizzard

Have a look at http://www.bestbrains.dk/dansk.aspx/Artikler/Using_Files_In_Unit_Tests

Basically, you enable XML documentation for your project and embed your text resource as a comment for your test case.

so you would have something like
///
///<?xml version="1.0" encoding="utf-8" ?>
///
/// ....
///
///

…..your test function here….

Then the first line of your code extracts the embedded doc, trims it and htmldecodes it…
(or you could change the first line to ///<?xml version=...... then a trim won't be needed)

The provided code on the site explains it well...

30 06 2009
eheuristic

first off i want to thank you so… much you did very good job.

4 07 2009
eheuristic

Hi
when fist time i see this site i thing this site is not special.
just author site but then i read and see this really best site in all sites.
Thank you.

11 07 2009
Geoff

Very helpful, I have been trying to figure this out for a long time. I use the embedded xml trick in most cases, but occasionally code requires an xml file that you can’t embed. For example WSE 3.0 frequently needs an external policy configuration file for authentication. You can’t embed that resource, because you don’t have control over the code that is looking for the file, it is in the CLR. If the file isn’t there, it just barfs. I couldn’t figure out how to get the wse3policyCache.config file into the TestResults directory. Thanks!

19 02 2010
Tapan Das

It is a very good, informative, instructive, revealing,

4 05 2011
antivirusfree

for trouble while I do unit tests…antivirus?

27 10 2011
Silver Coast

Interesting article, thank you for writing it and sharing with us.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.

%d bloggers like this: