C# 格式化与 .editorconfig:我可以为单元测试使用不同的命名风格吗?

huangapple go评论51阅读模式
英文:

C# formatting with .editorconfig: can I use a different naming style for unit tests?

问题

有没有办法在 .editorconfig 文件中指定我想要为单元测试使用不同的命名风格?

目前我们使用类似这样的命名:GetAll_GivenNoCustomers_ReturnsEmptyList,但我收到建议从名称中去除下划线:GetAllGivenNoCustomersReturnsEmptyList,而且 dotnet format 命令实际上会更改这个命名。

对于我们的其他方法,这种行为是期望的,只是对于我们的测试不是。

有没有办法不同对待我们的单元测试?
比如,在方法定义之前检测 [Test] 属性的存在?
或者(不太优先)排除我们测试项目的这种命名规则?

我想将所有规则都保留在一个单独的 .editorconfig 文件中,我们希望将其分发到不同应用程序和我们环境中的用户手中。

英文:

Is there a way to specify in an .editorconfig file that I want to use a different naming style for my Unit tests?

Currently we're using something like this: GetAll_GivenNoCustomers_ReturnsEmptyList but I get the suggestion to remove the underscores from the name: GetAllGivenNoCustomersReturnsEmptyList and the dotnet format command actually changes this.

For all our other methods this behavior is desired, just not for our tests.

Is there a way to treat our unit tests differently?
Like a way to sense the presence of the [Test] attributes before the method definition?
Or (less preferred) exclude our test projects from this naming rule?

I would like to keep all rules in one single .editorconfig file, which we would like to distribute across different applications and users in our environment.

答案1

得分: 1

我的理解是你可以在UnitTest项目文件夹中放置一个不同的.editorConfig,它将采用这些设置。

从http://docs.editorconfig.org/en/master/editorconfig-format.html:

文件名和位置

当给EditorConfig提供一个文件名时,将在给定文件的目录以及所有父目录中执行搜索,以查找EditorConfig文件(默认情况下命名为“.editorconfig”)。搜索将在找到一个具有根属性设置为true的EditorConfig文件或达到根文件系统目录时停止。

文件从上到下读取,最近找到的规则优先。如果多个EditorConfig文件具有匹配的部分,那么较近的EditorConfig文件中的规则将最后读取,因此较近的文件中的属性优先。

你需要注意root = true

还要查看这个答案:https://stackoverflow.com/a/40348831/486564,关于从.editorConfig文件中添加特定文件夹的规则。

英文:

My undestanding is that you can place a different .editorConfig in the UnitTest project folder, and it will take those instead.

From http://docs.editorconfig.org/en/master/editorconfig-format.html:

> Filename and Location

> When a filename is given to EditorConfig a search is performed in the directory of the given file and all parent directories for an EditorConfig file (named ".editorconfig" by default). All found EditorConfig files are searched for sections with section names matching the given filename. The search will stop if an EditorConfig file is found with the root property set to true or when reaching the root filesystem directory.

> Files are read top to bottom and the most recent rules found take precedence. If multiple EditorConfig files have matching sections, the rules from the closer EditorConfig file are read last, so properties in closer files take precedence.

You need to watch for root = true

Also check this answer: https://stackoverflow.com/a/40348831/486564 regarding adding rules for certain folders from the .editorConfig file.

huangapple
  • 本文由 发表于 2023年2月16日 19:00:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定