I have a unit test that looks at a text file included in my test project. I had to take this route, as I can’t seem to properly duplicate the special characters in a C# string that the users are getting into the raw data itself (some other system).
The unit test reads the content of the file during the arrange portion, then acts on the content to ensure things are working as planned.
Locally, the test runs just fine–zero issues.
We run TFS2010 Build, and my builds fail that test every time, as the text file itself isn’t found when the test is run on the build server.
My file is presently in a directory on the Test project itself called “Assets”, the text file’s properties are:
- Build Action: Content
- Copy To Output Directory: Copy Always
The Test Code is
[TestMethod]
public void Broken_First_Token_Ok_Second_Returns_Full_String()
{
string rawText = string.Empty;
// load the broken translation
using (StreamReader reader = new StreamReader(@"Assets\BrokenTranslation.txt")){
rawText = reader.ReadToEnd();
}
string expected = rawText;
string actual = [Some code that should return the proper values]
Assert.AreEqual(expected, actual, "Failed to return proper match");
}
What have I missed?
You can reliably make the text file available to your test via the DeploymentItem attribute. This blog post describes how and why to use it: http://luisfsgoncalves.wordpress.com/2011/05/31/unit-tests-with-dependencies-on-team-foundation-server/