Search code examples
powershelljenkinsaudit-loggingvstest.console.exe

Jenkins, vstest.console.exe, and PowerShell


Problem

This is a bit of a two fold problem.

I am working on a Job that runs unit tests found within a target project. I have two variants that I am working with and I am trying to get one of them to work properly.

Variant A

I am using the VsTestRunner plugin however, I do not know how to capture its input so that I can place that information into an external log.

Variant B

I have forgone the VsTestRunner plugin and am working with the vstest.console.exe through PowerShell, but have found the following problem:

How do I terminate the script and fail the build if any of the tests fail?

I tried looking through the code of the above plugin, but it's in Java (which I have limited experience with) and I could not find how exactly it terminates the code. I also could not find any resources on this... and the documentation for vstest.console.exe has nothing about use cases outside its option variables. I have also tried looking through the vstest.console.exe.config file in hopes that had something I could use... but that was a bust too.

Question

Variant A: Is it possible to capture the output of a Jenkins plugin? If so, how?

Variant B: How do I terminate the script and fail the Jenkins build if any of the tests fail?


Solution

  • Shout out to @4c74356b41 for nudging me in the right direction.

    After re-examining the code, I found that my first problem for Variant B was that I had put the check for $LastExitCode was in the wrong place. This at least allowed me to get the desired fail I was looking for. Once I had this, it was a matter of utilizing some variation of the following:

    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) 
        }