正则表达式以匹配TestMethods

时间:2019-01-15 19:37:29

标签: c# regex unit-testing

对于我们的应用程序,我们有大约4000个单元测试,如果我们将您的代码签入tfs,它将自动执行。

我们在Build-Definition上进行了很多更改,因此现在要求所有单元测试必须具有属性[TestCategory(TestCategories.GatedCheckin)]才能在gated-checkin中执行。

不幸的是,在4000个中只有700个单元测试已经具有此属性。现在,我必须将该属性添加到其余的单元测试中。

为此,我编写了一个小的VisualStudio-Extension,可以在其中打开源文件并搜索以下正则表达式:

^([\t]|[ ])*\[TestMethod\]([\t]|[ ]|[\w\/äÄüÜöÖß])*([\r\n]+)([\t]|[ ])*public

此正则表达式仅适用于以下单元测试:

[TestMethod]
public void PathIsValidTest1()
{...}

[TestMethod] // another test
public void Calculator_Add_3_And_3_Equals_6_Test()
{...}

但是对于单元测试,它还包含另一个属性,例如:

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void ThrowOnInputTooLongTest2()
{...}

正则表达式不起作用。

如何修改正则表达式,使其与所有[TestMethod]属性而不是[TestCategory(TestCategories.GatedCheckin)]属性的单元测试匹配?

我想用?!进行负向前瞻,但我没有使它起作用。

有什么想法吗?


我已经修改了Addison提供的解决方案,看起来像这样:

^[\t ]*\[TestMethod\][ \t\w\/äÄüÜöÖß]*(\n([ \t]*)\[(?!TestCategory\(TestCategories\.GatedCheckin\)).+\][ \t\w\/äÄüÜöÖß]*)?\s+public

如果我在regex101中使用它,则可以正常运行,如您所见here

但是如果我在C#中使用以下正则表达式,则:

string content = File.ReadAllText(file);    
Regex regex = new Regex(pattern, RegexOptions.Multiline);
int matchCount = regex.Matches(content).Count;  

我只有2场比赛。

1 个答案:

答案 0 :(得分:1)

好问题!

我设法做到这一点:

^[\t ]*\[TestMethod\][ \t\w\/äÄüÜöÖß]*(\n\[(?!TestCategory\(TestCategories\.GatedCheckin\)).+\][ \t\w\/äÄüÜöÖß]*)*\s+public

我添加了另一个捕获字段(并简化了正则表达式的其余部分),以便它现在在第一个[]之后检查是否有其他数量的[TestMethod],并且仅当且仅当他们都不是[TestCategory(TestCategories.GatedCheckin)]

Test it online

以下是一些C#代码:

using System;
using System.Text.RegularExpressions;

namespace Programming {
    class Program {
        static void Main() {
            string content = "namespace MyNameSpace1\n{\n    [TestClass]\n    public class GetMapPathActionTests\n    {\n        [TestMethod] // 1\n        public void GetMapPath_with_All_Attibutes()\n        {\n            ...\n        }\n        [TestMethod] // 2\n        [ExpectedException(typeof(DBInstallInfoConverterCrossCheckerRequiredChildNotFoundException))]\n        public void GetMapPath_with_Empty_Input()\n        {\n           \n        }\n        [TestMethod] // 3\n        [ExpectedException(typeof(DBInstallInfoConverterCrossCheckerRequiredChildNotFoundException))]\n        public void GetMapPath_with_Empty_Output()\n        {\n            \n        }\n        [TestMethod] // 4\n        public void GetMapPath_with_Empty()\n        {\n            \n        }\n        [TestMethod] // 5\n        [ExpectedException(typeof(DBInstallInfoConverterCrossCheckerRequiredChildNotFoundException))]\n        public void GetMapPath_with_All_Attibutes_Empty()\n        {\n            \n        }\n    }\n}\n";
            Regex regex = new Regex(@"^[\t ]*\[TestMethod\][ \t\w\/äÄüÜöÖß]*(\s+\[(?!TestCategory\(TestCategories\.GatedCheckin\)).+\][ \t\w\/äÄüÜöÖß]*)?\s+public", RegexOptions.Multiline);
            MatchCollection matches = regex.Matches(content);

            foreach (Match match in matches) {
                foreach (Capture capture in match.Captures) {
                    Console.WriteLine(capture.Value);
                }
            }
        }
    }
}
相关问题