【问题标题】:Is it possible to run karma on tfs build?是否可以在 tfs 构建上运行业力?
【发布时间】:2017-03-23 23:53:28
【问题描述】:

我正在使用 TFS 2010 和 Visual Studio 2012。现在我想对我的 AngularJs 脚本进行单元测试。在 Visual Studio 中,这似乎与 KarmaVs 一起工作,但我如何在 tfs-build 上运行测试?

【问题讨论】:

    标签: angularjs visual-studio-2012 tfs jasmine karma-runner


    【解决方案1】:

    假设 tfs-build 可以调用外部程序,你可以直接执行karma run。不过,您必须在 karma 配置中设置 singleRun:true

    【讨论】:

      【解决方案2】:

      对于自动构建,您可以使用以下 nuget 包,它将使用 karma 和 grunt 运行您的 jasmine 测试。因此,只要您在构建机器上安装了 nodejs,它就应该运行您的单元测试。

      https://www.nuget.org/packages/KarmaGruntJSUnit.MSBuild/

      谢谢

      【讨论】:

        【解决方案3】:

        我已经对此进行了一段时间的研究,并找到了一些解决方案。有些人建议您使用 Chutzpah,这实际上还不错,但如果您的项目很大,可能会变得过于复杂和复杂。另外,如果你使用 Karma 模板,你会遇到一些麻烦,因为 Chutzpah 找不到模块并抛出错误。

        在 TFS 中通过 CI 构建运行测试的最简单方法是在构建过程中添加一个 powershell 脚本。我们正在使用 TFS 2015

        TFS portal

        中间步骤是运行我们的 Angular 单元测试的 PowerShell 脚本。脚本如下所示:

        $ERRORTEXT = "FAILED"
        $FILE = "karmaTest.log"
        $TOTALFAIL = 0
        $TESTFILES = ""
        $TESTRESULT = ""
        $EXITCODE = 0
        
        
        node ../node/node_modules/karma/bin/karma start  "../Bp.Cdn/nodeConfig/karma.all.conf.js" --auto-watch false --single-run true > $FILE
        
        
        $lines = Get-Content $FILE
        foreach ($line in $lines) {
            If ($line | Select-String -Pattern $ERRORTEXT) { # FAIL SCENARIO
                $TESTRESULT = " FAILED "
                $TOTALFAIL = $TOTALFAIL + 1
            } 
            Else { # PASS SCENARIO
                $TESTRESULT = " PASSED "
            }
            $TESTFILES = $TESTFILES + " " + $TESTRESULT + " :  " + $line + "`r`n"
        }
        
        # We can delete the log file now
        Remove-Item $FILE
        
        
        If ($TOTALFAIL -eq 0) { # PASS SCENARIO
            Write-Host ("All Angular tests PASSED!")
            Write-Host ($TESTFILES)
            Write-Host ("Failed files: $TOTALFAIL")
            Write-Host ("All Angular tests PASSED!")  ### This line is here for clarity only when viewing the build console
        } Else { #FAIL SCENARIO
            Write-Host ("One or more Angular tests failed, please correct before re-trying another build: `r`n`r`n")
            Write-Host ($TESTFILES)
            Write-Host ("Failed files: $TOTALFAIL")
            Write-Error ("One or more Angular tests failed, please correct before re-trying another build: `r`n`r`n")  ### DO NOT REMOVE THIS LINE
        }
        
        EXIT
        

        我们有一个批处理文件,它调用我们在开发时使用的 autoWatch 设置为 true 的 Karma 测试,现在通过传入 autowatch 和 singlerun 参数的 powershell 脚本进行相同的调用。现在两者都可以工作并使用相同的 Karma 配置文件。

        祝你好运! :)

        【讨论】:

          猜你喜欢
          • 2012-07-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-07
          • 2013-12-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多