4.6 KiB
4.6 KiB
Testing Guide
Overview
The DBIS system includes comprehensive test coverage across multiple test types:
- Unit tests
- Integration tests
- Invariant tests
- Fuzz tests
- Fork tests
Running Tests
All Tests
forge test
Specific Test File
forge test --match-path test/vault/DBISInstitutionalVault.t.sol
Verbose Output
forge test -vvv
Gas Report
forge test --gas-report
Test Structure
Unit Tests
Located in test/{contract}/:
test/vault/DBISInstitutionalVault.t.soltest/router/FlashLoanRouter.t.soltest/kernel/RecursiveLeverageKernel.t.sol
Integration Tests
Located in test/integration/:
test/integration/FullCycle.t.soltest/integration/AmortizationInvariant.t.sol
Fuzz Tests
Located in test/fuzz/:
test/fuzz/KernelFuzz.t.sol
Test Categories
1. Core Functionality Tests
Vault Tests
function test_RecordCollateralAdded() public {
// Test collateral recording
}
function test_RecordDebtRepaid() public {
// Test debt repayment recording
}
function test_VerifyPositionImproved() public {
// Test invariant verification
}
Kernel Tests
function test_ExecuteAmortizingCycle() public {
// Test full amortization cycle
}
function test_ExecuteAmortizingCycleRevertsIfInvariantFails() public {
// Test invariant enforcement
}
2. Invariant Tests
Foundry invariant tests ensure invariants hold:
function invariant_HealthFactorNeverDecreases() public {
// HF must never decrease
}
function invariant_DebtNeverIncreases() public {
// Debt must never increase
}
function invariant_CollateralNeverDecreases() public {
// Collateral must never decrease
}
Run invariant tests:
forge test --match-test invariant
3. Fuzz Tests
Random input testing to find edge cases:
function testFuzz_ExecuteCycleWithRandomAmounts(
uint256 flashAmount,
uint256 yieldAmount
) public {
// Fuzz with random amounts
// Ensure invariants hold
}
Run fuzz tests:
forge test --match-test testFuzz
4. Fork Tests
Test against mainnet state:
forge test --fork-url $RPC_URL
Test Utilities
Helpers
Create test helpers in test/helpers/:
contract TestHelpers {
function deployMockAavePool() internal returns (address) {
// Deploy mock
}
function createPosition(uint256 collateral, uint256 debt) internal {
// Setup test position
}
}
Mocks
Mock external contracts:
- Mock Aave Pool
- Mock Oracle
- Mock Uniswap Router
Test Scenarios
Happy Path
- Successful amortization cycle
- Position improvement
- Invariant preservation
Edge Cases
- Maximum loops reached
- Flash loan fee exceeds profit
- Swap slippage too high
- Health factor at threshold
Failure Modes
- Policy denial
- Invariant violation
- Flash loan unavailable
- Insufficient gas
Stress Tests
- Large positions
- High leverage
- Multiple concurrent cycles
- Rapid price changes
Coverage Goals
Target coverage:
- Core contracts: >90%
- Governance: >85%
- Utilities: >80%
- Overall: >85%
Generate coverage report:
forge coverage
Continuous Integration
GitHub Actions Example
name: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
- name: Run tests
run: forge test
- name: Generate coverage
run: forge coverage
Best Practices
- Test All Edge Cases: Include boundary conditions
- Use Descriptive Names: Clear test function names
- Test Invariants: Always verify invariants
- Mock External Dependencies: Use mocks for external calls
- Test Both Success and Failure: Cover all paths
- Use Fuzz Testing: Find unexpected edge cases
- Fork Tests for Integration: Test with real protocols
- Gas Optimization Tests: Ensure gas efficiency
Debugging Tests
Verbose Output
forge test -vvvv # Maximum verbosity
Debug Specific Test
forge test --match-test test_ExecuteCycle -vvv
Trace Transactions
forge test --debug test_ExecuteCycle
Test Data
Fixtures
Store test data in test/fixtures/:
- Sample positions
- Test transaction data
- Expected outcomes
Constants
Define test constants:
uint256 constant TEST_COLLATERAL = 1000e18;
uint256 constant TEST_DEBT = 800e18;
address constant TEST_ASSET = 0x...