Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get build directory of a test project?

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?

like image 205
Halfgaar Avatar asked Jun 20 '12 10:06

Halfgaar


2 Answers

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.

like image 54
Christophe Geers Avatar answered Oct 21 '22 18:10

Christophe Geers


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.

like image 7
Marco Avatar answered Oct 21 '22 19:10

Marco