Chapter 8. Testing your application

 

This chapter covers

  • The Go testing libraries
  • Unit testing
  • HTTP testing
  • Testing with dependency injection
  • Using third-party testing libraries

Testing is one of the main critical activities in programming, but often it’s neglected or left as an afterthought. Go provides basic testing capabilities that look surprisingly primitive, but as you’ll learn in this chapter, Go supplies the tools to create the automated tests you need as a programmer. This chapter also covers the check and Ginkgo packages, popular Go testing libraries that extend the built-in testing package.

As with the web application programming libraries we’ve explored in the previous few chapters, Go provides only the fundamental tools. As a programmer you’ll need to build on them to deliver the kind of tests that you need.

8.1. Go and testing

Go offers a few testing-focused libraries in the standard library. The main test library is the testing package, which contains most of the functions you’ll see in this chapter. Another library that’s interesting for web application programming is the net/http/httptest package. The httptest package, as the name suggests, is a library for testing web applications; it is based on the testing package.

Let’s look at the main testing package first. It’s important to start here because this package provides basic automated testing capabilities in Go. Once you understand the testing package, the httptest package will make a whole lot more sense.

8.2. Unit testing with Go

8.3. HTTP testing with Go

8.4. Test doubles and dependency injection

8.5. Third-party Go testing libraries

8.6. Summary