开发者

How Can I Use NUnit with ASP.NET Website (Not Web Project)?

I am new to unit testing and I wanted to give NUnit a try.

In an ASP.NET Web Project, I can create a new project in my web project solution for unit testing and add a reference to my original project and in NUnit I can load the dll file for my unit testing project to run the tests.

However, i am developing an ASP.NET Website and because an ASP.NET Web Site does not have a dll file, I cannot add a seperate project in my solution which references my website project, and therefore, I was not able to access classes in the main project to test. Even if I decided to leave my tests in my main web site project, I am not able开发者_Go百科 to directly load the dll for the web site in the NUnit Gui (because there isn't any dll file).

I also face a problem when i try to creat Unit Tests for my web site using Visual Studio, don't know if they're related.

Any help would be apreciated.


Why can't you switch to Web Application Project instead? Or, you can move your business logic to an external Class Library Project and then reference the latter in your Nunit Test Project.


Yes, it is possible. The trick is not to use the NUnit GUI Runner, but have a custom ASP.net test page. Here's a sample using Razor. The following goes into App_Code\MyRunner.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NUnit.Core;
using NUnit.Framework;
using NUnit.Core.Extensibility;

/// <summary>
/// Summary description for TestRunner
/// </summary>
public class MyRunner
{
    public static IList<TestResult> Run(Type testCase)
    {
        NUnit.Core.CoreExtensions.Host.InitializeService();
        TestExecutionContext.CurrentContext.TestPackage = new TestPackage(testCase.FullName);
        MyListener listener = new MyListener();
        if (TestFixtureBuilder.CanBuildFrom(testCase))
        {
            NUnit.Core.Test test = TestFixtureBuilder.BuildFrom(testCase);
            test.Run(listener, NUnit.Core.TestFilter.Empty);
        }
        return listener.Results;
    }
}

public class MyListener : EventListener
{

    public IList<TestResult> Results { get { return _results; } }

    public void RunFinished(Exception exception)
    {

    }

    public void RunFinished(TestResult result)
    {

    }

    public void RunStarted(string name, int testCount)
    {

    }

    public void SuiteFinished(TestResult result)
    {
    }

    public void SuiteStarted(TestName testName)
    {

    }

    IList<TestResult> _results = new List<TestResult>();
    public void TestFinished(TestResult result)
    {
        _results.Add(result);
    }

    public void TestOutput(TestOutput testOutput)
    {

    }

    public void TestStarted(TestName testName)
    {

    }

    public void UnhandledException(Exception exception)
    {

    }
}

public class Class1
{
    [Test]
    public void TestOnePlusOne()
    {
        Assert.AreEqual(1 + 1, 2);
    }

    [Test]
    public void TestOnePlusTwo()
    {
        throw new Exception("Ooops");
    }
}

And here's a CSHTML page to go with it. Name it as MyNUnit.cshtml:

@using NUnit.Core
@{
    IList<TestResult> results = MyRunner.Run(typeof(Class1));
}
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <table>
        @foreach (TestResult result in results)
        {
            <tr>
                <td>
                    @result.Name
                </td>
                <td>
                    @result.IsSuccess
                </td>
                <td>
                    @result.Message
                </td>
            </tr>
        }
    </table>
</body>
</html>


You can provide the reference to your website project by

add reference->projects-> add project.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