iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 24
0
Modern Web

Learning ASP.NET core + Angular2 系列 第 24

[.NET Core] Unit Test with xUnit.net

  • 分享至 

  • xImage
  •  

Introduction


One of the MOST IMPORTANT parts on the way to happy coding: Unit Test!
Let us see how to use xUnit.net in .NET Core.

Environment


  • Visual Studio 2015 Update 3
  • .Net Core 1.0.0
  • xunit 2.1.0
  • dotnet-test-xunit 1.0.0-rc2

Implement


Create a new .NET Core Class Library project

Install packages

  1. dotnet-test-xunit
  2. xunit

Create a simple unit test

public class UnitTestDemo
{
        [Fact]
        public void TestSplitCount()
        {
            var input = "Luke Skywalker, Leia Skywalker, Anakin Skywalker";
            var actual = input.Split(',').Count();

            Assert.True(actual.Equals(3), $"Expected:3, Actual:{actual}");
        }
}

Build the VStudio solution, and we can see that the unit test is on the Test Explorer.

Or use the following command to run all tests.

dotnet test

Or run a single one.

dotnet test -method Angular2.Mvc.xUnitTest.UnitTestDemo.TestSplitCount

Use [Theory] to test multiple cases(data)

Here is a sample,

[Theory]
[InlineData("A,B,C")]
[InlineData("AA,BB,CC")]
[InlineData("1,2,3")]
[InlineData("#,^,*")]
public void TestSplitCountComplexInput(string input)
{
    var actual = input.Split(',').Count();
    Assert.True(actual.Equals(3), $"Expected:3, Actual:{actual}");
}

Test result:

Use [Theory] and [MemberData] to test with expected values

We will refactor the previous unit test and let every test has its own expected value.

First, create a TestCase class with test cases,

  • Testcase.cs
public class TestCase
{
        public static readonly List<object[]> Data = new List<object[]>
           {
              new object[]{"A,B,C",3},  //The last value is the expected value
              new object[]{"AA,BB,CC,DD",4},
new object[]{"1,2,3,4,5",5},
              new object[]{"(,&,*,#,!,?",6}
           };

        public static IEnumerable<object[]> TestCaseIndex
        {
            get
            {
                List<object[]> tmp = new List<object[]>();
                for (int i = 0; i < Data.Count; i++)
                    tmp.Add(new object[] { i });
                return tmp;
            }
        }
}
  • UnitTest
[Theory]
[MemberData("TestCaseIndex", MemberType = typeof(TestCase))]
public void TestSplitCountComplexInput(int index)
{
    var input = TestCase.Data[index];
    var value = (string)input[0];
    var expected = (int)input[1];

    var actual = value.Split(',').Count();

    Assert.True(actual.Equals(expected), $"Expected:{expected}, Actual:{actual}");
}

Test Result:

Whaz next?

We will talk about MSTest and decoupling with NSubstitute in the next day-sharing.

Reference


  1. Getting started with xUnit.net (.NET Core / ASP.NET Core)
  2. Stackoverflow : Pass complex parameters to [Theory]

上一篇
[ASP.NET Core] Configuration
下一篇
[.NET Core] Unit Test with MSTest
系列文
Learning ASP.NET core + Angular2 31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言