Skip to content

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:

AI Prompt: Transforming Your Resume for ATS Mastery

AI Prompt โ€” Code Refactoring With Constraints

Debug Race Conditions Instead of Guessing

Post Views: 4

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 *

April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

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

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

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

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

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