Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

test data file not found
CosminNet
#1 Posted : Wednesday, November 28, 2012 2:02:05 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 11/28/2012(UTC)
Posts: 2
Location: Romania

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:
Quote:
var rows = File.ReadAllLines(@"TestData\ShipmentDocs.txt").

ncrunch test fails with
Quote:

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?
Remco
#2 Posted : Wednesday, November 28, 2012 9:12:12 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,123

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
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
CosminNet
#3 Posted : Thursday, November 29, 2012 7:02:43 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 11/28/2012(UTC)
Posts: 2
Location: Romania

Remco;3289 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?
Remco
#4 Posted : Thursday, November 29, 2012 10:11:49 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,123

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
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

1 user thanked Remco for this useful post.
yanglee on 5/1/2013(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.058 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download