O'Reilly logo
live online training icon Live Online training

Testing Go Projects

Techniques for testing real-world Go applications from the ground up

Topic: Programming
Benjamin Muschko

Implementing test code for Go projects and its continuous execution is an integral activity to ensure that the software is ready for prime time. As developers, we often claim that certain pieces of software are untestable. It either has dependencies on actual services like a database or an HTTP API, requires extensive setups, or simply isn’t structured properly to set yourself up for success.

Testing doesn’t have to be hard. In this training, you’ll learn the ropes of writing and executing tests with the standard testing package. Moreover, you’ll apply proven testing techniques to verify hard-to-test code regularly encountered in real-world projects.

What you'll learn-and how you can apply it

By the end of this live, hands-on, online course, you’ll understand:

  • How to implement and execute tests for Go projects
  • How to apply proven testing techniques
  • How to tackle real-world testing scenarios

And you’ll be able to:

  • Verify the correctness of your code with sensible assertions
  • Write effective test code even for complex, hard-to-test use cases
  • Drive tests from the command line and an IDE
  • Use testing to improve maintainability, achieve high cohesion and reduce strong coupling of your production source code

This training course is for you because...

  • You are a Go developer new to writing tests and want to learn the basics
  • You are a Go developer familiar with the standard testing package but want to learn how to implement more advanced testing scenarios

Prerequisites

  • Basic understanding of Go language concepts and syntax
  • Exposure to test concepts and test scenarios
  • Knowledge of a Go test framework is not required
  • Installation of Go runtime 13.x or higher
  • Installation of an IDE, optimally GoLand or VSCode + Go plugin

Recommended preparation:

Recommended follow-up:

About your instructor

  • Benjamin Muschko is a software engineer, consultant, and trainer with over 15 years of experience in the industry. He’s passionate about project automation, testing, and continuous delivery. Ben is the author of Gradle in Action (Manning). He’s a frequent speaker at conferences and an avid open source advocate. He holds the CKAD and CJE certifications.

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

Why Testing? (15 minutes)

  • Presentation: Introduction to the Course (5 mins)
  • Poll (5 mins)
  • Presentation: On the Importance of Testing (5 mins)

Testing Basics (70 minutes)

  • Presentation: Naming conventions and Code Organization (10 mins)
  • Presentation: Writing Meaningful Assertions (5 mins)
  • Presentation: Signaling Helpful Failure States (5 mins)
  • Presentation: Running Tests (5 mins)
  • Exercise: Implementation and Execution of a Test Case (10 mins)
  • Discussion: Trade-offs Between Testing Exported and Internal Functions (5 mins)
  • Break (5 mins)
  • Presentation: Generating Code Coverage Metrics (5 mins)
  • Presentation: Reporting of Code Coverage (5 mins)
  • Exercise: Producing an HTML Report for Code Coverage Metrics (10 mins)
  • Q&A (5 mins)

Advanced Techniques (85 minutes)

  • Presentation: Managing Test Fixtures (5 mins)
  • Exercise: Setting up and Tearing Down Test Fixtures (10 mins)
  • Presentation: Implementing and Organizing Repetitive Test Code (5 mins)
  • Exercise: Implementing a Test Helper (10 mins)
  • Break (5 mins)
  • Presentation: Running Through Test Case Permutations (5 mins)
  • Exercise: Implementing a Table-Driven Test (10 mins)
  • Presentation: Using Mock Objects (5 mins)
  • Exercise: Testing in Isolation by Mocking an Interface (10 mins)
  • Presentation: Separating and Executing Different Types of Tests (5 mins)
  • Exercise: Differentiating Between Unit and Integration Tests (10 mins)
  • Q&A (5 mins)
  • Break (5 mins)

Testing Real-World Scenarios (65 minutes)

  • Presentation: Comparing Complex Output with Golden Files (10 mins)
  • Exercise: Using a Golden File for JSON Data (10 mins)
  • Presentation: Testing HTTP endpoints (10 mins)
  • Exercise: Avoiding Network Calls and Mocking the Response (10 mins)
  • Presentation: Testing Behavior of CLI Applications (10 mins)
  • Exercise: Testing a Cobra CLI Command (10 mins)
  • Q&A (5 mins)