【问题标题】:Cannot Associate Test Case in Visual Studio无法在 Visual Studio 中关联测试用例
【发布时间】:2019-01-18 10:56:28
【问题描述】:

我在单元测试 C# 项目文件中有一个标准的 MSTest 单元测试。该项目正在运行 .NET Framework 4.7.2,并安装了 1.3.2 版的 MSTest 适配器和框架。我正在运行 Visual Studio 2017 Enterprise 15.7.6,并且有一个 VSTS 工作区,其中包含一些随机手动创建的测试用例。

当我在测试资源管理器中右键单击我的单元测试并选择“关联到测试用例”时,我可以输入测试用例 ID,添加关联,然后单击“保存”。保存后,我收到一条错误消息,如下所示。

我尝试使用不同版本的 MSTest 和项目文件的不同 .NET Framework 版本来保存关联,但都没有解决问题。我还尝试以管理员身份运行 Visual Studio,但没有成功。有没有其他人遇到过这个问题,或者知道任何解决方法?

【问题讨论】:

  • 在 Visual Studio 中看起来像一个问题。您是否尝试过修复安装?
  • 很遗憾,修复安装并没有解决问题。我将尝试运行旧版本的 VS2017 Enterprise 15.6.7。我有一个与 MS 相关的未决案例,其中至少有一个人能够在 15.7.6 上重现该问题,并且能够通过降低一个到 15.7.5 来解决它。 developercommunity.visualstudio.com/content/problem/309413/…
  • 自更新到VS 2017 15.8.0后,不再显示错误信息。相反,在测试资源管理器中右键单击测试时会禁用“关联到测试用例”。
  • 我还有一个持续的 MS 社区帖子:developercommunity.visualstudio.com/content/problem/309413/…
  • 刚刚确认版本 15.8.0 和 15.8.1 之间的行为是相同的。

标签: c# visual-studio unit-testing tfs azure-devops


【解决方案1】:

我用两个VS2017版本在我这边测试,都运行良好。

例如,我在 VSTS 的一个测试计划中手动添加了一个简单的测试用例,然后我使用 VS2017 15.7.6 在我身边创建了一个带有 .net 4.7.2 的简单单元测试项目,我可以关联到 Test我这边的情况。

如果可能,您可以通过以下步骤在您身边进行测试:

(1) 用其他VS机器测试(有相同VS版本但不在同一台机器上)。

(2) 清理 VSTS 缓存。在解决方案资源管理器窗口中清理并重建您的测试项目,再次测试。

(3)Tools->Options->Work Items,选择“Visual Studio(兼容模式)”。

(4) 如果还是不行,在你的VS中添加一个新的测试简单单元测试项目,删除nuget包:MSTest.TestAdapter和MSTest.TestFramework,然后添加对Microsoft.VisualStudio.QualityTools.UnitTestFramework的本地引用,再次查看结果。

更新:

我将我的 VS2017 更新到 15.8.1 版本,我遇到了同样的问题,该选项默认禁用。这将是一个真正的反馈。

https://developercommunity.visualstudio.com/content/problem/309413/cannot-associate-test-case-in-visual-studio.html?childToView=311392#comment-311392

遇到相同问题的其他成员可以投票。

【讨论】:

  • 自安装更新 15.8.0 以来,所有测试的“关联到测试用例”按钮都是灰色的。测试项目当前安装了 v1.3.2 的测试包。我用其他机器测试过,都是用VS2017的新旧版本,都不行。执行清理 > 重建不起作用。在兼容模式下运行工作项浏览器不起作用。
  • 当我删除 MSTest NuGet 包并添加对 QualityTools.UnitTestFramework 程序集的直接本地引用时,“关联到测试用例”选项不再灰显,但我仍然收到来自的错误消息原来的帖子。建议的四个步骤都没有解决问题。
  • @srsedate,最新版本确实有这个问题,“Associate to Test Case”在所有测试中都是灰色的,我已经根据您在开发者社区网站的反馈向产品团队报告了这个问题。但是,如果我删除这些包并参考 QualityTools.UnitTestFramework 程序集,它在我这边运行良好。你在你的 VS 机器上安装了 MTM 吗?或者您可以将新的测试用例添加到新的测试计划中,然后再次查看结果。其他成员提供其他解决方法,例如使用 Rest API:stackoverflow.com/questions/44581836/…
  • 我已经在一台安装了 MTM 的机器上尝试了这个,另一台没有安装 MTM,结果仍然相同。
  • @srsedate,最新信息:MsTestV2 测试用例关联在 15.8.x 中被禁用,因为该功能尚未完全准备好。它将在 15.9 预览版 2 中启用。
【解决方案2】:

我放在一起并且现在只使用而不是手动关联的解决方法是在测试方法名称中提供测试用例 ID,并通过添加以下 PowerShell 脚本在 TFS 中通过其余 API 自动更新案例以在在 TFS 中成功构建。

