Skip to content

Unit Test Üretimi: Jest, Pytest, JUnit Örnekleri

Mevcut kod için kapsamlı test üretmek, edge case'leri yakalamak. 3 popüler framework için promptlar.

Şükrü Yusuf KAYA
9 min read
Intermediate
Unit Test Üretimi: Jest, Pytest, JUnit Örnekleri

"Bu fonksiyona test yaz" tek satır = profesyonel suite#

ChatGPT, mevcut bir fonksiyonu okuyup:
  • Happy path
  • Edge case
  • Error case
  • Boundary case
  • Negatif case
— hepsini kapsayan test suite yazabilir.
Prompt:
Aşağıdaki TypeScript fonksiyonu için Vitest test suite yaz. [Fonksiyon] İste: - describe + it yapısı - Her teste yorumu olarak senaryo açıklaması (Türkçe) - Mock kullanılacaksa vi.mock + reset - Edge case (boş, null, undefined, max, min) - Error case (throw scenarios) - Test coverage hedefi: %95+ - Test data: realistic, çeşitli
typescript
import { describe, it, expect, vi } from "vitest";
import { calculateDiscount } from "./pricing";
 
describe("calculateDiscount", () => {
// Senaryo: Standart indirim hesabı
it("returns 10% discount for amounts over 1000", () => {
expect(calculateDiscount(1500)).toBe(150);
});
 
// Senaryo: Sınır durumu — eşik değeri
it("applies discount exactly at threshold", () => {
expect(calculateDiscount(1000)).toBe(100);
});
 
// Senaryo: Eşik altı — indirim yok
it("returns 0 discount below threshold", () => {
expect(calculateDiscount(999)).toBe(0);
});
 
// Senaryo: Sıfır miktar
it("returns 0 for zero amount", () => {
expect(calculateDiscount(0)).toBe(0);
});
 
// Senaryo: Negatif miktar — hata
it("throws ValidationError for negative amount", () => {
expect(() => calculateDiscount(-100)).toThrow("Amount must be non-negative");
});
 
// Senaryo: Çok büyük miktar — overflow yok
it("handles large amounts correctly", () => {
expect(calculateDiscount(Number.MAX_SAFE_INTEGER)).toBeGreaterThan(0);
});
});
ChatGPT'nin ürettiği örnek Vitest suite — happy + edge + boundary + error.

Özet#

✓ Test suite tek promptla üretilebilir ✓ Happy + edge + boundary + error = %95 coverage ✓ Framework belirt: Jest/Vitest, Pytest, JUnit ✓ Fixture, mock, parametrize — gerektiğinde iste
Sıradaki ders: Dokümantasyon — JSDoc, Docstring, README.

Yorumlar & Soru-Cevap

(0)
Yorum yazmak için giriş yap.
Yorumlar yükleniyor...

Related Content