mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2026-02-16 05:05:39 +01:00
2.1 KiB
2.1 KiB
title, description
| title | description |
|---|---|
| Testing | Testing strategies and running tests |
Running Tests
# Run all tests
flutter test
# Run specific test file
flutter test test/battery_test.dart
# Run with coverage
flutter test --coverage
Test Structure
Tests are located in the test/ directory mirroring the lib structure:
test/
├── data/
│ ├── model/
│ └── provider/
├── view/
│ └── widget/
└── test_helpers.dart
Unit Tests
Test business logic and data models:
test('should calculate CPU percentage', () {
final cpu = CpuModel(usage: 75.0);
expect(cpu.usagePercentage, '75%');
});
Widget Tests
Test UI components:
testWidgets('ServerCard displays server name', (tester) async {
await tester.pumpWidget(
ProviderScope(
child: MaterialApp(
home: ServerCard(server: testServer),
),
),
);
expect(find.text('Test Server'), findsOneWidget);
});
Provider Tests
Test Riverpod providers:
test('serverStatusProvider returns status', () async {
final container = ProviderContainer();
final status = await container.read(serverStatusProvider(testServer).future);
expect(status, isA<StatusModel>());
});
Mocking
Use mocks for external dependencies:
class MockSshService extends Mock implements SshService {}
test('connects to server', () async {
final mockSsh = MockSshService();
when(mockSsh.connect(any)).thenAnswer((_) async => true);
// Test with mock
});
Integration Tests
Test complete user flows (in integration_test/):
testWidgets('add server flow', (tester) async {
await tester.pumpWidget(MyApp());
// Tap add button
await tester.tap(find.byIcon(Icons.add));
await tester.pumpAndSettle();
// Fill form
await tester.enterText(find.byKey(Key('name')), 'Test Server');
// ...
});
Best Practices
- Arrange-Act-Assert: Structure tests clearly
- Descriptive names: Test names should describe behavior
- One assertion per test: Keep tests focused
- Mock external deps: Don't depend on real servers
- Test edge cases: Empty lists, null values, etc.