能否在TFS构建中运行Karma?

3

我使用的是 TFS 2010 和 Visual Studio 2012。现在我想对我的 AngularJs 脚本进行单元测试。在 Visual Studio 中,似乎可以使用 KarmaVs 来实现,但我该如何在 TFS-build 上运行这些测试呢?

3个回答

1

1
假设 tfs-build 可以调用外部程序,你只需执行 karma run。但是在 karma 配置中必须设置 singleRun:true

0

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

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

TFS门户

中间步骤是运行我们的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

我们有一个批处理文件,它将我们的Karma测试调用autoWatch设置为true,我们在开发过程中使用它,现在从powershell脚本中进行相同的调用,并传递autowatch和singlerun参数。现在两者都可以工作并使用相同的Karma配置文件。
祝好运! :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接