在使用 NUnit 和 MSTest 进行单元测试之间切换

发布于 2024-07-16 10:03:52 字数 328 浏览 6 评论 0原文

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

背景:

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

在此先感谢您的帮助, 魔术安迪。

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?

Background:

In this project, some developers use VS2005 Team Edition, and others make use of VS2005 Pro, so not all of the developers are able to run MSTest. Given that this is an Enterprise project, the team is not able to make use of TestDriven.net or ReSharper. I am aware using either of these products with VS would resolve this issue, but given the time it would take to authorize the purchase of licenses, buying either of these products isn't a viable option.

Thanks in advance for your help,
MagicAndi.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

z祗昰~ 2024-07-23 10:03:52

我发现的最好的解决方案是使用我在

#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 对此的评论 博客文章

The best solution I have found is to make use of a simple piece of code I found in this article. Simply use this code snippet in the namespace section of each .cs test file:

#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;

The NUNIT in the code snippet refers to a custom build configuration for the solution. You can create this using the VS Configuration Manager (via the VS toolbar or the solution properties). Also, you need to replace all instances of the NUnit's Test attribute on your methods to make use of the MSTest TestMethod attribute (or vice versa).

EDIT: Updated the code snippet above to include a possible fix for the issue Jamie Ide pointed out in the comments. Note, I haven't managed to test this fix. The updated code snippet is taken from a comment by Simon on this blog post.

魔法少女 2024-07-23 10:03:52

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

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 的单声道下)。 只需包含该文件并获取正确的引用即可(您可能需要不同的项目文件或条件引用) ,你很好。

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.

清欢 2024-07-23 10:03:52

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

Do you have a mix of existing tests? If not, or you don't mind converting existing MSTests, I would standardize on NUnit. I strongly prefer NUnit over MSTest; it's faster and it doesn't force you to have the TestContext nonsense in your test classes. It's also more compatible with CI servers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文