切换使用NUnit和MSTest的单元测试之间单元测试、NUnit、MSTest

2023-09-02 02:07:34 作者:开着拖拉机去相亲i

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

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?

背景:

在这个项目中,一些开发人员使用VS2005团队版,和其他人使用VS2005的专业,所以开发商不是所有人都能够运行MSTest的。考虑到这是一个企业项目,团队是不是能够利用TestDriven.net或ReSharper的的。我知道无论用这些产品与VS会解决这个问题,但由于时间,将采取授权购买的许可证,购买其中任何一个产品是不是一个可行的选择。

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.

在此先感谢您的帮助, MagicAndi。

Thanks in advance for your help, MagicAndi.

推荐答案

我已经找到了最好的解决方案就是使用一段简单的code,我发现在这的文章。只需在每个测试的.cs文件的命名空间部分使用code片断:

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;

NUNIT 在code段指的是自定义生成配置的解决方案。您可以在此使用VS配置管理器(通过VS工具栏或溶液性质)创建。此外,你需要更换你的方法的NUnit的测试属性的所有实例,以利用MSTest的TestMethod的属性(或者相反)。

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).

编辑:更新上面的code段包括一个可能的修复该问题杰米井指出在该意见。请注意,我还没有成功地测试此修复程序。更新后的code片段取自这个评论由西蒙·blog帖子。

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.

 
精彩推荐