Testing
Running Tests
Section titled “Running Tests”# Run all testsflutter test
# Run specific test fileflutter test test/battery_test.dart
# Run with coverageflutter test --coverageTest Structure
Section titled “Test Structure”Tests are located in the test/ directory mirroring the lib structure:
test/├── data/│ ├── model/│ └── provider/├── view/│ └── widget/└── test_helpers.dartUnit Tests
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “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.