0Pricing
Vibe Coding · Lección

Generar pruebas mediante un prompt

Pida a la IA que escriba pruebas útiles

Generar pruebas mediante un prompt es una lección gratuita de Vibe Coding en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Vibe Coding, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Vibe Coding incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

Tests Are Your Safety Net

When AI writes most of your code, tests become the contract that keeps it honest. Each assertion is a promise the assistant cannot silently break.

The goal is not 100 percent coverage for its own sake. It is to pin the behaviors you care about so that future generations cannot regress them.

This lesson is about prompting your way to a test suite worth trusting.

Describe Behavior, Not Implementation

Weak prompts ask the model to "write tests for this function." The result mirrors the implementation and passes trivially, even when the code is wrong.

Strong prompts describe the expected behavior in your own words. The test then encodes intent, not the model's reading of the existing code.

Specify inputs, outputs, and invariants the way a product spec would.

Write tests for the checkout function based on these rules, not on the current implementation: total must equal sum of line items minus discount; discount can never exceed subtotal; an empty cart must throw. Cover the boundary at exactly zero and at the discount cap.

Start With the Failing Case

Tests that only confirm the happy path give false comfort. Ask for the cases most likely to break first: nulls, empties, limits, and malformed input.

A suite that exercises failure modes is the one that catches real regressions. The boring success case rarely surprises anyone.

Front-load the edges and you front-load the bugs.

Generate tests that focus on failure and boundary conditions for this parser: empty string, whitespace only, max length plus one, invalid unicode, and deeply nested input. For each, assert the exact error type and message the code should produce.

Beware Tests That Pin Bugs

If you ask a model to write tests against existing code, it may assert the current buggy behavior as if it were correct. The suite goes green while the bug stays.

This is the single biggest trap in AI test generation. The test documents the defect instead of catching it.

Always review whether each assertion reflects what you want, not merely what the code currently does.

For each test you generated, tell me explicitly whether the expected value is derived from my stated requirements or from observing the current code's output. Flag any assertion that simply mirrors existing behavior so I can verify it is actually correct.

The Arrange-Act-Assert Spine

Readable tests follow a clear shape: set up state, perform one action, assert one outcome. Models drift into multi-assertion mega-tests that are hard to debug when they fail.

Ask for focused tests with descriptive names that read like specifications.

A failing test should tell you exactly which behavior broke, in its name alone.

Refactor these tests so each one follows arrange-act-assert, tests a single behavior, and has a descriptive name that reads like a sentence describing the expected outcome. Split any test that contains unrelated assertions.

Mocking the Right Boundaries

AI often mocks too much, stubbing out the very logic under test until the test asserts nothing real. Or it mocks too little and hits a live database.

Mock at the system boundary: network, clock, filesystem, payment gateway. Keep your own business logic real.

State the boundary explicitly so the model does not guess.

Write these tests so that only external boundaries are mocked: the HTTP client, the system clock, and the payment provider. Do NOT mock our own pricing or validation logic; that logic must run for real so the test exercises it.

Property-Based Thinking

Example tests check specific inputs; property tests check invariants across many generated inputs. "Reversing a list twice returns the original" holds for every list.

Asking the model for property-based tests surfaces bugs that hand-picked examples miss, because the framework hunts for counterexamples.

Use them where invariants are clear: encoders, sorters, serializers, math.

Write property-based tests for the serializer using a generative testing library. Assert the round-trip invariant that decode(encode(x)) equals x for any valid input, and let the framework search for the smallest failing case.

Coverage Without Theater

Coverage percentage is a map, not the territory. You can hit every line while asserting nothing meaningful. Models love padding suites to chase a number.

Use coverage to find untested branches, then judge each by hand: is this path worth pinning?

Aim for tests that would fail if the behavior were wrong, not tests that merely execute the code.

Run coverage and list the uncovered branches in this module. For each one, tell me whether it represents a meaningful behavior worth testing or trivial code I can ignore, and propose a focused test only for the meaningful ones.

Mutation: Testing Your Tests

A green suite that survives any code change is worthless. Mutation testing introduces small bugs and checks whether your tests notice.

If a mutant survives, your tests do not actually constrain that behavior. The model can propose mutations and you verify your suite kills them.

This is how you measure test quality, not just quantity.

Propose five small mutations to this function, such as flipping a comparison or removing a guard, that would represent real bugs. For each mutation, tell me whether the current test suite would catch it, and write a new test for any mutation that survives.

Make Tests Deterministic

Flaky tests erode trust until people ignore failures entirely. The usual culprits are real time, random seeds, network calls, and test ordering.

Demand deterministic tests: inject the clock, fix the seed, isolate state between runs. A suite you cannot trust is worse than no suite.

Reliability is a feature of the tests themselves.

Make this test suite fully deterministic: inject a fixed clock instead of reading current time, seed all randomness, remove any reliance on test execution order, and ensure each test resets shared state in setup and teardown.

Tests As Living Spec

A good suite doubles as documentation. New contributors, human or AI, learn the system's rules by reading the assertions.

Keep test names and structure clear enough that the suite explains intended behavior on its own. The model reads them too, and writes better code as a result.

Your tests become the durable memory the model lacks.

Quick Check

Test your grasp of prompt-driven test generation.

Recap

Prompt tests from behavior and requirements, not from the implementation, so they catch bugs instead of pinning them. Front-load failure and boundary cases, mock only at boundaries, and use property and mutation testing to measure real strength.

Keep the suite deterministic and readable so it serves as a living spec. Next, you will hunt for security gaps the model left behind.

Preguntas frecuentes

¿La lección «Generar pruebas mediante un prompt» es gratis?

Sí — el texto completo de «Generar pruebas mediante un prompt» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Vibe Coding, actualiza a CoddyKit PRO. El curso de Vibe Coding incluye 4 lecciones en total.

¿Qué aprenderé en «Generar pruebas mediante un prompt»?

Pida a la IA que escriba pruebas útiles Practicas Vibe Coding con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Vibe Coding?

No se requiere experiencia previa. Vibe Coding en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.

¿Cuánto tiempo toma la lección «Generar pruebas mediante un prompt»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Vibe Coding?

Sí. Cada lección de Vibe Coding incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Por qué el código generado por IA necesita revisión
  2. Generar pruebas mediante un prompt
  3. Detectar brechas de seguridad
  4. Preparar para producción
← Volver a Vibe Coding