首页 > 解决方案 > 如何为 mstest 编写多线问题匹配器?

问题描述

我在多个文件中创建了许多失败的测试,以获得一些好看的输出。

Test run for c:\Users\anthony.mastrean.CORP\Work\gitlab.com\anthonymastrean\simplenetq\test\SimpleNetQ.Tests\bin\Debug\netcoreapp3.0\SimpleNetQ.Tests.dll(.NETCoreApp,Version=v3.0)
Microsoft (R) Test Execution Command Line Tool Version 16.0.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
Failed   TestMethod1
Error Message:
 Assert.Fail failed. 
Stack Trace:
   at SimpleNetQ.Tests.UnitTest1.TestMethod1() in c:\Users\anthony.mastrean.CORP\Work\gitlab.com\anthonymastrean\simplenetq\test\SimpleNetQ.Tests\UnitTest1.cs:line 11

Failed   TestMethod2
Error Message:
 Assert.Fail failed. 
Stack Trace:
   at SimpleNetQ.Tests.UnitTest1.TestMethod2() in c:\Users\anthony.mastrean.CORP\Work\gitlab.com\anthonymastrean\simplenetq\test\SimpleNetQ.Tests\UnitTest1.cs:line 17

Failed   TestMethod1
Error Message:
 Assert.Fail failed. 
Stack Trace:
   at SimpleNetQ.Tests.UnitTest2.TestMethod1() in c:\Users\anthony.mastrean.CORP\Work\gitlab.com\anthonymastrean\simplenetq\test\SimpleNetQ.Tests\UnitTest2.cs:line 11


Total tests: 3. Passed: 0. Failed: 3. Skipped: 0.
Test Run Failed.
Test execution time: 0.5951 Seconds

我制定了这个单行问题匹配器。但是,很明显,它只匹配文件和行。

        {
            "label": "test",
            "type": "process",
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "command": "dotnet",
            "args": ["test"],
            "problemMatcher": {
                "fileLocation": ["absolute"],
                "severity": "error",
                "pattern": {
                    // https://regex101.com/r/SlyIHb/1
                    "regexp": "^\\s+at\\s+(.*)\\s+in\\s+(.*):line\\s+(\\d+)\\s*$",
                    "file": 2,
                    "line": 3
                }
            }
        }

在此处输入图像描述

默认情况下,代码似乎将整个匹配的行作为消息。让我们尝试做得比这更好。mstest 将消息分成两行并没有帮助。

        {
            "label": "test",
            "type": "process",
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "command": "dotnet",
            "args": ["test"],
            "problemMatcher": {
                "fileLocation": ["absolute"],
                "severity": "error",
                "pattern": [
                    {
                        // "regexp": "^\\s+(.*)\\s+failed\\..*$",
                        "regexp": "^Failed\\s+(.*)\\s*$",
                        "message": 1
                    },
                    {
                        // https://regex101.com/r/SlyIHb/1
                        "regexp": "^\\s+at\\s+(.*)\\s+in\\s+(.*):line\\s+(\\d+)\\s*$",
                        "file": 2,
                        "line": 3
                    }
                ]
            }
        }

无论我匹配方法名称前的“失败”一词还是断言后的“失败”一词,它都不起作用。如果我更改模式项目的顺序似乎没有帮助。

标签: visual-studio-codemstest

解决方案


推荐阅读