Executing Integration Tests

Powerful Command-Line Applications in Go — by Ricardo Gerardi (94 / 127)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Completing and Deleting Items | TOC | Exercises 👉

As you learned in Testing the Client Without Connecting to the API, executing the unit tests locally allows you to execute these tests frequently without touching the actual REST API. This is particularly useful when you’re working on a disconnected environment or when using third-party APIs. It also provides a controlled and repeatable environment where you can test different conditions that may not be possible with the actual API, such as error conditions.

This approach has a downside. If you missed a detail about the API or if the API changed, the application may not work properly, but the local tests will make you think it does. To overcome this challenge, you’ll run an integration test that connects to the actual API as the last step in your testing process. To ensure that this test doesn’t run all the time, you’ll use a Go build constraint. You’ll learn more about build constraints in Chapter 11, Distributing Your Tool, so, for now, think of a build constraint as a condition that defines whether to include a file when building or testing the application.[52]

For this example, you’ll create a new test file cmd/integration_test.go with the build constraint integration. This prevents Go from selecting this file and consequently running this test unless you explicitly use the parameter -tags integration when…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.