Unit Testing Code with Hard-To-Mock Dependencies

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.

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.

Video

Stay connected

You can subscribe to our newsletter to stay updated on the latest news and announcements. Follow us on social media to join our community and engage with other attendees, speakers, and staff. We're on Twitter, Mastodon, Youtube and Facebook. Don't forget to use our official hashtag #GoLab2023 to share your experience and connect with other attendees on social media.

Subscribe to our newsletter

We hate spam just as much as you do, which is why we promise to only send you relevant communications. We respect your privacy and will never share your information with third parties.
©2023 GoLab | The international conference on Go in Florence-Design & devCantiere Creativo-Made withDatoCMS