Abstract
Unit testing using mocks is a common technique in Go and other languages. However, some dependencies are harder to mock than others; standard library packages such os can be particularly tricky. This talk covers strategies for effectively testing code that depends on such hard-to-mock packages. Mocking dependencies is a common unit testing technique. Mocks are code that can be injected in place of real dependencies, allowing a developer to control the behavior of these dependencies from a test. Mocks may be generated by a tool or written by hand, but either way they make it easy to exercise error conditions and isolate tests from external software and systems. In Go, it’s easiest to use mocks for dependencies that export interfaces. A mock object conforming to some interface can be built or generated and injected for tests, while a real object conforming to the same interface is used in production. However, not all packages provide nice mockable interfaces. In particular, low-level packages such as the os package in the Go standard library tend to export functions (as opposed to structs with methods) and structs with data members (as opposed to accessor functions). These properties make it difficult to build mocks for testing code that depends on such packages.