Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
AI

The AI Prompt That Writes Property-Based Tests for a Pure Function

- 19.11.26 - ErcanOPAK

🤖 The AI Prompt That Writes Property-Based Tests for a Pure Function

Example-based unit tests only check the specific inputs you thought to write down, so a pure function with a subtle edge case — an empty collection, a boundary value, an input that’s technically valid but unusual — can pass every test you wrote and still break the moment real data exercises a path nobody considered. Property-based testing flips that: instead of specific examples, you assert a general rule the function must hold for ANY input in a defined domain, and a property-based library generates hundreds of cases to try to break that rule — but writing good properties by hand takes a different kind of thinking, and a blank page is where most people give up on the technique.

🔎 The Problem

Here is a pure function. Write property-based tests for it using <FRAMEWORK, e.g.
fast-check / Hypothesis / FsCheck>.
1. Identify the function's invariants - properties that must hold for EVERY valid
   input, not just the examples I give you (idempotence, a round-trip with its
   inverse, a relationship between input size and output size, commutativity).
2. For each invariant, write one property test with a clearly named generator
   describing the input domain, biased toward edge values (empty, zero, negative,
   max-length), not just uniformly random ones.
3. Do not write example-based assertions (expect(f(2)).toBe(4)) - every assertion
   must be a property that holds across the generated range.
4. List, in a short comment above each property, what specific bug class that
   property is designed to catch.
5. If the function has a precondition (sorted input, non-negative, etc.), encode it
   in the generator itself, not as a runtime check inside the test.

Function:
<PASTE THE FUNCTION>

✅ Why This Works

  • Requiring a named generator per property forces you to think about the function’s actual input domain before writing any assertion, which is usually where the real insight in property-based testing happens.
  • Banning example-based assertions keeps the model from quietly falling back to the easier, weaker style it defaults to when a property is hard to express.
  • Asking for the bug class each property targets turns the test suite into documentation of what could go wrong, useful long after the session that generated it is gone.

🚨 Why This Is Dangerous

  • A property-based test can pass for years and then fail once on a generated input nobody saved, because most frameworks don’t persist the failing seed by default — always capture and commit the failing case as a permanent example-based regression test the moment one turns up, don’t just rerun and hope.

An example test tells you the function works on the inputs you thought of; a property test tells you it works on the ones you didn’t.

— AI Power User

Related posts:

The AI Prompt That Plans a Full Week of Home Workouts Around the Equipment You Actually Own

The AI Prompt That Plans a Full Week of Home Workouts Around the Equipment You Actually Own

The AI Prompt That Turns a Phone Full of Kids' Artwork and School Certificate Photos Into One Organi...

The AI Prompt That Turns a Phone Full of Kids' Artwork and School Certificate Photos Into One Organi...

The AI Prompt That Finds Every Place Your API Returns a 200 Status on a Failed Operation

The AI Prompt That Finds Every Place Your API Returns a 200 Status on a Failed Operation

Views: 1

Post navigation

HTML5: Fix a datalist Suggestion List Not Filtering as You Type
The AI Prompt That Negotiates a Lower Bill With Your Internet Provider

Leave a Reply Cancel reply

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

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (658)
  • Add Constraint to SQL Table to ensure email contains @ (613)
  • Average of all values in a column that are not zero in SQL (571)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (549)
  • Find numbers with more than two decimal places in SQL (477)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK
  • Git: Fix a Worktree Locking a Branch Needed Somewhere Else
  • Asp.Net Core: Fix a Background Service Leaking Scoped Dependencies
  • SQL Server: Fix a Columnstore Index Fragmenting After Bulk Deletes
  • Docker: Fix a Container Losing Logs After a Restart Policy
  • Asp.Net Core: Fix Output Caching Serving a Stale Authorized Page

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com