让编写的单元测试同时支持 NUnit/MSTest

让单元测试代码同时支持NUnit/MSTest,可以参照MSDN magazine,也可以参看 Switching Between Using NUnit and MSTest for Unit TestingUsing both MSTest and NUnit?

using System;

#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Category = Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestContext = System.Object;
using ClassCleanup = NUnit.Framework.TestFixtureTearDownAttribute;
using ClassInitialize = NUnit.Framework.TestFixtureSetUpAttribute;
#endif

 

具体测试代码如下:

 

复制代码
Run your tests in MSTest and in NUnit. This is a cool idea from MSDN magazine.

#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Category = Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestContext = System.Object;
#endif
 
namespace MTest
{
    /// <summary>
    /// A dual MSTest- NUnit Test supported test class.
    /// </summary>
    /// <remarks>
    /// MSTest: there are no categories, so we map Category to Description
    /// There is no RowTest (see DataDriven tests instead)
    /// NUnit: you can't use TestContext (mapped to object)
    /// </remarks>
    [TestClass]
    public class TestUnitTests
    {
        #region SetUp
        private TestContext testContextInstance;
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get { return testContextInstance; }
            set { testContextInstance = value; }
        }
 
        /* Unlike NUnit, in MSTest the class is recreated for each test
        * You can use [ClassInitialize] public void TestFixtureSetUp(TestContext context)
        * but the stored object[s] (eg Repository) must be static too.
        * TestContext is not static so not useful for storing objects.
        */
        [TestInitialize]
        public void TestFixtureSetUp()
        {
            store = 1; //in MSTest this is called per test, in NUnit, once per class
        }
        private int store;
        #endregion
 
        #region Tests
        [TestMethod]
        public void TestMethod1()
        {
            store++;
            Assert.AreEqual(2, store);
        }
 
        [TestMethod]
        [Category("Maths Tests")] /*MSTest doesn't have categories, so we map this to description */
        [ExpectedException(typeof(System.DivideByZeroException))]
        public void DivideMethodTest()
        {
            int d = 0;
            int result = (2 / d);
        }
        #endregion
    }
}
复制代码

 

相关文章:

   NUnit/MSTest Dual Testing

   How to run NUnit tests in Visual Studio 2010/MSTest

 

 

出处:https://www.cnblogs.com/shanyou/archive/2013/03/24/2979540.html

posted on   jack_Meng  阅读(277)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2017-07-07 机器学习--入门答疑

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏

主题色彩