Go’s testing support has two parts: libraries and tooling. The testing package in the standard library provides the types and functions to write tests, while the go test tool…
- 05.01Understanding the Basics of TestingGo’s testing support has two parts: libraries and tooling. The testing package in the standard library provides the types and functions to write tests, while the go test tool…
- 05.02Reporting Test FailuresThere are several methods on testing.T for reporting test failures. You’ve already seen Error, which builds a failure description string out of a comma-separated list of values.
- 05.03LoggingIt is often helpful to output additional information when running tests. Go provides access to the testing logger via a set of methods on testing.T. While you can use…
- 05.04Saving Test ArtifactsIt is sometimes useful to capture output files from a test (for additional analysis, perhaps). Instead of hard-coding a location into your test code, you can take advantage of…
- 05.05Setting Up and Tearing DownSometimes you have some common state that you want to set up before any tests run and remove when testing is complete. Use a TestMain function to manage this state and run your…
- 05.06Cleanup, Context, and HelperThe Cleanup method on testing.T is used to clean up temporary resources created for a single test. This method has a single parameter: a function with no input parameters or…
- 05.07Working with Temporary DirectoriesIf your test uses temporary files, use the t.TempDir method. It creates a new temporary directory every time it is invoked and returns the full path of the directory. It also…
- 05.08Testing with Environment VariablesIt’s a common (and very good) practice to configure applications with environment variables. To simplify writing tests for code that depends on environment variables, Go…
- 05.09Storing Sample Test DataAs go test walks your source code tree, it uses the current package directory as the current working directory. If you want to use sample data to test functions in a package,…
- 05.10Caching Test ResultsAs you learned in the related discussion, Go caches compiled packages if they haven’t changed. Go also caches test results when running tests across multiple packages if they…
- 05.11Testing Your Public APIThe tests that you’ve written are in the same package as the production code. This allows you to test both exported and unexported functions.
- 05.12Tests and the ContextWhen writing tests for functions that expect a context, don’t use context.Background to generate a context. Instead, use t.Context. This returns a context that is automatically…
- 05.13Using go-cmp to Compare Test ResultsWriting a thorough comparison of a compound type’s two instances can be verbose. While you can use reflect.DeepEqual to compare structs, maps, and slices, there’s a better way.…
- 05.14Running Table TestsMost of the time, it takes more than a single test case to validate that a function is working correctly. You could write multiple test functions to validate your function or…
- 05.15Running Tests ConcurrentlyBy default, unit tests are run sequentially. Since each unit test is supposed to be independent from every other unit test, they make ideal candidates for concurrency. To make…
- 05.16Code Review: Basic TestingTake advantage of Go’s built-in testing framework to write idiomatic tests.
- 05.17Practice: Basic TestingThese exercises help you practice the basics of Go testing.
- 05.18Checking Your Code CoverageCode coverage is a very useful tool for knowing whether you’ve missed any obvious cases. However, reaching 100% code coverage doesn’t guarantee that there aren’t bugs in your…
- 05.19FuzzingOne of the most important lessons that every developer eventually learns is that all data is suspect. No matter how well specified a data format is, you will eventually have to…
- 05.20Using BenchmarksDetermining how fast (or slow) code runs is surprisingly difficult. Rather than trying to figure it out yourself, use the benchmarking support that’s built into Go’s testing…
- 05.21Finding Concurrent Data Access Problems with the Data Race DetectorEven with Go’s built-in support for concurrency, bugs still happen. It’s easy to accidentally reference a variable from two different goroutines without acquiring a lock. The…
- 05.22Test Concurrent Code with synctestYou might be surprised to discover that a language with strong support for both testing and concurrency did not initially include strong support for testing concurrency. The…
- 05.23Code Review: Advanced TestingCode coverage is necessary, but not sufficient. Even code with 100% code coverage can contain bugs. Make sure that tests include multiple sets of data for the same paths…
- 05.24Practice: Advanced TestingNow that you’ve seen the more advanced testing tools provided by Go, these exercises will give you a chance to try them out.
- 05.25Using Stubs in GoSo far, you’ve written tests for functions that didn’t depend on other code. This is not typical, as most code is filled with dependencies. As you saw in the related…
- 05.26Using Integration Tests and Build TagsEven though httptest provides a way to avoid testing against external services, you should still write integration tests, automated tests that connect to other services. These…
- 05.27Code Review: Tests with Mocks and StubsMake sure your code interacts with other components via abstractions. They make code easier to understand and easier to test.
- 05.28Practice: Tests with Mocks and StubsNow that you’ve learned about using mocks and stubs, complete these exercises to apply that knowledge to a sample application.
- 05.29Recap: Writing TestsIn this topic, you’ve learned how to write tests and improve code quality by using Go’s built-in support for testing, code coverage, benchmarking, fuzzing, and data race…