使用MS Test进行单元测试

MS Test也可以方便的进行单元测试,可以通过Visual Studio很方便的建立单元测试。

添加对待测试工程的引用,即可方便的开始单元测试。

最基本的一些测试使用如下:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
        }

        [ClassInitialize]
        public static void Init(TestContext context)
        {
            Console.WriteLine("Use ClassCleanup to run code before all tests in a class have run.");
        }

        [TestInitialize]
        public void BeforeTest()
        {
            Console.WriteLine("Use TestCleanup to run code before you run each test.");
        }

        [TestMethod]
        public void TestAMethodOrFunction()
        {
            Assert.AreEqual(3, 3);
        }
        [TestCleanup]
        public void AfterTest()
        {
            Console.WriteLine("Use TestCleanup to run code after you run each test.");
        }

        [ClassCleanup]
        public static void Cleanup()
        {
            Console.WriteLine("Use ClassCleanup to run code after all tests in a class have run.");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestExpectedException()
        {
            throw new ArgumentException("Wrong argument!");
        }
    }
}

其中的标签的作用和NUnit类似,只是名称稍有不同。不做过多解释。
可以通过Visual Studio 的Test菜单,运行进行有关测试的一些操作,如运行指定测试、运行所有测试、查看覆盖率。。。

 例如,我们针对如下一个单元测试通过Test菜单运行所有测试如下:

 

也可以在Test Explorer中控制测试的运行,如

分析代码覆盖率,等等等等

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值