0

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?

Brandon
  • 731
  • 2
  • 12
  • 29
  • does it return a proper exit code when tests fail? – 4c74356b41 Dec 01 '17 at 20:30
  • @4c74356b41 I couldn't tell you. I have no idea how to access the information from this command file. When I run the file, I can't seem get a 'last exit code' from it. It is possible that my lack of knowledge has me doing it all wrong. However, since none of the other parts of the code fails, my last exist code tends to be '0' – Brandon Dec 01 '17 at 21:31
  • well, you should either read up on the vstest.console.exe or experiment with powershell, check $lastexitcode after you launch it from powershell and it fails – 4c74356b41 Dec 01 '17 at 21:35

1 Answers1

1

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) 
    }
Brandon
  • 731
  • 2
  • 12
  • 29