无法通过 PowerShell 脚本将(某些)SpecFlow 测试导入 TFS?

Failing to Import (some) SpecFlow Tests to TFS via PowerShell script?

我在 VSTFS 2015 中每晚为 SpecFlow 测试 运行 自动构建,它是通过 PowerShell 脚本填充的。大多数测试导入没有问题,但是在大约 800 个测试中,大约 30 个根本无法导入。

从 TFS 构建返回的错误是:

TF237086: 无法保存工作项,因为至少一个字段包含不允许的值。

因为这是一个脚本,所以这些特定测试始终无法导入似乎很奇怪。

脚本添加如下(大部分来自ye olde interweb):



    [CmdletBinding()]

    $storage = "$env:BUILD_ARTIFACTSTAGINGDIRECTORY\AcmeCo.Acceptance.Tests.dll"

    Write-Host "Importing tests from '$storage'"

    & 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\TCM.exe' testcase /collection:$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI /teamproject:"AcmeCo" /import /storage:$storage /category:"SomeSuite1" /syncsuite:135777
    & 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\TCM.exe' testcase /collection:$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI /teamproject:"AcmeCo" /import /storage:$storage /category:"SomeSuite2" /syncsuite:148870
    & 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\TCM.exe' testcase /collection:$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI /teamproject:"AcmeCo" /import /storage:$storage /category:"SomeSuite3" /syncsuite:135775
    & 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\TCM.exe' testcase /collection:$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI /teamproject:"AcmeCo" /import /storage:$storage /category:"SomeSuite4" /syncsuite:148872

    & 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\TCM.exe' testcase /collection:$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI /teamproject:"AcmeCo" /import /storage:$storage /category:"SomeSuite5" /syncsuite:135781
    & 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\TCM.exe' testcase /collection:$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI /teamproject:"AcmeCo" /import /storage:$storage /category:"SomeSuite6" /syncsuite:135782
    & 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\TCM.exe' testcase /collection:$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI /teamproject:"AcmeCo" /import /storage:$storage /category:"SomeSuite7" /syncsuite:148869

    Write-Host "Tests imported"

    Write-Host "Updating test plans"
    [Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.Client')
    [Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.TestManagement.Client')
    [Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.Build.Client')

    # Find all test plans using this build definition
    $tpc = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)
    $tcm = $tpc.GetService([Microsoft.TeamFoundation.TestManagement.Client.ITestManagementService])
    $buildServer = $tpc.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])
    $teamProject = $buildServer.GetBuild($Env:BUILD_BUILDURI);
    $testProject = $tcm.GetTeamProject($teamProject.TeamProject);
    $testPlans = $testProject.TestPlans.Query("SELECT * FROM TestPlan")

    $matchingTestPlans = @()
    foreach($testPlan in $testPlans)
    {
        Write-Host "The current Test Plan build definition is: '$testPlan.BuildFilter.AreaPath'"

        if($testPlan.BuildFilter.AreaPath -contains 'AcmeCo Test Plan')
        {
            $matchingTestPlans += $testPlan
        }
    }

    # Update test plans with latest build
    if($matchingTestPlans)
    {
        Write-Host "Updating test plans using '$Env:BUILD_BUILDDEFINITIONNAME' to '$Env:BUILD_BUILDURI' ($($matchingTestPlans.count) matching test plans)"

        foreach ($matchingTestPlan in $matchingTestPlans) {
            $matchingTestPlan.BuildUri = $Env:BUILD_BUILDURI
            $matchingTestPlan.Save()
        }

        Write-Host "Test plans updated"
    }
    else
    {
        Write-Warning "Found no test plans to update."
    }

关于为什么有些测试无法导入的任何想法,或者以前有人遇到过这个问题?

解决find/narrow错误的最佳方法是在 MTM 中手动创建失败的自动化测试用例。

实际上要创建一个测试用例,只需要“Title”值。但是,您还应该再次确认您的测试用例中是否存在某些错误的字段默认值。因为至少有一个字段包含一个不允许的值会导致这个错误。这意味着这可能是一个测试用例模板错误。

更多解决问题的方法请查看这个类似的问题:How to troubleshoot TFS error TF237086 "The work item cannot be saved..."