Reputation: 13
I'm having quite a bit of trouble in resolving an issue dealing with referencing a custom text file in my project. NOTE: the file contains text, but its of a custom type '.rit'
I'm currently building and running some test cases for a new application and one of them involves reading a file that resides in a sample folder of my project called 'TestFiles'. At first, I ran the test locally using the absolute path of the file and it ran with no issues. However, I am using TFS to check in my code and once the test runs in the server it fails immediately because it can't locate the file, which was obvious because I was using an absolute path to a file in my local drive, so I NEED to change how the test accesses this file to make it work both locally and on the server.
I've looked at a look of examples as I was looking for help but none seem to work... I've tried:
DeploymentItem:
[DeploymentItem(@"TestProject\TestFiles\test file.rit", "TestData")]
[TestMethod]
public void LoadFileTest()
{
//Assert.IsTrue(File.Exists("test file.rit"));
var obj = new obj();
var tstFile = @"TestData\\test file.rit";
var file = new StreamReader(tstFile);
obj.OpenRitFileInTextFormat(file);
}
The hierarchy of the file is: Solution\TestProject\TestFiles\test file.rit and I made sure to set the Copy to Output to "copy always"
Adding it as a Resource:
//[DeploymentItem(@"TestProject\\TestFiles\\test file.rit", "TestData")]
[TestMethod]
public void LoadFileTest()
{
//Assert.IsTrue(File.Exists("test file.rit"));
var obj = new obj();
var tstFile = Properties.Resources.test_file;
//this one throws an error however because it says the type of tstFile is now
//a byte[] instead of a stream reader
*var file = new StreamReader(tstFile);*
obj.OpenRitFileInTextFormat(file);
}
I'm not very experienced in this one, so I might be doing something wrong... If anyone could please enlighten me or give me some guidance as to where else I could look for help I would GREATLY appreciate it!
Thanks,
EDIT: I have found out my TestData folder I'm specifying to be copied in the DeploymentItem path is not being created/copied to the Out folder of the test run. I'm starting to think this is more of a settings file problem, here's the contents of my settings file:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- Configurations that affect the Test Framework -->
<RunConfiguration>
<!-- Path relative to solution directory -->
<ResultsDirectory>.\TestResults</ResultsDirectory>
<!-- [x86] | x64
- You can also change it from menu Test, Test Settings, Default Processor Architecture -->
<TargetPlatform>x86</TargetPlatform>
<!-- Framework35 | [Framework40] | Framework45 -->
<TargetFrameworkVersion>Framework40</TargetFrameworkVersion>
</RunConfiguration>
<!-- Configurations for data collectors -->
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Configuration>
<CodeCoverage>
<ModulePaths>
<Exclude>
<ModulePath>.*CPPUnitTestFramework.*</ModulePath>
</Exclude>
</ModulePaths>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
<!-- Adapter Specific sections -->
<!-- MSTest adapter -->
<MSTest>
<MapInconclusiveToFailed>True</MapInconclusiveToFailed>
<CaptureTraceOutput>false</CaptureTraceOutput>
<DeleteDeploymentDirectoryAfterTestRunIsComplete>False</DeleteDeploymentDirectoryAfterTestRunIsComplete>
<DeploymentEnabled>True</DeploymentEnabled>
</MSTest>
</RunSettings>
Upvotes: 1
Views: 2437
Reputation: 35891
When using the @
modifier you shouldn't escape the backslashes. So use either [DeploymentItem("TestProject\\TestFiles\\test file.rit", "TestData")]
or [DeploymentItem(@"TestProject\TestFiles\test file.rit", "TestData")]
. The second version is preferable.
From my experience, if a file is added to the project at its root level, then it can be referenced just by [DeploymentItem("file.xyz")]
in the test case. So in your case [DeploymentItem(@"TestFiles\test file.rit", "TestData")]
should be fine.
You also need to remember to "Enable deployment" in "Solution items" -> Local.testsettings:
Upvotes: 2