Test-Driven Development in Go

Book description

Explore Go testing techniques and leverage TDD to deliver and maintain microservices architecture, including contract, end-to-end, and unit testing Purchase of the print or Kindle book includes a free PDF eBook

Key Features

  • Write Go test suites using popular mocking and testing frameworks
  • Leverage TDD to implement testing at all levels of web applications and microservices architecture
  • Master the art of writing tests that cover edge cases and concurrent code

Book Description

Experienced developers understand the importance of designing a comprehensive testing strategy to ensure efficient shipping and maintaining services in production. This book shows you how to utilize test-driven development (TDD), a widely adopted industry practice, for testing your Go apps at different levels. You’ll also explore challenges faced in testing concurrent code, and learn how to leverage generics and write fuzz tests.

The book begins by teaching you how to use TDD to tackle various problems, from simple mathematical functions to web apps. You’ll then learn how to structure and run your unit tests using Go’s standard testing library, and explore two popular testing frameworks, Testify and Ginkgo. You’ll also implement test suites using table-driven testing, a popular Go technique. As you advance, you’ll write and run behavior-driven development (BDD) tests using Ginkgo and Godog. Finally, you’ll explore the tricky aspects of implementing and testing TDD in production, such as refactoring your code and testing microservices architecture with contract testing implemented with Pact. All these techniques will be demonstrated using an example REST API, as well as smaller bespoke code examples.

By the end of this book, you’ll have learned how to design and implement a comprehensive testing strategy for your Go applications and microservices architecture.

What you will learn

  • Create practical Go unit tests using mocks and assertions with Testify
  • Build table-driven test suites for HTTP web applications
  • Write BDD-style tests using the Ginkgo testing framework
  • Use the Godog testing framework to reliably test web applications
  • Verify microservices architecture using Pact contract testing
  • Develop tests that cover edge cases using property testing and fuzzing

Who this book is for

If you are an intermediate-level developer or software testing professional who knows Go fundamentals and is looking to deliver projects with Go, then this book is for you. Knowledge of Go syntax, structs, functions, and interfaces will help you get the most out of this book.

