Skip to main content

Gomega

Gomega

Expressive matcher/assertion library for Go

Gomega is a matcher and assertion library for Go, commonly used with Ginkgo for BDD-style testing in the Control Plane.

Expressive assertions

Gomega provides a fluent, expressive syntax for making assertions in tests, making tests both more readable and more maintainable.

Overview

Gomega enhances Go tests with readable assertions and powerful matching capabilities. While it's designed to work seamlessly with Ginkgo, it can also be used with Go's standard testing package.

Gomega Logo

Why Gomega?

🔍 Expressive Matchers

Clear, readable syntax for assertions with detailed failure messages.

⏱️ Asynchronous Testing

Built-in support for testing asynchronous behavior with Eventually and Consistently.

🔄 Custom Matchers

Ability to create custom matchers for domain-specific assertions.

🧩 Combinable Matchers

Combine matchers with And, Or, and Not for complex assertions.

🌐 Collection Matchers

Special matchers for arrays, slices, maps, and channels.

⚙️ Error Handling

Specific matchers for error handling and propagation.

Integration in the Control Plane

Consistent assertions

Gomega provides a consistent way to express expectations across the Control Plane codebase, making tests more readable and maintainable.

The Control Plane uses Gomega for assertions in various types of tests:

Unit Tests

Basic assertions for functions and methods:

  • Verifying return values
  • Checking error conditions
  • Validating data transformations
  • Testing boundary conditions

Controller Tests

Assertions for controller behavior:

  • Resource creation and updates
  • Status field changes
  • Reconciliation results
  • Error handling and recovery

Configuration

Matcher types and options

Gomega provides different types of matchers for various testing scenarios, with options to customize behavior.

Common Matchers

Equality Matchers

Equal(expected): Deep equality
BeIdenticalTo(expected): Pointer identity
BeEquivalentTo(expected): Type-converting equality

Comparison Matchers

BeNumerically(">", 5): Numeric comparison
BeLessThan(10): Less than
BeGreaterThan(5): Greater than

Boolean Matchers

BeTrue(): Is true
BeFalse(): Is false
BeTruthy(): Evaluates to true

Asynchronous Testing

Testing async behavior

Gomega's Eventually and Consistently functions are particularly useful for testing controllers in the Control Plane, which often involve asynchronous reconciliation loops.

Best Practices in the Control Plane

📝 Descriptive Assertions

Write assertions that clearly convey intent and provide meaningful failure messages.

🔄 Eventually with Timeouts

Always specify reasonable timeouts for Eventually assertions to prevent long-running tests.

🧩 Custom Domain Matchers

Create custom matchers for domain-specific concepts to make tests more readable.

♻️ Reuse Complex Assertions

Extract complex assertions into helper functions for reuse and clarity.

⚠️ Avoid Over-Specificity

Focus on testing behavior rather than implementation details to create more maintainable tests.

🔍 Targeted Assertions

Test one concept per assertion for clearer failure messages and better test diagnostics.

Ginkgo

BDD-style testing framework that pairs perfectly with Gomega.

Testify

Another testing toolkit used in the Control Plane.

Go-Snaps

Snapshot testing library for Go applications.