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

AI Prompt: Generate Unit Tests with 90% Coverage Instantly

- 19.03.26 - ErcanOPAK

๐Ÿงช Write Tests 100x Faster

Writing tests is tedious. AI generates comprehensive test suites in seconds. Edge cases included.

The Test Generation Prompt

Generate comprehensive unit tests for this code:

[Paste your function/class]

Requirements:
- Use [Testing Framework: Jest/xUnit/pytest/etc]
- Test happy path
- Test edge cases (null, empty, invalid input)
- Test error conditions
- Mock external dependencies
- Aim for 90%+ code coverage
- Include descriptive test names
- Add comments explaining what each test validates

Generate at least 10 test cases.

Example Output

// Original function
function calculateDiscount(price, userType) {
  if (userType === 'premium') return price * 0.8;
  if (userType === 'regular') return price * 0.9;
  return price;
}

// AI-generated tests
describe('calculateDiscount', () => {
  test('applies 20% discount for premium users', () => {
    expect(calculateDiscount(100, 'premium')).toBe(80);
  });

  test('applies 10% discount for regular users', () => {
    expect(calculateDiscount(100, 'regular')).toBe(90);
  });

  test('returns original price for guest users', () => {
    expect(calculateDiscount(100, 'guest')).toBe(100);
  });

  test('handles null userType', () => {
    expect(calculateDiscount(100, null)).toBe(100);
  });

  test('handles undefined userType', () => {
    expect(calculateDiscount(100, undefined)).toBe(100);
  });

  test('handles zero price', () => {
    expect(calculateDiscount(0, 'premium')).toBe(0);
  });

  test('handles negative price', () => {
    expect(calculateDiscount(-100, 'premium')).toBe(-80);
  });
});

๐ŸŽฏ Advanced: Integration Tests

Generate integration tests for this API endpoint:

[Paste controller code]

Test:
- Authentication (valid/invalid tokens)
- Authorization (different user roles)
- Request validation (missing fields, invalid formats)
- Database interactions (successful CRUD, conflicts)
- Error responses (500, 404, 400, 401, 403)
- Response format validation

Use supertest for HTTP testing.

“Generated 47 test cases for complex payment processing function. AI caught edge cases I never thought of. Test coverage: 23% โ†’ 94% in 5 minutes.”

โ€” Backend Engineer, Fintech

Related posts:

The AI Prompt That Finds Every Unbounded Query That Could Blow Up Your Database Under Real Traffic

The AI Prompt That Finds Every Unbounded Query That Could Blow Up Your Database Under Real Traffic

The AI Prompt That Turns a Flaky Test Failure Into a Ranked List of Likely Root Causes

The AI Prompt That Turns a Flaky Test Failure Into a Ranked List of Likely Root Causes

The AI Prompt That Finds Every Place a Date Comparison Ignores Time Zones and Silently Compares Loca...

The AI Prompt That Finds Every Place a Date Comparison Ignores Time Zones and Silently Compares Loca...

Views: 15

Post navigation

AI Prompt: Get Senior-Level Code Reviews Instantly
AI Prompt: Generate Complex Regex Patterns in Plain English

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,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

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

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

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