使用MSTest和NUnit?

时间:2011-02-19 21:11:24

标签: nunit mstest

阅读MSTest和NUnit我无法真正决定在我的项目中使用什么。我们使用TFS 2008和VS2010。

我喜欢MSTest,因为它集成了VS2010,持续集成和代码覆盖率报告。 我喜欢NUnit,因为它允许以一种漂亮,可读的方式表达复杂的断言语句。

绊倒http://alsagile.com/archive/2010/03/09/stop-the-war-between-nunit-and-mstest-make-them.aspx我问社区:是否有可能同时使用两者?

我还考虑坚持使用MSTest并使用http://fluentassertions.codeplex.com为我提供一种更灵活的方法来制定断言语句。这不是最好的选择吗?

3 个答案:

答案 0 :(得分:23)

我个人不喜欢混合两个框架的想法,就像你所指的article中提到的那样。

在两个测试框架下运行单元测试的一个可能条件可能是您不希望或无法在持续集成服务器上安装Visual Studio。

为了澄清,MSTest不是Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll程序集中定义的Visual Studio单元测试框架。

为了能够在两个框架下运行您的单元测试,您需要定义构建常量(此处为NUNIT)并在preprocessor directivesnamespace aliases的帮助下结束用这样的片段:

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

我相信我首先通过MSDN Magazine article发现了这个想法。关于这个主题的另一篇有价值的博客文章将是:

关于你的上一个问题:Fluent Assertions项目似乎是一个很好的图书馆。如果你喜欢它的流畅风格,我没有看到任何你不应该使用它的真正原因。

答案 1 :(得分:2)

将对nunit.framework和以下行的引用添加到MSTest测试器类的顶部...

using NAssert = NUnit.Framework.Assert;

现在你可以使用......

// Test whether a new SimplexInterpreter was created
[TestMethod]
public void SimplexInterpreterConstructorTest()
{
    Assert.IsNotNull(target);
    NAssert.IsNotNull(target);
}

答案 2 :(得分:1)

您当然可以使用MSTest进行一个测试项目,使用NUnit进行另一个测试项目。如果要在同一个项目中混合两者,则必须坚持使用常用功能并使用编译器指令并使用语句“重命名”并编译为某些。

你必须同时运行nunit和mstest来确定你的所有测试是否真的通过了,所以我强烈建议你不要试图同时使用它们。选一个。

如果你想在构建服务器上运行测试,可能最好的选择是NUnit,除非你想在你的构建服务器上安装VS.虽然TFS可以在不安装VS的情况下工作,但您必须检查文档。