Switching Between Using NUnit and MSTest for Unit Testing

前端 未结 3 1252
生来不讨喜
生来不讨喜 2020-12-04 19:54

How can I configure a .NET solution (C#, .NET 2.0) to to allow other developers to make use of the same unit tests for the solution using either NUnit or MSTest?

Ba

3条回答
  •  情书的邮戳
    2020-12-04 20:52

    If you don't want to change any test code (i.e. don't want to add the aliasing at the top), this shim works for me:

    using System;
    using System.Collections;
    
    namespace Microsoft.VisualStudio.TestTools.UnitTesting
    {
        public class Placeholder{}
        public class TestClassAttribute : NUnit.Framework.TestFixtureAttribute
        {
        }
        public class TestInitializeAttribute : NUnit.Framework.SetUpAttribute
        {
        }
        public class TestMethodAttribute : NUnit.Framework.TestAttribute
        {
        }
        public class TestCleanupAttribute : NUnit.Framework.TearDownAttribute
        {
        }
        public class IgnoreAttribute : NUnit.Framework.IgnoreAttribute
        {
        }
        public class ExpectedExceptionAttribute : NUnit.Framework.ExpectedExceptionAttribute
        {
            public ExpectedExceptionAttribute(Type exceptionType) : this(exceptionType, null)
            {
            }
            public ExpectedExceptionAttribute(Type exceptionType, string message) : base(exceptionType)
            {
                UserMessage = message;
            }
        }
        public class TestContext : NUnit.Framework.TestContext
        {
            public TestContext(IDictionary dictionary) : base(dictionary)
            {
            }
        }
        public class Assert : NUnit.Framework.Assert
        {
            public static void IsInstanceOfType(object obj, Type type)
            {
                NUnit.Framework.Assert.IsInstanceOfType (type, obj, null);
            }
            public static void IsInstanceOfType(object obj, Type type, string message)
            {
                NUnit.Framework.Assert.IsInstanceOfType (type, obj, message);
            }
        }
        public class CollectionAssert : NUnit.Framework.CollectionAssert
        {
        }
    }
    

    This has worked for me to run MSTest via NUnit (at least under mono with Xamarin Studio). Just include the file and get references right (you may need a different project file or conditional references), and you're good.

提交回复
热议问题