Skip to content

Testing

This guide covers testing strategies and tools used in the TradeX platform.

  • Unit Tests: Test individual components in isolation
  • Integration Tests: Test service interactions
  • End-to-End Tests: Test complete workflows
  • Performance Tests: Test system performance under load
Terminal window
cd apps/marketdata-service
# Run all tests
go test ./...
# Run specific test
go test ./internal/service/trade
# Run with coverage
go test -cover ./...
Terminal window
# Run integration tests (requires infrastructure)
go test -tags=integration ./...
func TestTradeService_GetRecentTrades(t *testing.T) {
// Setup
repo := mock.NewTradeRepository()
service := trade.NewService(repo, logger, nil)
// Execute
trades, err := service.GetRecentTrades(ctx, "BTCUSDT-PERP", 10)
// Assert
assert.NoError(t, err)
assert.Len(t, trades, 10)
}
Terminal window
cd apps/order-service
# Run tests
bun test
# Run with coverage
bun test --coverage
import { describe, it, expect } from 'bun:test';
describe('OrderService', () => {
it('should create order', async () => {
const service = new OrderService();
const order = await service.createOrder({
instrumentId: '...',
side: 'buy',
type: 'limit',
quantity: 10,
price: 50000
});
expect(order).toBeDefined();
expect(order.status).toBe('accepted');
});
});
Terminal window
cd apps/auth-service
# Run tests
uv run pytest
# Run with coverage
uv run pytest --cov=app --cov-report=html
import pytest
from app.services.auth_service import AuthService
def test_login_success():
service = AuthService()
result = service.login("user@example.com", "password")
assert result.success is True
assert result.token is not None

Use Docker Compose for test infrastructure:

docker-compose.test.yml
version: '3.8'
services:
postgres-test:
image: postgres:14
environment:
POSTGRES_DB: tradex_test
describe('Order Service Integration', () => {
beforeAll(async () => {
// Start test infrastructure
await startTestInfra();
});
afterAll(async () => {
// Stop test infrastructure
await stopTestInfra();
});
it('should process order end-to-end', async () => {
// Test complete order flow
});
});
  1. User Registration and Login
  2. Order Placement and Execution
  3. Market Data Subscription
  4. Wallet Operations
describe('E2E: Order Flow', () => {
it('should place and execute order', async () => {
// 1. Authenticate
const token = await login();
// 2. Place order
const order = await placeOrder(token, {
symbol: 'BTCUSDT-PERP',
side: 'buy',
quantity: 1,
price: 50000
});
// 3. Wait for execution
await waitForOrderExecution(order.id);
// 4. Verify trade
const trades = await getTrades(token);
expect(trades).toContainEqual(
expect.objectContaining({ orderId: order.id })
);
});
});

Use tools like:

  • k6: Load testing tool
  • Apache Bench: HTTP load testing
  • JMeter: Comprehensive load testing
import http from 'k6/http';
import { check } from 'k6';
export const options = {
vus: 10,
duration: '30s',
};
export default function () {
const res = http.get('http://localhost:8080/v1/trades?symbol=BTCUSDT-PERP');
check(res, {
'status is 200': (r) => r.status === 200,
'response time < 100ms': (r) => r.timings.duration < 100,
});
}
// Mock repository
type MockTradeRepository struct {
trades []*Trade
}
func (m *MockTradeRepository) GetRecentTrades(ctx context.Context, symbol string, limit int) ([]*Trade, error) {
return m.trades[:limit], nil
}
// Mock service
const mockWalletService = {
getBalance: jest.fn().mockResolvedValue({ available: 1000 }),
holdBalance: jest.fn().mockResolvedValue({ success: true })
};

Create reusable test data:

export const testInstruments = {
btcPerp: {
id: 'test-btc-perp',
symbol: 'BTCUSDT-PERP',
status: 'active'
}
};
Terminal window
# Seed test data
bun run seed:test
name: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: bun install
- run: bun test
  1. Test Isolation: Each test should be independent
  2. Cleanup: Clean up test data after tests
  3. Fast Tests: Keep unit tests fast (< 1s)
  4. Clear Assertions: Use descriptive assertion messages
  5. Test Coverage: Aim for > 80% coverage