System.Web.Hosting.HostingEnvironment.MapPath returns null in unit test

Possibly save 1 hour of your time: There are times when you need to access a file that is included within your application. This could be a data file. In this case, you may need to grab the path of this file on your server.

You can use System.Web.Hosting.HostingEnvironment.MapPath(“~/yourPath”) but this returns null in your functional unit tests.

An easy way to fix this is to check if it is running in a hosted environment. If it is not, then use AppDomain.CurrentDomain.BaseDirectory.

private static string MapPath(string filePath)
var hostingRoot = System.Web.Hosting.HostingEnvironment.IsHosted
? System.Web.Hosting.HostingEnvironment.MapPath("~/")
: AppDomain.CurrentDomain.BaseDirectory;

return Path.Combine(hostingRoot, filePath.Substring(1).Replace('/', '\\'));

NUnit – test performance time on your method

Possibly save 1 hour of your time: There will be times you want to write some integration unit tests that hit the database to test performance.

One easy way to impose the maximum time in milliseconds on the unit test is using the MaxTime property available on NUnit and can be done as follows:

 [Test, MaxTime(2000)]
public void YourMethod()

Tip: You might also want to do an initial call to your database in OneTimeSetup to simulate first request will take longer time.

public void OneTimeSetUp()

// do a call to database to simulate that first request will take longer time



Resharper nunit unit test – Could not find a part of the path

Possibly save 1 hour of your time: There are times when your unit test depends on input file. To access the file when using nunit 3.8.1, you can use TestContext.CurrentContext.TestDirectory.

public void LoadFileInUnitTestProject()
var doc = new XmlDocument();

doc.Load(TestContext.CurrentContext.TestDirectory + "\\Services\\CountryProfile\\population.query.xml");

Assert.That(doc, Is.Not.Null);


Book ReSharper Essentials

Make methods accessible to unit tests

Possibly save 1 hour of your time: If you would like to create unit tests for your library, you can create the unit tests in a different project to separate the tests and the code.

1. Typically, a good convention is use the same project name and append dot Tests. i.e. MyProjectName.Tests

2. Then you will need to add reference to your project.

3. To access the methods in your project from the tests project, you will need to mark them internal access unless it is already marked as public access.

4. In the AssemblyInfo.cs of your project, you need to add the following the make your project visible to your tests project.

   [assembly: InternalsVisibleTo(“YourProjectName.Tests”)]

There are other methods of testing your protected or private methods, but I find this solution the easiest to configure and maintain.

Mock HttpRequest

Possibly save 2 hours of your time:

We often want to unit test our controller layer with the input request URL. Here is an example of unit test that can mock the HttpRequest within the HttpContext constructor that you can set on your controller.

private HttpContextBase MockCurrentContext()
var sb = new StringBuilder();
TextWriter w = new StringWriter(sb);
var context = new HttpContext(new HttpRequest(“”, “ “, “”), new HttpResponse(w));
return new HttpContextWrapper(context);

public void OneTimeSetUp()
controller = new YourController();
controller.ControllerContext = new ControllerContext(MockCurrentContext(), new RouteData(), controller);