Loading

Visual Studio Code Unit Testing

1、NUnit

project.json

{
  "version": "1.0.0-*",
  "testRunner": "nunit",
  "buildOptions": {
    "debugType": "portable"
  },
  "dependencies": {
    "NUnit": "3.5.0",
    "dotnet-test-nunit": "3.4.0-beta-3"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": "portable-net45+win8",
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      }
    }
  }
}

 

测试类

using System;
using NUnit.Framework;

namespace ClassLibrary
{
    [TestFixture]
    public class Class1
    {
        [Test]
        public void Method1()
        {
            Assert.AreEqual(1101, 1100 + 1);
        }
    }
}

 

然后在集成终端里面输入dotnet test,就可以运行Console Runner

 

 

2、xunit

project.json

{
  "version": "1.0.0-*",
  "testRunner": "xunit",
  "buildOptions": {
    "debugType": "portable"
  },
  "dependencies": {
    "xunit": "2.2.0-beta2-build3300",
    "dotnet-test-xunit": "2.2.0-preview2-build1029"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      }
    }
  }
}

 

 测试类

using System;
using Xunit;

namespace ClassLibrary
{
    public class Class1
    {
        public void Method1()
        {

        }

        [Fact]
        public void PassingTest()
        {
            int a = 5;
            int b = a;
            Assert.Equal(b, Add(2, 2));
        }

        [Fact]
        public void FailingTest()
        {
            Assert.Equal(5, Add(2, 2));
        }

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

 

和NUnit一样,输入dotnet test,就可以运行测试

  

Visual Studio Code现在也可以直接嗅探到测试方法,只需要在上面轻轻点击run test或者debug test就可以轻松的运行个别测试

 

posted @ 2017-02-14 15:51  Richeir  阅读(439)  评论(0编辑  收藏  举报