3

I am using MS Unit Tests in VS2010.

I created a class that reads a file..just a static word list. The file was added to the main project and the test project as content with "Copy always" selected as the build event.

When I run the unit test, however, it looks for the file in the output directory of the unit test. How do you make it look in the bin directory. I don't want to mess with absolute filepaths just for testing...

Test method RequiredServicesTest.RequiredServices_JamesTest.Number2WordsTest threw exception:



Test method RequiredServicesTest.RequiredServices_JamesTest.Number2WordsTest threw exception: 
System.IO.FileNotFoundException: Could not find file E:\scomA3proj\TestResults\James_JAMES-PC 2012-02-24 21_22_17\Out\randomdictionary.txt.

Update: One thing I forgot to mention is that this is a WCF Service Library project.

James Cotter
  • 798
  • 11
  • 22
  • possible duplicate of: http://stackoverflow.com/questions/1541096/visual-studio-unit-tests-loading-resources-in-the-project – javamonkey79 Mar 02 '12 at 02:54

4 Answers4

4

Please consider the following trick to get the right path:

var assembly = typeof(RequiredServices_JamesTest).Assembly;
var assemblyPath = Path.GetDirectoryName(new Uri(assembly.EscapedCodeBase).LocalPath);

var filePath = Path.Combine(assemblyPath, "randomdictionary.txt");
// You have the correct file path now!
ogggre
  • 2,204
  • 1
  • 23
  • 19
  • For one, the project should not need to know about the test project. – James Cotter Feb 25 '12 at 05:57
  • I see. You can try to set the current directory from your test method to assemblyPath. Then, the rest of your production code will stay intact. – ogggre Feb 25 '12 at 06:00
2

You can make the word list file a deployment item, then it gets automatically copied into your test output directory.

[TestClass()]
[DeploymentItem("randomdictionary.txt")]
public class SomeTestClass
{
   //..
}
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • Should work fine, make sure file is in the project root directory otherwise it won't be found. Also see http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.deploymentitemattribute%28v=vs.100%29.aspx – BrokenGlass Feb 25 '12 at 04:59
  • I added that to the class as well as the individual method. I added the randomdictionary.txt file directly to the test project too. [TestMethod()] [DeploymentItem("randomdictionary.txt")] public void Number2WordsTest() { – James Cotter Feb 25 '12 at 05:48
  • It gets copied to the test projects "bin" folder but it tries to find the file in the test output directory.. – James Cotter Feb 25 '12 at 05:52
  • I forgot to mention this is a "WCF Service project" that I am testing...maybe it does not work for that... – James Cotter Feb 25 '12 at 05:54
1

I try to store my test files as an embedded resource in the test class and extract them to a temporary directory (with auto clean up / delete functions) so I never have to worry about where the files live. It seems to be easier and more stable this way.

tsells
  • 2,751
  • 1
  • 18
  • 20
0

I resolve using this article https://msdn.microsoft.com/en-us/library/ms182475.aspx

One example to keep it simple

  1. inside the root dir of UnitTestProject1 I have test data in dir TestData (it contains at this stage of development all directory and file useful for test). Inside TestData i have a dir TestCollisioni

    Direcotry path: UnitTestProject1|->TestData|->TestCollisioni

  2. My command onPostBuild event (this is documented on link above) for xcopy test files is (you need to modify only UnitTstProject1 and TestData value if needed (i.e. only if you use a different test Project Name or a different TestData dirname)).

    xcopy /Y /S "$(SolutionDir)UnitTestProject1\TestData*" "$(TargetDir)TestData\"

    Using $(SolutionDir) and $(TargetDir) seems best way to autodeploy test file, don't change it

  3. Test Method have this value for DeploymentItem

    [TestMethod]
    
    [DeploymentItem("TestData")]
    
  4. Inside test you can access file inside test data (and whatever inside that dir) as , for example:

    Directory.Exists(@"TestCollisioni\"),

So my working test test is (NO "\" Before TestCollisioni) :

    [TestMethod]
    [DeploymentItem("**TestData**")]
    public void TestMethod1()
    {
        Assert.IsTrue(Directory.Exists(@"TestCollisioni\"));
    }
Antimo
  • 460
  • 1
  • 8
  • 19