Visual Studio Code Unit Testing

本文介绍如何使用NUnit和xUnit.net进行.NET Core应用的单元测试。通过配置project.json文件并编写测试类,可以方便地进行测试案例的编写与执行。在Visual Studio Code中可以直接运行测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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就可以轻松的运行个别测试

 

转载于:https://www.cnblogs.com/Richeir/p/6397979.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值