unit-testing-testify

Create and maintain Go unit tests with testify table-driven tests and mocks.

Updated Jul 7, 2024
One-click install
npx skills add https://github.com/manorfm/toToggles --skill unit-testing-testify
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: unit-testing-testify
Source: https://github.com/manorfm/toToggles/tree/main/server/.agents/skills/unit-testing-testify
Command: npx skills add https://github.com/manorfm/toToggles --skill unit-testing-testify

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Este Skill facilita a criação e a estruturação de testes unitários em Go, promovendo práticas consistentes com o uso do pacote testify, aumentando a confiabilidade do código e reduzindo retrabalho.

Core Features & Use Cases

  • Utilize testify: Use o pacote assert do github.com/stretchr/testify/assert para asserções mais legíveis e concisas.
  • Nomenclatura: Coloque seus testes no arquivo adjunto ex: toggle_usecase_test.go para o arquivo toggle_usecase.go.
  • Estrutura Table-Driven: Para métodos com múltiplos caminhos (Sucesso vs. Falhas), organize usando Table-Driven Tests ([]struct{...}).
  • Mocks: Sempre utilize mocks (testify/mock) para isolar camadas. Exemplo: para testar um Usecase, implemente seu Repository como Mock para simular respostas.

Quick Start

Escreva testes unitários table-driven para um usecase específico usando testify e mocks.

Frequently Asked Questions about unit-testing-testify

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

FAQPage Schema
How do I structure Go unit tests using table-driven tests?

Table-driven tests in Go organize multiple test cases into a slice of structs, typically []struct{...}, to execute varying input paths like success and failure scenarios. This structure ensures comprehensive coverage and simplifies maintaining complex test logic.

What is the best way to isolate layers when writing Go unit tests?

To isolate layers in Go unit tests, implement mocks using testify/mock to simulate dependencies. For example, when testing a Usecase, mock its Repository interface to control simulated responses and ensure the test strictly evaluates the target logic.

How does testify improve Go unit testing compared to standard assertions?

Testify improves Go unit testing by providing the assert package, which allows developers to write more readable and concise assertions. It enforces standard testing practices and reduces boilerplate code compared to native error checking.

What file naming conventions should I use for Go test files?

Go test files should use adjacent naming conventions, such as naming the test file toggle_usecase_test.go for a source file named toggle_usecase.go. This ensures consistent testing and easy navigation across Go projects.

Do I need testify to run table-driven tests in Go?

You do not need testify to run table-driven tests in Go, as the structure relies on native slices and loops. However, combining table-driven structures with testify assert helpers yields more readable and concise assertion outputs.