How can I use NUnit with an ASP.NET website (not a web project)?

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

In ASP.NET web project, I can create a new project in my solution for web projects for unit testing and add a link to my original project, and in NUnit I can upload a DLL file for my unit testing project to run the tests.

However, I am developing the ASP.NET website and because there is no dll file on the ASP.NET website, I cannot add a separate project in my solution that refers to my project on the website, and therefore I do not was able to access classes in the main project for testing. Even if I decided to leave my tests in my main website project, I cannot directly load the DLL for the website in NUnit Gui (because there is no DLL file).

I also had a problem when I try to create module tests for my website using Visual Studio, I don’t know if they are related.

Any help would be appreciated.

+3
source share
3 answers

-? - , Nunit.

+4

, . NUnit GUI Runner, ASP.net. Razor. 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");
    }
}

CSHTML, . 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>
+2

-

- > projects- > .

-1

Source: https://habr.com/ru/post/1735216/


All Articles