testing-commands

Test Go/Cobra CLI commands with unit and end-to-end tests.

10|14|Updated Feb 26, 2026
One-click install
npx skills add https://github.com/openkaiden/kdn --skill testing-commands
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: testing-commands
Source: https://github.com/openkaiden/kdn/tree/main/.agents/skills/testing-commands
Command: npx skills add https://github.com/openkaiden/kdn --skill testing-commands

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill provides comprehensive testing patterns for CLI commands, including unit tests for preRun validation and end-to-end tests for full command execution.

Core Features & Use Cases

  • Unit Tests: Validate the preRun method directly to ensure argument and flag handling logic is correct.
  • E2E Tests: Verify the full command flow, Cobra wiring, and persistence of results in an end-to-end scenario.
  • Patterns & Best Practices: Demonstrates how to structure tests with t.Run subtests, temporary directories, and output capture for robust validation.

Quick Start

Create a unit test for preRun and an end-to-end test for full command execution to validate your CLI robustly.

Frequently Asked Questions about testing-commands

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

FAQPage Schema
How do I test Cobra CLI commands in Go?

Testing Cobra CLI commands involves unit tests for preRun validation and E2E tests using rootCmd.Execute to verify full command execution, Cobra wiring, and result persistence.

What is the best way to structure unit tests and E2E tests for a Golang CLI?

Structuring Golang CLI tests involves using t.Run subtests for organization, temporary directories for isolated file operations, and output capture to validate command behavior and errors robustly.

How do I validate preRun argument and flag handling logic in a Cobra application?

Validate preRun argument and flag handling by writing unit tests that call the preRun method directly, ensuring argument and flag logic is correct independently from full command execution.

Does this CLI testing approach support capturing output and validating errors?

Yes, this CLI testing approach supports output capture and error validation by demonstrating patterns that intercept command output and assert error conditions during E2E tests with rootCmd.Execute.

When should I use E2E tests instead of unit tests for CLI command validation?

Use E2E tests instead of unit tests when you need to verify full command flow, Cobra wiring, and result persistence; use unit tests for isolated preRun argument and flag validation.

Can I test full command execution and result persistence with Cobra rootCmd.Execute?

Yes, you can test full command execution and result persistence by running E2E tests with rootCmd.Execute, verifying the entire command flow from Cobra wiring to final result persistence.