10

I have a test project in which I needs to load an XLSX file. For that, I added a file with copy-always so that it ends up in the build directory, but all of the following return a wrong path:

  1. System.Reflection.Assembly.GetAssembly(typeof(testclass)).Location;
  2. AppDomain.CurrentDomain.BaseDirectory
  3. Directory.GetCurrentDirectory();

They all give me:

"C:\\Users\\username\\Documents\\visual-studio-projecten\\projectname\\TestResults\\username_ICT003 2012-06-20 12_07_06\\Out"

and I need

"C:\\Users\\username\\Documents\\visual-studio-projecten\\projectname\\TestProject\\bin\\Debug\\SupportFiles\\"

How do I accomplish that?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Halfgaar
  • 732
  • 2
  • 7
  • 32

4 Answers4

9

Use the DeploymentItemAttribute attribute. To quote MSDN:

This attribute identifies files and directories that contain files that are used by the deployed test to run. The test engine makes a copy of the deployment items and places them in test deployment directory based upon the OutputDirectory specified or the default directory.

For example:

[TestClass]
public class MyUnitTest
{
    [TestMethod()]
    [DeploymentItem("myfile.txt")]
    public void MyTestMethod()
    {          
        string file = "myfile.txt";           
        Assert.IsTrue(File.Exists(file), "deployment failed: " + file +
            " did not get deployed");
    }
}

Of course assuming you are using MSTest as your testing framework.

Christophe Geers
  • 8,564
  • 3
  • 37
  • 53
  • It works, but I have to do really funky things to get files in subdirs to be deployed. Specifying the file like `SupportFiles\dir\bla.xls` doesn't work, but if I say `SupportFiles\dir` is a deployment item, the xls file in that dir gets deployed in the root dir of the output. And I have to set `copy always` on it. – Halfgaar Jun 21 '12 at 07:52
  • [DeploymentItem] takes an optional second arg to specify the destination path – Malcolm Oct 13 '21 at 21:22
8

Try this:

string dir = Path.Combine(
    Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
    "SupportFiles");

Don't use Directory.GetCurrentDirectory() because current directory could not be your exe dir and may change during program execution.

Marco
  • 56,740
  • 14
  • 129
  • 152
  • 2
    +1 for `Don't use Directory.GetCurrentDirectory() because current directory could not be your exe dir and may change during program execution.` – dtsg Jun 20 '12 at 10:22
  • Didn't help, still the testresults dir. I had actually already tried that one, but forgot. I tried it again to be sure. – Halfgaar Jun 20 '12 at 10:24
  • @Halfgaar: which is the build directory? I'm pretty sure it should work, I always use it!!! – Marco Jun 20 '12 at 10:28
  • It's the needed path I mentioned. The XLS file is also in that location, as well as the libs and executables. – Halfgaar Jun 20 '12 at 10:31
  • @Halfgaar: if you run your main exe from the dir you need and show a messagebox with my `dir` value, what do you get? – Marco Jun 20 '12 at 13:37
  • This will still return the location of the executable in most cases, as dependent assemblies are copied to the "main" bin directory, and executed from there. – BartoszKP Dec 13 '16 at 12:08
1

In case someone like me comes along, if you're using a .testsettings file and the DeploymentItem attribute on the class doesn't work even though you've set your files as Content and to Always Copy, it's because either you already have a Deployment section in your .testsettings file or you need to use DeploymentItem in the .testsettings file. Here's how ours looks now:

<Deployment>
    <DeploymentItem filename="Tests\Unit Tests\ConnectionStrings.config" />
    <DeploymentItem filename="Tests\Unit Tests\<project dir>\bin\Debug\TestFiles\" outputDirectory="TestFiles" />
</Deployment>

One does a file, the other does 13 files in a directory. Note the trailing slash for the directory's "filename"!

Solution and more info was found at:

MsTest DeploymentItem OutputDirectory in testsettings

Community
  • 1
  • 1
jgerman
  • 1,143
  • 8
  • 14
1

Switch to xUnit or NUnit.

Then both option 2., 3. and Environment.CurrentDirectory works as needed, returning the build-output-diretory. Remove "bin\Debug" or "bin\Release" and files can be left at 'Do not copy'.

Also see the GetFileFromMethod-method and usage in ElasticSearch for a nice way to have one file per test.

Grastveit
  • 15,770
  • 3
  • 27
  • 36