詹金斯、vstest.console.exe 和 PowerShell

Jenkins, vstest.console.exe, and PowerShell

问题

这是一个双重问题。

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

变体 A

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

变体 B

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

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

我尝试查看上述插件的代码,但它在 Java 中(我对它的经验有限),我找不到它究竟是如何终止代码的。我也找不到关于此的任何资源... vstest.console.exe 的文档在其选项变量之外没有任何关于用例的信息。我还尝试查看 vstest.console.exe.config 文件,希望其中有一些我可以使用的东西……但这也是一个失败。

问题

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

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

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

重新检查代码后,我发现 变体 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) 
    }