【问题标题】:Jenkins, vstest.console.exe, and PowerShellJenkins、vstest.console.exe 和 PowerShell
【发布时间】:2017-12-01 20:13:58
【问题描述】:

问题

这是一个双重问题。

我正在处理一个运行在目标项目中找到的单元测试的作业。我正在使用两种变体,我正在尝试让其中一种正常工作。

变体 A

我正在使用 VsTestRunner 插件,但是我不知道如何捕获它的输入,以便我可以将该信息放入外部日志中。

变体 B

我已经放弃了 VsTestRunner 插件,正在通过 PowerShell 使用 vstest.console.exe,但发现了以下问题:

如果任何测试失败,我如何终止脚本并使构建失败?

我尝试查看上述插件的代码,但它是用 Java 编写的(我的经验有限),我无法找到它究竟是如何终止代码的。我也找不到这方面的任何资源......而且 vstest.console.exe 的文档在其选项变量之外没有任何关于用例的内容。我也尝试过查看 vstest.console.exe.config 文件,希望有一些我可以使用的东西……但这也太失败了。

问题

变体 A:是否可以捕获 Jenkins 插件的输出?如果有,怎么做?

变体 B:如果任何测试失败,我如何终止脚本并使 Jenkins 构建失败?

【问题讨论】:

  • 测试失败时是否返回正确的退出代码?
  • @4c74356b41 我不能告诉你。我不知道如何从这个命令文件中访问信息。当我运行该文件时,我似乎无法从中获得“最后退出代码”。可能是我缺乏知识让我做错了。但是,由于代码的其他部分都没有失败,我最后存在的代码往往是'0'
  • 好吧,您应该阅读 vstest.console.exe 或尝试使用 powershell,从 powershell 启动它后检查 $lastexitcode 并失败

标签: powershell jenkins audit-logging vstest.console.exe


【解决方案1】:

向@4c74356b41 大声疾呼,让我朝着正确的方向前进。

重新检查代码后,我发现Variant B 的第一个问题是我把$LastExitCode 的检查放错了地方。这至少让我得到了我想要的失败。一旦我有了这个,就需要利用以下的一些变体:

Try {
    & $vsTestConsole $testFile /any /necessary /options

    If ($LastExitCode -ne 0) {
        throw "TestFailure"
    }
}
Catch {
    # Custom error specifically for failed tests
    If ($_.Exception.Message -ieq 'TestFailure') {
        Write-Host "One or more tests failed, ending action"
        # Terminate PS Script with an Exit Code of 1 so that Jenkins Fails the build
        # Exiting in this manner also prevents the addition of unnecessary fluff
        # This way you can fully customize the error response
        [Environment]::Exit(1) 
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 2017-05-29
    • 2015-10-16
    相关资源
    最近更新 更多