2010-03-03 62 views
3

我是单元测试新手,我想给NUnit一个尝试。如何在ASP.NET网站(不是Web项目)上使用NUnit?

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

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

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

任何帮助将是apreciated。

回答

4

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

+0

因此,单元测试ASP.NET网站是不可能的? – bahith 2010-03-03 11:48:35

+0

这可能与该网站的已发布版本有关。这样,你就可以在Nunit测试项目中链接到一个DLL。然而,这是一个相当尴尬的解决方案。我实际上对网站项目有负面印象。 – Kerido 2010-03-03 12:00:40

+0

非常感谢。 – bahith 2010-03-03 12:08:09

-1

您可以通过

提供参考你的网站项目中添加参考 - >项目 - >添加项目。

+0

您无法添加对网站的引用 – Steve 2013-02-06 22:15:17

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> 
相关问题