如何强制所有测试在 NUnit 中具有单一类别?

发布于 2024-09-15 21:53:07 字数 424 浏览 5 评论 0原文

我使用的是 NUnit 2.5.3,但如果更新版本的 NUnit 可以解决我的问题,那么升级就不是问题了。

我们有几个不同的测试类别。不同类别的测试将在不同风格的构建期间运行(用于签入的快速运行单元测试、所有单元测试以及用于隔夜等的具有外部依赖项(数据库等)的其他测试)。将真正的单元测试和具有外部依赖项的测试分离到不同的程序集中是一个长期目标,但我仍然希望在测试上强制执行类别。

我想确保所有测试方法都有一个使用有效类别名称定义的 CategoryAttribute。有效的类别名称是我可以定义的列表中的一个。然而,仅仅对每个测试方法强制执行一个类别将是朝着正确方向迈出的一步。

NUnit 是否支持这种开箱即用的行为?除了支持这种强制执行的 CategoryAttribute 之外,是否还有另一个我可以使用的 NUnit 属性?或者是实施一个通过反射检查程序集的自定义构建步骤?

I'm using NUnit 2.5.3, but if a more recent version of NUnit solves my problem, it won't be an issue to upgrade.

We have a couple of different categories for tests. Different categories of tests will be run during different flavours of build (fast running unit tests for checkins, all unit tests and other tests with external dependencies (database etc.) for overnighters etc.). Divorcing the true unit tests and the tests with external dependencies into different assemblies is a longer term objective, but I'll still want to enforce categories on the tests.

I want to ensure that all test methods have a single CategoryAttribute defined with a valid category name. A valid category name would be one from a list that I could define. However, just enforcing a Category on each test method would be a step in the right direction.

Does NUnit supports this kind of behaviour out of the box? Is there another NUnit attribute I can use other than CategoryAttribute which supports that kind of enforcement? Or is implementing a custom build step which checks the assemblies via reflection the way to go?

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

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

发布评论

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

评论(2

听闻余生 2024-09-22 21:53:10

从(至少)nUnit v2.9.1 开始, CategoryAttribute(用于将类别分配给 nUnit 测试)支持分配给整个程序集。

设置此属性会将指定的类别应用于程序集/项目中的所有测试。通常,这是在 AssemblyInfo.cs 中完成的,但从技术上讲,它可以在任何代码文件中完成。

示例:

using NUnit.Framework;

[assembly:Category("Integration")]

上面的示例将“集成”的类别值应用于项目内的所有测试。

Since (at least) nUnit v2.9.1 the CategoryAttribute (which is used to assign categories to nUnit tests) supports being assigned to entire assemblies.

Setting this attribute will apply the specified category to all tests in the assembly / project. Typically this is done in AssemblyInfo.cs, however technically it can be done in any codefile.

Example:

using NUnit.Framework;

[assembly:Category("Integration")]

The above example applies the Category value of "Integration" to all tests within the project.

身边 2024-09-22 21:53:10

我认为没有办法确保所有测试都标有 Category 属性 - 这就是为什么我总是将不同类型的测试拆分为不同的程序集(拆分遗留程序集应该不会太难,特别是如果您正在使用 Resharper 等工具)。因此,您必须按照您所说的那样编写自己的工具,该工具将加载程序集并进行验证。

另外,这些想法可能会有所帮助:

  1. 您可以将 Category 属性放在 TestFixture 级别上,这样会更容易记住它并进行验证

  2. 您可以创建从类别属性派生的自定义属性。这样可以更轻松地确保类别名称中没有拼写错误

请参阅:http://www.nunit.org/index.php?p=category&r=2.4.8

I don't think there is a way to ensure that all tests are marked with the Category attribute - that's why I always split different types of tests into different assemblies (it shouldn't be too hard to split a legacy assembly, especially if you are using tools like Resharper). So you would have to write your own tool as you said which would load the assembly and do validation.

Also, these ideas might help:

  1. You can place the Category attribute on the TestFixture level, this way it will be a bit easier to remember about it and do the validation

  2. You can create custom attributes derived from the Category attribute. This way its easier to ensure that there are no spelling mistakes in category names

See: http://www.nunit.org/index.php?p=category&r=2.4.8

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