Anthropic Claudeソフトウェア開発⭐ リポ 299品質スコア 84/100
Property Testing
fast-checkを使用したプロパティベーステストによるビジネスロジック検証
description の原文を見る
Property-based testing with fast-check for business logic validation
SKILL.md 本文
プロパティ テスティング
LivestockAI は、ビジネスロジックの不変性を検証するために、fast-check を使用したプロパティベースドテスティング (PBT) を採用しています。
プロパティ テスティングとは
具体的な例をテストする代わりに、プロパティテストは、あらゆる可能な入力に対してプロパティが成立することを検証します:
// 例ベースのテスト
it('calculates FCR correctly', () => {
expect(calculateFCR(150, 100)).toBe(1.5)
})
// プロパティベースのテスト
it('FCR is always positive when inputs are positive', () => {
fc.assert(
fc.property(
fc.float({ min: 0.1, max: 10000 }),
fc.float({ min: 0.1, max: 10000 }),
(feed, weight) => {
const fcr = calculateFCR(feed, weight)
return fcr === null || fcr > 0
},
),
)
})
fast-check の基本
import { describe, it, expect } from 'vitest'
import * as fc from 'fast-check'
describe('Property Tests', () => {
it('property holds for all inputs', () => {
fc.assert(
fc.property(fc.integer({ min: 1, max: 100000 }), (quantity) => {
// Property must return true or throw
return quantity > 0
}),
{ numRuns: 100 },
)
})
})
よく使用されるアービトラリ
// 整数
fc.integer({ min: 1, max: 100000 })
fc.nat() // 非負整数
// 浮動小数点数
fc.float({ min: 0, max: 10000 })
// 文字列
fc.string()
fc.uuid()
// 配列
fc.array(fc.integer(), { minLength: 0, maxLength: 20 })
// オブジェクト
fc.record({
quantity: fc.integer({ min: 1, max: 1000 }),
price: fc.float({ min: 0, max: 10000 }),
})
インベントリ不変性の例
tests/features/batches/batches.property.test.ts から:
/**
* Property 4: Inventory Invariant
* For any batch, current_quantity SHALL always equal:
* initial_quantity - sum(mortality) - sum(sales)
*/
describe('Property 4: Inventory Invariant', () => {
it('current_quantity equals initial - mortalities - sales', () => {
fc.assert(
fc.property(
fc.integer({ min: 1, max: 100000 }),
fc.array(fc.integer({ min: 1, max: 1000 })),
fc.array(fc.integer({ min: 1, max: 1000 })),
(initial, mortalities, sales) => {
const { constrained } = constrainQuantities(
initial,
mortalities,
sales,
)
const current = calculateCurrentQuantity(initial, constrained)
expect(current).toBeGreaterThanOrEqual(0)
expect(current).toBeLessThanOrEqual(initial)
},
),
{ numRuns: 100 },
)
})
})
要件へのリンク
テストに要件リンクで注釈を付けます:
/**
* **Validates: Requirements 3.2, 4.2, 8.2**
*/
it('inventory invariant holds', () => {
// ...
})
プロパティ テスティングを使用するべき場合
- 数学的計算 (FCR、死亡率、利益)
- 不変性 (数量が負にならない、合計が一致する)
- 状態遷移 (バッチステータスの変更)
- データ変換 (通貨換算)
関連スキル
vitest-patterns- ユニットテストの基本three-layer-architecture- サービスレイヤーのテスト
ライセンス: MIT(寛容ライセンスのため全文を引用しています) · 原本リポジトリ
詳細情報
- 作者
- majiayu000
- ライセンス
- MIT
- 最終更新
- 2026/5/4
Source: https://github.com/majiayu000/claude-skill-registry / ライセンス: MIT