Skip to content
PromptPressPromptPress
  • Home
  • News
    • Hero – No Sidebar
    • Hero – Right Sidebar
    • Hero – Left Sidebar
    • Classic – Right Sidebar
    • Classic – No Sidebar
    • Classic – Left Sidebar
    • Gallery Post
    • Audio Post
    • Video Post
  • Pages
    • About Us
    • Contact Us
    • Our Team
    • FAQs
    • Prompt Library
    • Full With
    • Error 404
  • Contact Us
  • Prompt Library
  • Download

Writing Unit Tests with AI: A Prompt Pattern That Holds Up

Author: Dannci• July 21, 2026• Coding with AI

AI-generated tests often just confirm what the code already does. This prompt pattern asks for edge cases first, and in our example it found a real bug before a single test ran.

AI is fast at writing tests, and that is exactly the problem. Left alone, it writes tests that confirm what the code already does, bugs included. The test-writing prompts in our code collection use a different order: edge cases first, then tests.

Below is the pattern, with a small PHP example where the edge-case step found a real bug before any test was written. If you haven’t set up a context brief yet, do that first. Tests need to match your framework and conventions.

The pattern

You are writing PHPUnit tests for the function below.

Step 1: List the edge cases this function should handle, including
inputs the author may not have considered. Do not write tests yet.
Step 2: For each edge case, state the expected result and whether
the current code produces it.
Step 3: Write tests for every case. Mark tests you expect to fail
with a comment explaining why.

Function:
{{code}}

The function under test

function slugify( string $title ): string {
	$slug = strtolower( trim( $title ) );
	$slug = preg_replace( '/[^a-z0-9]+/', '-', $slug );
	return trim( $slug, '-' );
}

It looks fine. Step 1 of the prompt returned a list that included one case we hadn’t thought about: non-ASCII characters. strtolower() doesn’t transliterate, so “Café Menu” becomes caf-menu. The accented letter is silently dropped.

The tests

use PHPUnit\Framework\TestCase;

final class SlugifyTest extends TestCase {

	public function test_converts_spaces_and_punctuation(): void {
		$this->assertSame( 'hello-world', slugify( 'Hello, World!' ) );
	}

	public function test_trims_leading_and_trailing_separators(): void {
		$this->assertSame( 'ai-prompts', slugify( '  --AI Prompts--  ' ) );
	}

	public function test_returns_empty_string_for_symbols_only(): void {
		$this->assertSame( '', slugify( '!!!' ) );
	}

	// Expected to fail: accented characters are dropped, not transliterated.
	public function test_keeps_accented_letters_readable(): void {
		$this->assertSame( 'cafe-menu', slugify( 'Café Menu' ) );
	}
}

Why the order matters

  • Asking for edge cases first makes the model reason about intended behaviour, not current behaviour.
  • Step 2 separates “what should happen” from “what happens”, which is where bugs show up.
  • Marked failing tests are a to-do list, not noise.

A test suite that passes on the first run should make you suspicious, not happy.

QA lead

Keep a human in the loop

Always run the generated tests and read the assertions. A test that asserts the wrong value is worse than no test at all. It gives you confidence you haven’t earned.

←
Previous:
How to Feed Your Codebase Context Without Pasting Everything
→
Next:
Aspect Ratios, Negative Prompts and Why Your Images Look Off

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Coding with AI 3
  • Image Prompts 3
  • News & Updates 1
  • Prompt Engineering 3
  • Team Workflows 2
  • Writing & Marketing 2

Latest

  • Featured image for blog post Image
  • Turn survey answers into insights Text
  • Plan a feature: technical spec and task list Code
  • Customer case study from interview notes Text
  • Seasonal campaign key visual Image
  • Objection handling guide Text

View all prompts

PromptPress is a WordPress theme that comes with an AI prompt library plugin. Save your best prompts with blanks, let anyone fill them in and copy a ready prompt for ChatGPT, Claude or Midjourney. Private, team or public.

News

  • Coding with AI 3
  • Image Prompts 3
  • News & Updates 1
  • Prompt Engineering 3
  • Team Workflows 2
  • Writing & Marketing 2

Prompts

  • Brand Visuals (9 prompts)
  • Content Engine (5 prompts)
  • Dev Toolkit (4 prompts)
  • Launch Kit (5 prompts)
  • Research & Insights (1 prompts)
  • Sales & Outreach (3 prompts)

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© 2026 PromptPress • PromptPress™ Theme
↑Scroll to top