notexOPENOverview
Notex/Languages/Writing Tests

Writing Tests

Always free to read

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

GO / CORE LANGUAGE

Writing Tests

29 LESSONS · 74 MIN
  1. 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…READING3 MIN
  2. 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.READING1 MIN
  3. 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…READING1 MIN
  4. 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…READING1 MIN
  5. 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…READING2 MIN
  6. 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…READING2 MIN
  7. 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…READING1 MIN
  8. 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…READING1 MIN
  9. 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,…READING1 MIN
  10. 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…READING1 MIN
  11. 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.READING2 MIN
  12. 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…READING1 MIN
  13. 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.…READING2 MIN
  14. 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…READING3 MIN
  15. 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…READING2 MIN
  16. 05.16Code Review: Basic TestingTake advantage of Go’s built-in testing framework to write idiomatic tests.PRACTICE1 MIN
  17. 05.17Practice: Basic TestingThese exercises help you practice the basics of Go testing.PRACTICE1 MIN
  18. 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…READING3 MIN
  19. 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…READING10 MIN
  20. 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…READING9 MIN
  21. 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…READING2 MIN
  22. 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…READING7 MIN
  23. 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…PRACTICE1 MIN
  24. 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.PRACTICE1 MIN
  25. 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…READING9 MIN
  26. 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…READING3 MIN
  27. 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.PRACTICE1 MIN
  28. 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.PRACTICE1 MIN
  29. 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…READING1 MIN
KEEP EXPLORING

One note leads to another.

Back to the library →
⌕
Search across the entire library ⌘ K