【问题标题】:How Can I Use NUnit with ASP.NET Website (Not Web Project)?如何在 ASP.NET 网站(不是 Web 项目)中使用 NUnit?
【发布时间】:2012-12-12 01:04:03
【问题描述】:

我是单元测试的新手,我想试试 NUnit。

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

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

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

我们将不胜感激。

【问题讨论】:

    标签: asp.net web nunit webforms webproject


    【解决方案1】:

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

    【讨论】:

    • 这可能与网站的已发布版本有关。这样,您就有了一个可以在 Nunit 测试项目中链接的 DLL。但是,这是一个非常尴尬的解决方案。我实际上对网站项目有负面印象。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 您无法添加对网站的引用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 2010-09-25
      • 2011-02-03
      • 1970-01-01
      相关资源
      最近更新 更多