使用Xunit進行單元測試

http://xunit.github.io/docs/getting-started-desktop.htmljavascript

 

1. 新建一個類庫項目html

2. 經過NuGet引入xunit,Shouldly,xunit.runner.visualstudio三個程序包。java

3. 編寫代碼git

public class Class1
{
    public int Add(int x, int y)
    {
        return x + y;
    }

    public string Reverse(string str)
    {
        return new string(str.Reverse().ToArray());
    }
}

  

public class Class1Tests
{
    [Fact]
    public void AddTest()
    {
        var class1 = new Class1();
        class1.Add(2, 2).ShouldBe(4);
    }

    [Fact]
    public void ReverseTest()
    {
        var class1 = new Class1();
        class1.Reverse("hello").ShouldBe("olleh");
    }
    [Fact]
    public void ReverseWithNull_Test()
    {
        var class1 = new Class1();
        class1.Reverse(null).ShouldBe(null);
    }
}

  

4. 在測試方法上右鍵執行github

—————————————————————— app

ps:測試管理資源器打開方式單元測試

附: VS插件測試

——————ui

.Net Core項目中使用 Xunit進行單元測試  插件

 .NET Core系列 :4 測試

{
  "version": "1.0.0-*",
  "testRunner": "xunit",
  "dependencies": {

    "xunit":"2.2.0-beta4-build3444",
    "Shouldly": "2.8.2",
    "xunit.runner.visualstudio": "2.2.0-beta4-build1194",
    "dotnet-test-xunit":   "2.2.0-preview2-build1029"
  },

  "frameworks": {
    "net452":{}
  }
}

 

版本兼容

{
  "version": "1.0.0-*",
  "testRunner": "xunit",

  "dependencies": {
    "NSubstitute": "2.0.3",
    "Shouldly": "2.8.3",
    "xunit": "2.2.0",
    "xunit.runner.visualstudio": "2.2.0"
  },
  "runtimes": {
    "win10-x64": {}
  },
  "frameworks": {
    "net46": {
      "dependencies": {
        "dotnet-test-xunit": "2.2.0-preview2-build1029"
      }
    },
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NET.Test.Sdk": "15.0.0"
      }
    }
  }
}
相關文章
相關標籤/搜索