同时使用 MSTest 和 NUnit?

发布于 2024-10-18 03:15:05 字数 558 浏览 5 评论 0原文

阅读有关 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 并使用 Fluent Assertions 为我提供更灵活的方式来制定断言语句。这难道不是最好的选择吗?

Reading about MSTest and NUnit I couldn't really decide what to use in my project. We use TFS 2008 and VS2010.

I like MSTest because of its integration into VS2010, Continuous Integration and Code Coverage reports.
I like NUnit because it allows to formulate complex assert statements in a nice, readable fashion.

Stumbling upon http://alsagile.com/archive/2010/03/09/stop-the-war-between-nunit-and-mstest-make-them.aspx I ask the community: is it possible to use both?

I also think about sticking to MSTest and use Fluent Assertions to give me a more flexible way in formulating assert statements. Wouldn't this be the best option after all?

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

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

发布评论

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

评论(3

送你一个梦 2024-10-25 03:15:05

我个人不喜欢混合两个框架的想法,就像 您所指的文章

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

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

为了能够让您的单元测试在两个框架下运行,您需要定义一个构建常量(此处为 NUNIT )并在 预处理器指令命名空间别名 你最终会得到这样的代码片段:

#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 杂志文章。另一篇涉及该主题的有价值的博客文章是:

对于你的最后一个问题:Fluent Assertions 项目似乎是一个很好的库。如果您喜欢它流畅的风格,我看不出有什么真正的理由不应该使用它。

I personally don't like the idea of mixing two frameworks like it's mentioned in the article you are referring to.

A possible condition to make your unit test run under both test frameworks could be that you do not want or can not install Visual Studio on your continuous integration server.

Just for clarification, MSTest is not the Visual Studio Unit Testing Framework defined in the Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll assembly.

To be able to let your unit test run under both frameworks you would need to define a build constant (here NUNIT) and with the help of preprocessor directives and namespace aliases you would end up with a snippet like this:

#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

I believe I came across this idea first through this MSDN Magazine article. Another worthwhile blog post covering this topic would be:

To your last question: The Fluent Assertions project seems to be a good library. I do not see any real reasons why you shouldn't use it if you like the fluent style of it.

又爬满兰若 2024-10-25 03:15:05

当然,您可以拥有一个使用 MSTest 的测试项目,而另一个使用 NUnit 的测试项目。如果您想在同一个项目中混合使用两者,则必须坚持共同功能并使用编译器指令和 using 语句来“重命名”并编译为某些功能。

不过,您必须同时运行 nunit 和 mstest 才能确定所有测试是否真的通过,因此我强烈建议不要尝试同时使用两者。选择一个。

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

You can certainly have one test project using MSTest and another using NUnit. If you want to mix both in the same project, you'll have to stick to common features and use compiler directives and using statements to "rename" and compile to certain ones.

You'd have to run both nunit and mstest though to figure out if all your tests really pass, so I'd strongly advise against trying to use both. Pick one.

If you want to run tests on a build server, probably the best bet is NUnit, unless you want to install VS on your build server as well. TFS may work without installing VS though, but you'll have to check your documentation.

深海蓝天 2024-10-25 03:15:05

添加对 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);
}

Add a reference to nunit.framework and the following line to the top of your MSTest tester class...

using NAssert = NUnit.Framework.Assert;

Now you can use either...

// Test whether a new SimplexInterpreter was created
[TestMethod]
public void SimplexInterpreterConstructorTest()
{
    Assert.IsNotNull(target);
    NAssert.IsNotNull(target);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文