6

I'm trying to test the functionality of a class in my (ASP.Net) web application, using unit tests. This class loads some files from the hard drive (to perform xsl transformations):

Xsl = GetXSLFromFile(AppDomain.CurrentDomain.BaseDirectory + "\XML Transformationen\Transformation_01.xslt")

This path is correctly resolved during debugging of the web application itself. But whenever I start the unit test (which resides in a separate testing project, referencing the project of the web application), I get the path of the testing project instead.

Is it possible to get the path of the web application in this scenario, or do I have to use a different approach? Any hints are appreciated.

Kai Hartmann
  • 3,106
  • 1
  • 31
  • 45

3 Answers3

4

I suggest you do something like this:

public class MyXslFileLoader
{
    public void Load()
    {
        Load(AppDomain.CurrentDomain.BaseDirectory + "\XML Transformationen\Transformation_01.xslt");
    }

    public void Load(string path)
    {
        Xsl = GetXSLFromFile(path);
    }
}

You would call Load() in your web application, but use the overloaded version of this method in your unittest application. You could consider adding the xslt file as a resource to your project.

You would be able to load the path like this:

var webApplicationDllPath = Path.GetDirectoryName(typeof(ClassInTheWebApplicationDll).Assembly.GetName().CodeBase);
Jordy Langen
  • 3,591
  • 4
  • 23
  • 32
  • Yes, this might be viable. The only drawback is, that I have to use the path parameter in every parent method. Perhaps I have to reconsider my class instantiation, and provide the path throught the constructor. – Kai Hartmann Jun 26 '13 at 07:29
  • How is that a drawback? You could make base a class that contains the method overloads and implement them there. Besides, the `Path.GetDirectoryName` example would suffice, theoretically – Jordy Langen Jun 26 '13 at 07:32
  • The Path.GetDirectoryName example returns the path of the testing project as well. What I mean with drawback is, that every method in the callstack from the call in the testing project down to the method loading the file in the webapplication would have to get that parameter. Something like class.LoadXsl(path) -> class.LoadXsl2(path) -> class.Load(path) . – Kai Hartmann Jun 26 '13 at 07:48
  • The class is located in the webapplication project. But because the testing project references this project, the dll of the webapplication is copied to the debug folder of the testing project. I assume, that getting the path of the webapplication during runtime is probably impossible in this scenario. – Kai Hartmann Jun 26 '13 at 07:57
2
string path;
path = System.IO.Path.GetDirectoryName( 
  System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );

HOW TO: Determine the Executing Application's Path

Getting the path of a executable file in C#

Hope this is helpful.. :)

Paritosh
  • 11,144
  • 5
  • 56
  • 74
  • 5
    The executing assembly would be the unittest project. You would get the wrong path. – Jordy Langen Jun 26 '13 at 07:29
  • 1
    Hi. It is helpful in general, because it gives the path of the actual executing assembly. :) But in my case it also returns a subfolder of the testing project. What I need is the path the class is located in or something. – Kai Hartmann Jun 26 '13 at 07:34
0

Ok, this gave the hint: Can a unit test project load the target application's app.config file?

In the .testsettings file, added by the testing project to my webapplication project, I can add files and folders which should be copied to the testing projects debug folder each time the test is executed.

So after this I can reference the xsl-files per AppDomain.CurrentDomain.BaseDirectory.

Additionally, to keep also the folder structure, I had to do what has been described here: Visual Studio Test Project - Does not copy folder on deployment

I had to edit the .testsettings file with a text editor, and add the outputDirectory parameter. After that I restarted Visual Studio, and when started the testing project the folder and files have been copied correctly.

Community
  • 1
  • 1
Kai Hartmann
  • 3,106
  • 1
  • 31
  • 45