21

如何配置 .NET 解决方案(C#、.NET 2.0)以允许其他开发人员使用 NUnit 或 MSTest 对解决方案使用相同的单元测试?

背景:

在本项目中,部分开发者使用VS2005 Team Edition,部分开发者使用VS2005 Pro,因此并非所有开发者都能运行MSTest。鉴于这是一个企业项目,团队无法使用 TestDriven.net 或 ReSharper。我知道将这些产品中的任何一个与 VS 一起使用都可以解决此问题,但考虑到授权购买许可证所需的时间,购买这些产品中的任何一个都不是一个可行的选择。

提前感谢您的帮助,MagicAndi。

4

3 回答 3

26

我找到的最佳解决方案是使用我在本文中找到的一段简单代码。只需在每个 .cs 测试文件的命名空间部分中使用此代码片段:

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

using NUnitAssert = NUnit.Framework.Assert;
using MsAssert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;

代码片段中的NUNIT是指解决方案的自定义构建配置。您可以使用 VS 配置管理器(通过 VS 工具栏或解决方案属性)创建它。此外,您需要在您的方法上替换 NUnit 的 Test 属性的所有实例以使用 MSTest TestMethod 属性(反之亦然)。

编辑:更新了上面的代码片段,包括对Jamie Ide在评论中指出的问题的可能修复。请注意,我还没有设法测试此修复程序。更新后的代码片段取自 Simon 在这篇文中的评论。

于 2009-04-02T11:29:06.207 回答
9

如果您不想更改任何测试代码(即不想在顶部添加别名),这个 shim 对我有用:

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
    {
    }
}

这对我来说可以通过 NUnit 运行 MSTest(至少在 Xamarin Studio 的单声道下)。只需包含该文件并正确获取引用(您可能需要不同的项目文件或条件引用),就可以了。

于 2015-07-31T06:31:42.940 回答
1

您是否混合了现有的测试?如果没有,或者您不介意转换现有的 MSTest,我会在 NUnit 上进行标准化。我更喜欢 NUnit 而不是 MSTest;它更快,并且不会强迫您在测试类中使用 TestContext 废话。它也更兼容 CI 服务器。

于 2009-04-02T12:13:15.757 回答