Loading...
Loading...
Generate, execute, and analyze tests for codebases, covering unit, integration, and end-to-end testing with coverage reporting. Use when the user requests testing or provides relevant inputs for this workflow.
npx skill4agent add seb1n/awesome-ai-agent-skills testingpytest --covjest --coverage| Language | Framework | Runner Command |
|---|---|---|
| Python | pytest | |
| JavaScript | Jest | |
| TypeScript | Jest / Vitest | |
| Java | JUnit 5 | |
| Go | testing (stdlib) | |
| Rust | cargo test | |
src/utils/parser.pysrc/cart.pyclass ShoppingCart:
def __init__(self):
self.items = []
def add_item(self, name: str, price: float, quantity: int = 1):
if price < 0:
raise ValueError("Price cannot be negative")
if quantity < 1:
raise ValueError("Quantity must be at least 1")
self.items.append({"name": name, "price": price, "quantity": quantity})
def total(self) -> float:
return sum(item["price"] * item["quantity"] for item in self.items)
def remove_item(self, name: str):
self.items = [item for item in self.items if item["name"] != name]tests/test_cart.pyimport pytest
from src.cart import ShoppingCart
@pytest.fixture
def cart():
return ShoppingCart()
def test_add_item_and_total(cart):
cart.add_item("Widget", 9.99, 2)
assert cart.total() == pytest.approx(19.98)
def test_empty_cart_total_is_zero(cart):
assert cart.total() == 0.0
def test_add_item_negative_price_raises(cart):
with pytest.raises(ValueError, match="Price cannot be negative"):
cart.add_item("Bad", -1.0)
def test_add_item_zero_quantity_raises(cart):
with pytest.raises(ValueError, match="Quantity must be at least 1"):
cart.add_item("Bad", 5.0, 0)
def test_remove_item(cart):
cart.add_item("A", 1.0)
cart.add_item("B", 2.0)
cart.remove_item("A")
assert cart.total() == 2.0
def test_remove_nonexistent_item_does_nothing(cart):
cart.add_item("A", 1.0)
cart.remove_item("Z")
assert cart.total() == 1.0pytest tests/test_cart.py --cov=src -vsrc/validator.jsfunction isValidEmail(email) {
if (typeof email !== "string") return false;
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}
function isStrongPassword(password) {
if (typeof password !== "string") return false;
return (
password.length >= 8 &&
/[A-Z]/.test(password) &&
/[a-z]/.test(password) &&
/[0-9]/.test(password)
);
}
module.exports = { isValidEmail, isStrongPassword };src/__tests__/validator.test.jsconst { isValidEmail, isStrongPassword } = require("../validator");
describe("isValidEmail", () => {
test.each([
["user@example.com", true],
["name+tag@sub.domain.org", true],
["missing-at-sign.com", false],
["@no-local.com", false],
["spaces in@email.com", false],
["", false],
])("isValidEmail(%s) => %s", (input, expected) => {
expect(isValidEmail(input)).toBe(expected);
});
test("returns false for non-string input", () => {
expect(isValidEmail(null)).toBe(false);
expect(isValidEmail(42)).toBe(false);
});
});
describe("isStrongPassword", () => {
test("accepts a strong password", () => {
expect(isStrongPassword("Str0ngPwd")).toBe(true);
});
test("rejects short password", () => {
expect(isStrongPassword("Ab1")).toBe(false);
});
test("rejects password without uppercase", () => {
expect(isStrongPassword("alllower1")).toBe(false);
});
test("rejects non-string input", () => {
expect(isStrongPassword(undefined)).toBe(false);
});
});npx jest --coverage --verbosetest_empty_cart_total_is_zerotest_total_method@pytest.mark.parametrizetest.eachpytest-asyncio