Please excuse the ambiguous title; it's difficult to describe in a single line.
Basically we have MbUnit tests which run fine using TestDriven from within Visual Studio, but fails when attempting to run the tests via the <gallio> task from within NAnt.
The failure are to do with the tests which attempt to read files; they read files relative to the current directory, for example "..\..\files\dir\whatever". However the problem seems to be that Gallio copies the test DLLs to a directory elsewhere, and sets the current directory to be "%HOMEDIR%\AppData\Local\Temp\Gallio\MSTestAdapter\randomname\TestDir\Out\something".
So my question is two-fold: Where should I be putting files which are required by tests so they can be found at runtime, and how should I be referring to them from code?
(Also, I didn't think we were using MS-Test at all, so how come there's an 'MSTest' directory in there?)
Although we use NUnit instead of MbUnit I think there is some general advice I can give regarding handling files in unit tests.
Never rely on paths - neither absolute nor relative. Keep control over paths inside your tests. This is what we do:
Resources
to your test project (so you have everything in one place)MyFile.txt
)Add
> Existing Item...
(so your files are kept with your sources. They get deployed afterwards as part of the assembly of your test project)Resources
in the project's properties, Add Resource
> Add Existing File...
)Path.GetTempFileName()
since you have a unique path then and it's most likely you have sufficient access rights on any machine)Here is a sample:
[TestFixture]
public class MyFixture
{
private static readonly string MyFilePath = Path.GetTempFileName();
[SetUp]
public void SetUp()
{
// use File.WriteAllBytes for binary files
File.WriteAllText(MyFilePath, Properties.Resources.MyFile);
}
[Test]
public void TestSomething()
{
Assert.That(ObjectUnderTest.UseFile(MyFilePath), Is.True);
}
[TearDown]
public void TearDown()
{
File.Delete(MyFilePath);
}
}