Table of contents

  1. Test-Driven Development in Go
  2. Foreword
  3. Contributors
  4. About the author
  5. About the reviewer
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
    9. Download a free PDF copy of this book
  7. Part 1: The Big Picture
  8. Chapter 1: Getting to Grips with Test-Driven Development
    1. Exploring the world of TDD
      1. Introduction to the Agile methodology
      2. Types of automated tests
      3. The iterative approach of TDD
      4. TDD best practices
    2. Understanding the benefits and use of TDD
      1. Pros and cons of using TDD
      2. Use case – the simple terminal calculator
    3. Alternatives to TDD
      1. Waterfall testing
      2. Acceptance Test-Driven Development
    4. Understanding test metrics
      1. Important test metrics
      2. Code coverage
    5. Summary
    6. Questions
    7. Further reading
    8. Answers
  9. Chapter 2: Unit Testing Essentials
    1. Technical requirements
    2. The unit under test
      1. Modules and packages
      2. The power of Go packages
      3. Test file naming and placement
      4. Additional test packages
    3. Working with the testing package
      1. The testing package
      2. Test signatures
      3. Running tests
      4. Writing tests
      5. Use case – implementing the calculator engine
    4. Test setup and teardown
      1. The TestMain approach
      2. init functions
      3. Deferred functions
    5. Operating with subtests
      1. Implementing subtests
      2. Code coverage
    6. The difference between a test and a benchmark
    7. Summary
    8. Questions
    9. Further reading
  10. Chapter 3: Mocking and Assertion Frameworks
    1. Technical requirements
    2. Interfaces as dependencies
      1. Dependency injection
      2. Implementing dependency injection
      3. Use case – continued implementation of the calculator
    3. Exploring mocks
      1. Mocking frameworks
      2. Generating mocks
      3. Verifying mocks
    4. Working with assertion frameworks
      1. Using testify
      2. Asserting errors
    5. Writing testable code
    6. Summary
    7. Questions
    8. Further reading
  11. Chapter 4: Building Efficient Test Suites
    1. Technical requirements
    2. Testing multiple conditions
      1. Identifying edge cases
      2. External services
      3. Error-handling refresher
    3. Table-driven testing in action
      1. Step 1 – declaring the function signature
      2. Step 2 – declaring a structure for our test case
      3. Step 3 – creating our test-case collection
      4. Step 4 – executing each test
      5. Step 5 – implementing the test assertions
      6. Step 6 – running the failing test
      7. Step 7 – implementing the base cases
      8. Step 8 – expanding the test case collection
      9. Step 9 – expanding functional code
      10. Parallelization
      11. Advantages and disadvantages of table-driven testing
    4. Use case – the BookSwap application
      1. Testing BookService
    5. Summary
    6. Questions
    7. Further reading
  12. Part 2: Integration and End-to-End Testing with TDD
  13. Chapter 5: Performing Integration Testing
    1. Technical requirements
    2. Supplementing unit tests with integration tests
      1. Limitations of unit testing
      2. Implementing integration tests
      3. Running integration tests
    3. Behavior-driven testing
      1. Fundamentals of BDD
      2. Implementing BDD tests with Ginkgo
    4. Understanding database testing
      1. Useful libraries
    5. Spinning up and tearing down environments with Docker
      1. Fundamentals of Docker
      2. Using Docker
    6. Summary
    7. Questions
    8. Further reading
  14. Chapter 6: End-to-End Testing the BookSwap Web Application
    1. Technical requirements
    2. Use case – extending the BookSwap application
      1. User journeys
      2. Using Docker
      3. Persistent storage
      4. Running the BookSwap application
    3. Exploring Godog
    4. Implementing tests with Godog
      1. Creating test files
      2. Implementing test steps
      3. Running the test suite
    5. Using database assertions
      1. Seed data
      2. Test cases and assertions
    6. Summary
    7. Questions
    8. Further reading
  15. Chapter 7: Refactoring in Go
    1. Technical requirements
    2. Understanding changing dependencies
      1. Code refactoring steps and techniques
      2. Technical debt
      3. Changing dependencies
    3. Relying on your tests
      1. Automated refactoring
      2. Validating refactored code
    4. Error verification
      1. Custom error types
    5. Splitting up the monolith
      1. Key refactoring considerations
    6. Summary
    7. Questions
    8. Further reading
  16. Chapter 8: Testing Microservice Architectures
    1. Technical requirements
    2. Functional and non-functional testing
      1. Performance testing in Go
      2. Implementing performance tests
    3. Contract testing
      1. Fundamentals of contract testing
      2. Using Pact
    4. Breaking up the BookSwap monolith
    5. Production best practices
      1. Monitoring and observability
      2. Deployment patterns
      3. The circuit breaker pattern
    6. Summary
    7. Questions
    8. Further reading
  17. Part 3: Advanced Testing Techniques
  18. Chapter 9: Challenges of Testing Concurrent Code
    1. Technical requirements
    2. Concurrency mechanisms in Go
      1. Goroutines
      2. Channels
    3. Applied concurrency examples
      1. Closing once
      2. Thread-safe data structures
      3. Waiting for completion
    4. Issues with concurrency
      1. Data races
      2. Deadlocks
      3. Buffered channels
    5. The Go race detector
      1. Untestable conditions
    6. Use case – testing concurrency in the BookSwap application
    7. Summary
    8. Questions
    9. Further reading
  19. Chapter 10: Testing Edge Cases
    1. Technical requirements
    2. Code robustness
      1. Best practices
    3. Usages of fuzzing
      1. Fuzz testing in Go
    4. Property-based testing
    5. Use case – edge cases of the BookSwap application
    6. Summary
    7. Questions
    8. Further reading
  20. Chapter 11: Working with Generics
    1. Technical requirements
    2. Writing generic code in Go
      1. Generics in Go
      2. Exploring type constraints
    3. Table-driven testing revisited
      1. Step 1 – defining generic test cases
      2. Step 2 – creating test cases
      3. Step 3 – implementing a generic test run function
      4. Step 4 – putting everything together
      5. Step 5 – running the test
      6. Test utilities
    4. Extending the BookSwap application with generics
    5. Testing best practices
      1. Development best practices
      2. Testing best practices
      3. Culture best practices
    6. Summary
    7. Questions
    8. Further reading
  21. Assessments
    1. Chapter 1, Getting to Grips with Test-Driven Development
    2. Chapter 2, Unit Testing Essentials
    3. Chapter 3, Mocking and Assertion Frameworks
    4. Chapter 4, Building Efficient Test Suites
    5. Chapter 5, Performing Integration Testing
    6. Chapter 6, End-to-End Testing the BookSwap Web Application
    7. Chapter 7, Refactoring in Go
    8. Chapter 8, Testing Microservice Architectures
    9. Chapter 9, Challenges of Testing Concurrent Code
    10. Chapter 10, Testing Edge Cases
    11. Chapter 11, Working with Generics
  22. Index
    1. Why subscribe?
  23. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Test-Driven Development in Go
  • Author(s): Adelina Simion
  • Release date: April 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781803247878