如何将 NUnit 与 ASP.NET 网站(不是 Web 项目)一起使用?

发布于 2024-08-23 05:49:49 字数 379 浏览 3 评论 0原文

我是单元测试新手,我想尝试一下 NUnit。

在 ASP.NET Web 项目中,我可以在 Web 项目解决方案中创建一个新项目以进行单元测试,并添加对原始项目的引用,在 NUnit 中,我可以加载单元测试项目的 dll 文件来运行测试。

但是,我正在开发一个 ASP.NET 网站,并且由于 ASP.NET 网站没有 dll 文件,我无法在解决方案中添加引用我的网站项目的单独项目,因此,我无法访问类在主项目中进行测试。即使我决定将测试留在我的主网站项目中,我也无法直接在 NUnit Gui 中加载该网站的 dll(因为没有任何 dll 文件)。

当我尝试使用 Visual Studio 为我的网站创建单元测试时,我也遇到一个问题,不知道它们是否相关。

任何帮助将不胜感激。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉城 2024-08-30 05:49:49

为什么不能切换到 Web 应用程序项目?
或者,您可以将业务逻辑移至外部类库项目,然后在 Nunit 测试项目中引用后者。

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.

无妨# 2024-08-30 05:49:49

是的,这是可能的。诀窍是不使用 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>

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>
堇色安年 2024-08-30 05:49:49

您可以通过以下方式提供您的网站项目的参考

添加参考->项目->添加项目。

You can provide the reference to your website project by

add reference->projects-> add project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文