Glenn Rodriguez Glenn Rodriguez - 3 months ago 21
ASP.NET (C#) Question

NUnit Engine in ASP.Net

Good Afternoon,

For my current project I'm trying to create a NUnit Runner that will be hosted in our Development Server.
For a quick version, I'm using ASP.NET Webforms + latest version of NUnit from NuGet (currently 3.4.1, both Engine + Core). I'm following the suggestions from this post on how to run a test from code.

When loading my application I'm getting the following exception:


Could not load file or assembly 'nunit.framework' or one of its dependencies. The system cannot find the file specified.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'nunit.framework' or one of its dependencies. The system cannot find the file specified.


Source Error:
Line 33: {
Line 34: // execute the tests
Line 35: XmlNode result = runner.Run(null, emptyFilter);
Line 36: lblTest.Text = result.InnerText;
Line 37: }


My NUnit library is located in the Binary folder from the Web Application, so I don't know why is unable to locate 'nunit.framework' from there.

This is the actual code that I'm using for the runner:

using NUnit.Engine;
using NUnit.Framework;
using System;
using System.Reflection;
using System.Xml;

namespace NunitWebRunner
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// set up the options
string path = Assembly.GetExecutingAssembly().Location;
TestPackage package = new TestPackage(path);
package.AddSetting("WorkDirectory", Environment.CurrentDirectory);

// prepare the engine
ITestEngine engine = TestEngineActivator.CreateInstance();
var _filterService = engine.Services.GetService<ITestFilterService>();
ITestFilterBuilder builder = _filterService.GetTestFilterBuilder();
TestFilter emptyFilter = builder.GetFilter();

using (ITestRunner runner = engine.GetRunner(package))
{
// execute the tests
XmlNode result = runner.Run(null, emptyFilter);
lblTest.Text = result.InnerText;
}
}
}

[TestFixture]
public class TemplateTestClass
{
[Test]
public void TestThatPasses()
{
Assert.IsTrue(true);
}

[Test]
public void TestThatFails()
{
Assert.AreEqual(4, 5);
}

[Test]
[Ignore("Ignored")]
public void IgnoredeTest()
{
Assert.IsTrue(false);
}
}
}


Any idea of what is happening, or how I can try to fix it to make it work?

Answer

After discussion in Nunit-Dicussion Group the solution to this problem was really easy.

Charlie commented above, the issue is with the current configurations that are used in the code. instead of using "WorkingDirectory", "ProcessModel" and "DomainUsage" should be setted so NUnit runner can be executed without problems in a Web environment like ASP.NET.

Below you can find the code that mark this thread as aswered.

using NUnit.Engine;
using NUnit.Framework;
using System;
using System.Reflection;
using System.Xml;

namespace NunitWebRunner
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // set up the options
            string path = Assembly.GetExecutingAssembly().Location;
            TestPackage package = new TestPackage(path);
            package.AddSetting("ProcessModel", "Single");
            package.AddSetting("DomainUsage", "None");

            // prepare the engine
            ITestEngine engine = TestEngineActivator.CreateInstance();
            var _filterService = engine.Services.GetService<ITestFilterService>();
            ITestFilterBuilder builder = _filterService.GetTestFilterBuilder();
            TestFilter emptyFilter = builder.GetFilter();

            using (ITestRunner runner = engine.GetRunner(package))
            {
                // execute the tests            
                XmlNode result = runner.Run(null, emptyFilter);
                lblTest.Text = result.InnerText;
            }
        }
    }

    [TestFixture]
    public class TemplateTestClass
    {
        [Test]
        public void TestThatPasses()
        {
            Assert.IsTrue(true);
        }

        [Test]
        public void TestThatFails()
        {
            Assert.AreEqual(4, 5);
        }

        [Test]
        [Ignore("Ignored")]
        public void IgnoredeTest()
        {
            Assert.IsTrue(false);
        }
    }
}
Comments