Daily Usage Issues

test data file not found

Started by CosminNet on 10,442 views

using vs2010 sp1 on win7 pro x64 with resharper 7.1
i have a unit test project, in it a directory named "TestData" and in the dir a file ''ShipmentDocs.txt" that i intend to use to read in some test. the file is set with build action: none and copy if newer in the vs
project.
in ncrunch settings i set Additional files to include to the TestData dir so all files in the dir are included.
in the begining of a test i attempt to read the file:
var rows = File.ReadAllLines(@"TestData\ShipmentDocs.txt").

ncrunch test fails with

System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\Cosmin.EDI-SOFT\AppData\Local\NCrunch\220\62\TestResults\75dd90c8-7ebe-4594-9ace-aa5cb9de06be\Out\TestData\ShipmentDocs.txt'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
at System.IO.StreamReader..ctor(String path, Encoding encoding)
at System.IO.File.InternalReadAllLines(String path, Encoding encoding)
at System.IO.File.ReadAllLines(String path)
at DALTest.PortalDALTestSuiteSQL.TestUpdate() in D:\!Trunk\Portal2\TestSuites\PortalDALUnitTest\PortalDALTestSuiteSQL.cs:line 164#0

the path [C:\Users\Cosmin.EDI-SOFT\AppData\Local\NCrunch\220\62\TestResults\75dd90c8-7ebe-4594-9ace-aa5cb9de06be\Out\TestData\ShipmentDocs.txt] does not exist. the path is valid until [C:\Users\Cosmin.EDI-SOFT\AppData\Local\NCrunch\220].

when searching using total commander for occurences on the disk i find the file here
c:\Users\Cosmin.EDI-SOFT\AppData\Local\NCrunch\3684\23\TestSuites\PortalDALUnitTest\bin\Debug\TestData\
and here
c:\Users\Cosmin.EDI-SOFT\AppData\Local\NCrunch\3684\23\TestSuites\PortalDALUnitTest\TestData\

it seems that the copying is done in the ncrunch folder but when the test runs the current folder is a nonexistant one.

what should i do so that a file in the test projects (not the tested proj) is available for reading by the unit tests?

Edited

Hi, thanks for posting!

NCrunch emulates the behaviour of MSTest's sandboxing, so it will execute tests within a shadow copied directory that is positioned relative to the project file in the NCrunch workspace. So basically it creates a shadow copy of a shadow copy (confusing I know).

You have a few options here:
1. Reference the ShipmentDocs.txt using the relative path: ..\..\..\TestData\Shipmentdocs.txt
2. Add a [DeploymentItem(@"TestData\ShipmentDocs.txt")] attribute to the test that uses the resource file. This will tell the MSTest runner to deploy the file into the MSTest sandbox
3. Use a different test framework that isn't MSTest

I hope this helps!


Cheers,

Remco
Remco wrote:Hi, thanks for posting!

NCrunch emulates the behaviour of MSTest's sandboxing, so it will execute tests within a shadow copied directory that is positioned relative to the project file in the NCrunch workspace. So basically it creates a shadow copy of a shadow copy (confusing I know).

You have a few options here:
1. Reference the ShipmentDocs.txt using the relative path: ..\..\..\TestData\Shipmentdocs.txt
2. Add a [DeploymentItem(@"TestData\ShipmentDocs.txt")] attribute to the test that uses the resource file. This will tell the MSTest runner to deploy the file into the MSTest sandbox
3. Use a different test framework that isn't MSTest

I hope this helps!


Cheers,

Remco


hi

i've added the attribute (DeploymentItem) both on the test class and on the method and it does not seem to work. i know that this path reffers to the project being tested. my file is within the unit test proj, not within the tested proj. any guidance?
Sorry - I made the mistake of thinking that this resource file originated from the test project itself, rather than a project referenced by it. This makes the solution a bit more limited and more complicated.

The resource file will always be located relative to the assembly built from the project containing it. Because NCrunch arranges projects in isolation, it isn't possible to introduce relative path references between your projects to resolve resource files. Instead, you need to rely on reflection. Have a look at the following example code that could be placed inside your test project:

var resourceFileAssemblyLocation = typeof(TypeWithinProjectContainingResource).Assembly.Location;
var directoryOfResourceAssembly = Path.GetDirectoryName(resourceFileAssemblyLocation);
var pathToResourceFile = Path.Combine(directoryOfResourceAssembly, @"TestData\Shipmentdocs.txt");
Assert.IsTrue(File.Exists(pathToResourceFile)); // Should pass assertion

It's also safe to resolve resource locations like this in your production code, and it's actually better practice than relying on Directory.GetCurrentDirectory(), as in this way your application behaviour is not determined by the current directory that is defined when the O/S loads it.

I'll make a note to have a closer look at how resource files are handled in these kind of situations to see if I can come up with a cleaner, more intuitive approach that doesn't require code alterations. Meanwhile, I hope this will solve your immediate problem.


Cheers,

Remco

Post a reply

Log in to reply.