且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

你如何单元测试的ASP.NET核心控制器或模型对象?

更新时间:2023-02-17 07:40:59

更​​新:的xUnit仍然是一个伟大的想法,但这个答案现在已经过时了,因为你可以此外,如果你想使用ASP.NET核心使用MSTEST。 (2016年6月1日)

UPDATE: XUnit is still a great idea, but this answer is outdated now because you can also use MSTEST if you want with ASP.NET core. (June 1, 2016)

最近玩过的xUnit说明LINK:优秀的指令可能会更加频繁更新比这个答案在维基的xUnit被发现。

MOST RECENT XUNIT INSTRUCTIONS LINK: Excellent instructions that may be updated more often than this answer are found at the xUnit wiki.

IDE解决方法:手动查找并删除%TEMP%\\ VisualStudioTestExplorerExtensions时,Visual Studio中去笨,不会检测,并显示您的测试

IDE WORKAROUND: manually find and delete %TEMP%\VisualStudioTestExplorerExtensions when Visual Studio goes stupid and won't "detect" and show you your tests.

截至2016年5月,随着最近RC2所取代ASP.NET 1.0的核心RC1,它仍然没有出现可能使用与ASP.NET核心(原ASP.NET 5)微软的标准单元测试框架,以及出现的xUnit成为RC1和RC2一个不错的选择。

As of May 2016, with ASP.NET Core 1.0 RC1 recently superceded by RC2, it still does not appear possible to use the standard Microsoft Unit Test framework with ASP.NET Core (Formerly ASP.NET 5), and XUnit appears to be a good choice for RC1 and RC2.

您可以得到XUnit.net单元测试与ASP.NET核心1.0.0-RC1工作,使用官方的说明]的 2在具有特定的的xUnit GitHub的项目入门的情况。

You can get XUnit.net unit testing to work with ASP.NET Core 1.0.0-RC1, using the official instructions]2 at the XUnit github project which has a specific ".net core getting started" case.

您还可以安装的xUnit新项目模板,提供定期完整的.NET和.NET核心模板化的单元测试项目。单击工具,然后扩展和更新型的xUnit,并找到的xUnit测试项目模板安装模板。不要安装的xUnit测试运行器,你不需要它。

You can also install the XUnit New Project Template that provides a templated unit test project for regular full .net and .net core. Click Tools and then Extensions and Updates type in XUnit, and find xUnit Test Project TEMPLATE and install the TEMPLATE. DO NOT INSTALL ANY xUNIT TEST RUNNER, YOU DO NOT NEED IT.

我创建了一个工作示例,并上传到到位桶:

I have created a working sample and uploaded it to bitbucket:

https://bitbucket.org/wpostma/aspnet5mvc6xunitdemo

如果你没有做善变,你可以从到位桶下载一个ZIP。

If you don't have mercurial, you can download a zip from bitbucket.

该演示包括一个测试通过,一个测试失败。

The demo includes one test that passes, and one test that fails.

快速摘​​要:


  1. 您的Visual Studio 2015年,包括UPDATE2和1.0.0 preVIEW工具(如最新的五月2016年)。

  1. You have Visual Studio 2015 including Update2 and the "1.0.0 preview" tools (latest as of May 2016).

创建Web类库不是一个单元测试项目。

Create a Web Class Library NOT a Unit Test Project.

添加的xUnit引用它,并修复project.json(下面的例子)。

Add XUnit references to it, and fix your project.json (example below).

编写类(下面的例子)。

Write your class (example below).

里面的IDE,或外部的IDE,键入测试资源管理器运行测试在 DNX。测试并检查输出(下面的例子)。

Run tests with Test Explorer inside ide, or outside ide, type in dnx . tests and examine output (example below).

project.json为1.0.0-RC2参照演示组件和的xUnit:

project.json for 1.0.0-rc2 with reference to a demo assembly and xunit:

 {
  "version": "1.0.0-*",

  "testRunner": "xunit",

  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0-rc2-3002702",
      "type": "platform"
    },

    "dotnet-test-xunit": "1.0.0-rc2-*",

    "xunit": "2.1.0",


    "YetAnotherWebbyDemo": "1.0.0-*"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  }
}

单元测试类(whatever.cs):

Unit test class (whatever.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Xunit;

using YetAnotherWebbyDemo.Models;

namespace YetAnotherWebbyDemoTests
{
    // This project can output the Class library as a NuGet Package.
    // To enable this option, right-click on the project and select the Properties menu item. In the Build tab select "Produce outputs on build".
    public class TestBasics
    {
        [Fact]
        public void TestAdd()
        {

            TestableModelClass TestMe = new TestableModelClass();


            Assert.True(TestMe.Add(3, 2) == 5, "Basic Math Failure");

            Assert.True(TestMe.Add(-3, -2) == -5, "Basic Math Failure");
        }

    }
}

这是命令行中RC1当我们用DNX输出示例:

Example output from commandline in RC1 when we used dnx:

C:\dev\Demo\YetAnotherWebbyDemo\src\YetAnotherWebbyDemoTests>dnx . test

xUnit.net DNX Runner (32-bit DNX 4.5.1)
  Discovering: YetAnotherWebbyDemoTests
  Discovered:  YetAnotherWebbyDemoTests
  Starting:    YetAnotherWebbyDemoTests
    YetAnotherWebbyDemoTests.TestBasics.TestAdd [FAIL]
      Basic Math Failure
      Expected: True
      Actual:   False
      Stack Trace:
        YetAnotherWebbyDemoTestBasics.cs(25,0): at YetAnotherWebbyDemoTests.Test
Basics.TestAdd()
  Finished:    YetAnotherWebbyDemoTests
=== TEST EXECUTION SUMMARY ===
   YetAnotherWebbyDemoTests  Total: 1, Errors: 0, Failed: 1, Skipped: 0, Time: 0.263s

例输出RC2,我们正在使用 DOTNET

D:\dev\aspnet5mvc6xunitdemo\src\YetAnotherWebbyDemoTests>dotnet test
Project YetAnotherWebbyDemo (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Project YetAnotherWebbyDemoTests (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
xUnit.net .NET CLI test runner (64-bit win10-x64)
  Discovering: YetAnotherWebbyDemoTests
  Discovered:  YetAnotherWebbyDemoTests
  Starting:    YetAnotherWebbyDemoTests
    YetAnotherWebbyDemoTests.TestBasics.TestAdd [FAIL]
      Basic Math Failure
      Expected: True
      Actual:   False
      Stack Trace:
        D:\dev\aspnet5mvc6xunitdemo\src\YetAnotherWebbyDemoTests\YetAnotherWebbyDemoTestBasics.cs(26,0): at YetAnotherWebbyDemoTests.TestBasics.TestAdd()
  Finished:    YetAnotherWebbyDemoTests
=== TEST EXECUTION SUMMARY ===
   YetAnotherWebbyDemoTests  Total: 1, Errors: 0, Failed: 1, Skipped: 0, Time: 0.205s
SUMMARY: Total: 1 targets, Passed: 0, Failed: 1.