Building Go programs in TeamCity

TeamCity provides support for multiple technologies and programming languages. In TeamCity 2019.1, support for Go has been included in the distribution. In this blog post, we will explain how to configure TeamCity to work with Go programs.

Configuring Golang build feature

To enable Go support in TeamCity, go to Build Configuration Settings | Build Features, click Add build feature, and select Golang from the list.

The Golang build feature enables the real-time reporting and history of Go test results in TeamCity.

golang-build-feature

Running Go tests

Once the Goland build feature is enabled, TeamCity will parse the JSON output of go test command. Hence, the command should be executed with a -json flag using one of these two methods:

  • Add this flag to the Command Line build runner’s script: go test -json
  • Add the env.GOFLAGS=-json parameter to the build configuration

go-build-step

In fact, since a lot of Go projects are actually using make to build the code and execute the tests, it will require changing the Makefile respectively:

The build feature is enabled and a -json argument is added to the go test command. We can now execute the build configuration. As a result, TeamCity will record data about the test execution status, execution time and present this data in the UI.

go-tests

For each individual test, it is possible to review its execution time from a historical perspective across different build agents where the test was executed.

Muting the failing tests

Assume that there are 248 tests in our project and 4 of those tests are failing. Meaning, the build does not succeed. However, we know that it is okay for those tests to fail and we would like to temporarily “mute” the specific test failures in the project. TeamCity provides a way to “mute” any of the currently failing tests so they will not affect the build status for future builds.

mute-tests

Muting test failures is a privilege of the project administrator. Select the individual failed tests in the build results and click the Investigate/Mute button that appears at the bottom of the screen. It is then possible to mute the tests either project-wide or just in the selected build configuration. The unmute policy can also be specified. For instance, once the test is fixed, it will automatically unmute.

build-with-muted-tests

But what does muting the test failure has to do with running Go tests? We use the go test command to execute the tests, and its exit code depends on the status of the execution. If there is even a single test failure, the exit code will be 1. The build will fail since the Command Line build step in TeamCity aims to respect the exit codes.

To mitigate this issue, we just have to make sure that the exit code of the last command in the Command Line runner (or a build script) is 0. For instance, by executing an exit 0 command in the build step (or in the Makefile). In this case, it will be possible to mute the failures and make the build configuration succeed. If the exit code is 0 but there are still test failures that are not muted, then TeamCity knows that it has to fail the build.

Summary

Support for Go is provided by TeamCity out of the box, there are no external plugins required. TeamCity parses results of go test command execution. The results are persisted and it is possible to review the figures in a historical perspective. Consequently, all the TeamCity features that are related to test reporting are now available for Go developers.

banner_blog@2x

About Anton Arhipov

Developer Advocate at JetBrains
This entry was posted in Blogroll, Features, How-To's and tagged , , , . Bookmark the permalink.

2 Responses to Building Go programs in TeamCity

  1. Eric Hubbard says:

    How do you deal with tests printing output that gets intermixed with the json produced by -json? For example if a test prints out something without ending in a \n and now you have a single line of output from go test that contains both json test results & unit test output..

    Maybe you regex each line looking for valid json documents that represent the test results..hmm..

    • Anton Arhipov says:

      Hi!

      The current implementation doesn’t use regexp to search for JSON in the output. The code checks for the ‘{‘ ‘}’ characters at the start and at the end of the line. So if the line matches then the contents will be parsed for the data.

      If there’s a print statement in your test without “\n” then we wouldn’t be able to locate a valid json. Golang devs recommend using t.Log / t. Error instead of print calls for logging the output in the tests. https://github.com/golang/go/issues/23036#issuecomment-355669573

Leave a Reply

Your email address will not be published. Required fields are marked *