A skill that automates the creation of comprehensive unit tests for functions or classes, emphasizing coverage of edge cases and assertions.
To generate thorough, production-ready unit tests that ensure code reliability and maintainability. This skill helps developers catch regressions early, document expected behavior, and follow testing best practices for their specific framework.
Trigger this skill when the user:
When asked to generate unit tests, follow this process:
assertTrue).tests/, __tests__/, spec/).test_*.py, *.test.js).User Request: “Write tests for this function”
def calculate_discount(price: float, discount_percent: float) -> float:
"""Calculate the discounted price."""
if price < 0 or discount_percent < 0:
raise ValueError("Price and discount must be non-negative")
if discount_percent > 100:
raise ValueError("Discount cannot exceed 100%")
return price * (1 - discount_percent / 100)
AI Response:
import pytest
from your_module import calculate_discount
class TestCalculateDiscount:
"""Tests for the calculate_discount function."""
# Normal cases
def test_calculate_discount_with_valid_values(self):
"""Test discount calculation with typical values."""
assert calculate_discount(100.0, 20.0) == 80.0
def test_calculate_discount_with_zero_discount(self):
"""Test that zero discount returns original price."""
assert calculate_discount(50.0, 0.0) == 50.0
def test_calculate_discount_with_full_discount(self):
"""Test that 100% discount returns zero."""
assert calculate_discount(100.0, 100.0) == 0.0
# Edge cases
def test_calculate_discount_with_zero_price(self):
"""Test discount on zero price."""
assert calculate_discount(0.0, 50.0) == 0.0
def test_calculate_discount_with_small_values(self):
"""Test with very small decimal values."""
result = calculate_discount(0.01, 10.0)
assert result == pytest.approx(0.009, rel=1e-3)
def test_calculate_discount_with_large_price(self):
"""Test with large price values."""
assert calculate_discount(1_000_000.0, 25.0) == 750_000.0
# Error cases
def test_calculate_discount_negative_price_raises_error(self):
"""Test that negative price raises ValueError."""
with pytest.raises(ValueError, match="non-negative"):
calculate_discount(-10.0, 20.0)
def test_calculate_discount_negative_discount_raises_error(self):
"""Test that negative discount raises ValueError."""
with pytest.raises(ValueError, match="non-negative"):
calculate_discount(100.0, -5.0)
def test_calculate_discount_over_100_percent_raises_error(self):
"""Test that discount over 100% raises ValueError."""
with pytest.raises(ValueError, match="exceed 100"):
calculate_discount(100.0, 150.0)
User Request: “Generate Jest tests for this function”
function validateEmail(email) {
if (typeof email !== 'string') {
throw new TypeError('Email must be a string');
}
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(email);
}
AI Response:
const { validateEmail } = require('./validators');
describe('validateEmail', () => {
// Normal cases
it('should return true for valid email addresses', () => {
expect(validateEmail('user@example.com')).toBe(true);
expect(validateEmail('name.surname@domain.org')).toBe(true);
});
it('should return false for invalid email addresses', () => {
expect(validateEmail('invalid-email')).toBe(false);
expect(validateEmail('missing@domain')).toBe(false);
expect(validateEmail('@nodomain.com')).toBe(false);
});
// Edge cases
it('should return false for empty string', () => {
expect(validateEmail('')).toBe(false);
});
it('should return false for email with spaces', () => {
expect(validateEmail('user @example.com')).toBe(false);
});
// Error cases
it('should throw TypeError for non-string input', () => {
expect(() => validateEmail(null)).toThrow(TypeError);
expect(() => validateEmail(undefined)).toThrow(TypeError);
expect(() => validateEmail(123)).toThrow(TypeError);
});
});
assertEqual, toEqual) over generic ones (assertTrue).