使用测试方法的完整命名空间生成每个测试的 GUID,需要添加到“/fields/Microsoft.VSTS.TCM.AutomatedTestId”

这需要根据您自己的 TFS 身份验证方法和可能的 TFS 版本(我使用的是 2017.2)以及您需要读入的测试类型进行调整。这支持编码 UI 和 xUnit。如果您根本不使用 Coded UI,则可以删除顶部的 LoadFrom。

param (
    [string]$Dll = $(throw "-path to test Dll is required.")    
)
Write-Warning "$Dll will be locked until this powershell session closes"
#Load for CodedUi Support
[Reflection.Assembly]::LoadFrom(("C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.TestTools.UITesting.dll"))
[Reflection.Assembly]::LoadFrom(("C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll"))

try {
    $tests = ([Reflection.Assembly]::LoadFrom(($Dll)).GetTypes().GetMethods() | Where-Object { $_.GetCustomAttributes($false) | Where-Object {$_.TypeId.Name -icontains 'TestMethodAttribute' -or $_.TypeId.Name -icontains 'FactAttribute' -or $_.TypeId.Name -icontains 'SkippableFactAttribute' -or $_.TypeId.Name -icontains 'TheoryAttribute'}} | ForEach-Object { @{ Class = $_.DeclaringType.Name; Name = $_.Name; FullName = $_.DeclaringType.FullName + "."  + $_.Name; }})
}
catch {

    Write-Error "Could not load or read $dll" -ErrorAction Stop
}


foreach ($test in $tests)
{
    $sha1 = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider;
    $nameHash = $sha1.ComputeHash([System.Text.Encoding]::Unicode.GetBytes($test.FullName));
    [byte[]]$toGuid = [System.Byte[]]::CreateInstance([System.Byte],16);
    [System.Array]::Copy($nameHash, $toGuid, 16);
    $guid = [guid]::new($toGuid);
    $id = ([Regex]::Match($test.Name, "(\d+)(?!.*\d)").Value)

    try {
        if ($psversiontable.PSVersion.Major -lt 6) {
            $currentGUID = (Invoke-RestMethod "http://{instance}[/{team-project}]/_apis/wit/workitems/$($id)?api-version=3.0-preview" -Method Get -UseBasicParsing -UseDefaultCredentials).Fields.'Microsoft.VSTS.TCM.AutomatedTestId'
        }
        else {
            $currentGUID = (Invoke-RestMethod "http://{instance}[/{team-project}]/_apis/wit/workitems/$($id)?api-version=3.0-preview" -Method Get -UseBasicParsing -UseDefaultCredentials -AllowUnencryptedAuthentication).Fields.'Microsoft.VSTS.TCM.AutomatedTestId'
        }
    }
    catch {
        $currentGUID = $null;
    }

    if($currentGUID -ne $guid)
    {       
        Write-Host "Updating $id."
        [array]$hash = @{
            op = "add";
            path = "/fields/Microsoft.VSTS.TCM.AutomatedTestName";
            from = $null;
            value = $test.FullName;        
        },@{
            op = "add";
            path = "/fields/Microsoft.VSTS.TCM.AutomatedTestStorage";
            from = $null;
            value = (Split-Path $DLL -leaf);
        },@{
            op = "add";
            path = "/fields/Microsoft.VSTS.TCM.AutomatedTestId";
            from = $null;
            value = $guid;
        },@{
            op = "add";
            path = "/fields/Microsoft.VSTS.TCM.AutomationStatus";
            from = $null;
            value = "Automated";
        },@{
            op = "add";
            path = "/fields/System.Reason";
            from = $null;
            value = "Completed";
        },@{
            op = "add";
            path = "/fields/System.State";
            from = $null;
            value = "Ready";
        }

        $patch = Convertto-json $hash -Compress

        write-host $test.Name
        write-host "http://{instance}[/{team-project}]/_apis/wit/workitems/$($id)?api-version=3.0-preview"
        if ($psversiontable.PSVersion.Major -lt 6) {
            $result = Invoke-RestMethod "http://{instance}[/{team-project}]/_apis/wit/workitems/$($id)?api-version=3.0-preview" -Method Patch -UseBasicParsing -UseDefaultCredentials -Body $patch -ContentType "application/json-patch+json"
        }
        else {
            $result = Invoke-RestMethod "http://{instance}[/{team-project}]/_apis/wit/workitems/$($id)?api-version=3.0-preview" -Method Patch -UseBasicParsing -UseDefaultCredentials -Body $patch -ContentType "application/json-patch+json" -AllowUnencryptedAuthentication
        }
    }
    else {
        Write-Host "No changes to $id."
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2020-04-13
    • 1970-01-01
    • 2012-11-19
    相关资源
    最近更新 更多