在 Visual Studio C# 项目中,是否可以强制执行 Microsoft Learn 的测试最佳实践命名约定?

问题描述 投票:0回答:1

Microsoft Learn 建议命名单元测试方法的最佳方式类似于

Add_SingleNumber_ReturnsSameNumber

我想强制项目贡献者遵循此约定,如果名称不包含所有三个部分,则会出现错误。

但是,目前以这种方式命名测试方法会导致违反 CA1707。

虽然我可以使用 GlobalSuppressions 来抑制这种情况:

[assembly: SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "<Pending>", Scope = "module")]

这并不是我真正想要做的,因为它仍然允许测试有不正确的名称。

一位同事建议尝试将以下内容添加到 .editorconfig 中:

dotnet_naming_rule.test_method_names.rule = ^[A-Z][a-zA-Z0-9]*(_[a-zA-Z0-9]+)*$
dotnet_naming_rule.test_method_names.symbols = testMethodName
dotnet_naming_rule.test_method_names.style = camel_case
dotnet_naming_rule.test_method_names.severity = suggestion
dotnet_naming_rule.test_method_names.title = Test method names should be in camelCase and start with an uppercase letter
dotnet_naming_rule.test_method_names.description = Test method names should be in camelCase and start with an uppercase letter
dotnet_naming_rule.test_method_names.where = dotnet_test_methods

然而,Visual Studio 似乎忽略了这一点。

我们后来添加了

dotnet_code_quality.CA1707.exclude = dotnet_test_methods

dotnet_naming_rule.methods_should_not_have_underscores.rule = ^[^_]*$
dotnet_naming_rule.methods_should_not_have_underscores.symbols = methodName
dotnet_naming_rule.methods_should_not_have_underscores.style = identifier_style
dotnet_naming_rule.methods_should_not_have_underscores.severity = suggestion
dotnet_naming_rule.methods_should_not_have_underscores.title = Method names should not contain underscores
dotnet_naming_rule.methods_should_not_have_underscores.description = Method names should not contain underscores
dotnet_naming_rule.methods_should_not_have_underscores.where = dotnet_symbols_without_parameters and !dotnet_test_methods

但这也被 Visual Studio 忽略了。

我可以/应该做些什么来完成这项工作?

c# visual-studio unit-testing coding-style editorconfig
1个回答
0
投票

您从同事那里收到的建议的语法没有意义。它类似于在 .editorconfig 中定义命名规则的语法,但又不完全一样。例如,据我所知,语法没有提供使用正则表达式描述标识符的规定。

正如您已经发现的,Microsoft 在 Visual Studio 中实现 .editorconfig 是在静默失败的极权体制下工作的,这意味着如果 Visual Studio 无法理解 .editorconfig 文件中的一行,那么该行将被忽略,并且您不会给出最轻微的错误、警告、指示或暗示它被忽略。 (你唯一的暗示将是你试图完成的任何事情都不会完成。)

<rant>
我坚信任何个人或团体、组织或政府以任何方式实施、传播或以其他方式促进任何形式的无声失败都应该被枪决,但我离题了。
</rant> 

所以,您给出的语法是错误的。正确的语法解释如下:

https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/naming-rules

为了让事情变得有趣,还有这个:

https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1707

...微软完全不清楚两者之间的关系。

雪上加霜的是,

property = value
的.editorconfig语法不适合定义命名规则这样复杂的事情,但他们还是继续将命名规则定义硬塞到.editorconfig语法中,从而产生了命名规则定义这种机制非常复杂、不直观且难以正确实施,并且与无声的失败一起形成了致命的组合。

我给出的命名规则链接以正式的方式解释了它们,可能适合官方参考资料,但很难理解;所以这里有一个快速参考,更适合快速掌握它:

# General syntax for defining naming rules:
#
# dotnet_naming_style.<StyleName>.capitalization = one of: pascal_case, camel_case, first_word_upper, all_upper, all_lower
# dotnet_naming_style.<StyleName>.required_prefix = <string>
# dotnet_naming_style.<StyleName>.required_suffix = <string>
# dotnet_naming_style.<StyleName>.word_separator = <string>
#
# dotnet_naming_symbols.<SymbolsName>.applicable_kinds = one of: *, namespace, class, struct, interface, enum, property, method, field, event, delegate, parameter, type_parameter, local, local_function
# dotnet_naming_symbols.<SymbolsName>.applicable_accessibilities = one of: *, public, internal or friend, private, protected, protected_internal or protected_friend, private_protected, local
# dotnet_naming_symbols.<SymbolsName>.required_modifiers = one of: abstract or must_inherit, async, const, readonly, static or shared
#
# dotnet_naming_rule.<RuleName>.style = <StyleName>
# dotnet_naming_rule.<RuleName>.symbols = <SymbolsName>
# dotnet_naming_rule.<RuleName>.severity = one of: error, warning, suggestion, silent, none, default
#
# NOTE: You must specify a capitalization style as part of your naming style, otherwise your naming style might be ignored.

我发现使某些命名规则适用于测试并且仅适用于测试的唯一方法是在

[*Tests.cs]
部分中定义这些命名规则,然后确保包含测试方法的所有源文件都以
 结尾...Tests.cs
。虽然很笨重,但确实有用。

© www.soinside.com 2019 - 2024. All rights reserved.