Testing

Provides standards for unit testing, table-driven tests, and mocking in Golang.

51|6|Updated Mar 28, 2019
One-click install
npx skills add https://github.com/Mte90/dotfiles --skill testing-mte90
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Testing
Source: https://github.com/Mte90/dotfiles/tree/main/.config/opencode/skills/golang/testing
Command: npx skills add https://github.com/Mte90/dotfiles --skill testing-mte90

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides clear guidelines and best practices for writing effective unit tests, implementing table-driven tests, and utilizing mocking strategies in Golang projects.

Core Features & Use Cases

  • Test-Driven Development (TDD): Guides users through the Red-Green-Refactor cycle.
  • Table-Driven Tests: Offers a template and explanation for creating robust, data-driven tests.
  • Mocking: Details strategies for mocking dependencies using libraries like Testify and Mockery.
  • Use Case: A developer needs to ensure their new Golang function is thoroughly tested. They can refer to this Skill for the correct structure of test files, how to write table-driven tests for various inputs, and how to mock external services the function depends on.

Quick Start

Follow the TDD workflow by writing a failing table-driven test case first.

Frequently Asked Questions about Testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write table-driven tests in Golang?

Table-driven tests in Golang use a struct slice to define test cases and golden snippets, iterating through them to assert inputs against expected outputs. This approach provides robust, data-driven coverage for various function inputs.

What is the TDD workflow for Go test functions?

The TDD workflow for Go test functions follows the Red-Green-Refactor cycle: write a failing test first, implement the minimum code to pass it, then refactor. This ensures your Golang functions are thoroughly tested from the start.

Testify vs GoMock: which mocking library should I use for Golang unit tests?

Testify and GoMock are both viable mocking libraries for Golang unit tests. Testify offers assertion extensions, while Mockery and GoMock generate mocks for dependencies, allowing you to stub external services effectively during testing.

What are common anti-patterns when writing unit tests in Go?

Common anti-patterns in Go unit tests include sleeping to wait for conditions and improper test file naming. Adhering to established naming conventions and avoiding sleep calls ensures your tests remain reliable and maintainable.

Do I need external dependencies to run stdlib testing in Golang?

No external dependencies are required for stdlib testing in Golang. The standard library provides core testing capabilities, while tools like Testify, Mockery, and GoMock are optional additions for mocking and assertions.