# Testing

Testing is a crucial part of the development process, ensuring that your code behaves as expected and helping to prevent bugs. Flutter provides a rich set of testing features to test apps at unit, widget, and integration levels.

## 1. Unit Testing 📊

Unit testing involves testing individual functions, methods, or classes. The `test` package provides tools to write unit tests.

### Setup:

Add the `test` package to your `pubspec.yaml` file:

```yaml
dev_dependencies:
  test: ^1.24.9
```

### Example:

```dart
import 'package:test/test.dart';

void main() {
  test('Addition', () {
    expect(2 + 2, equals(4));
  });
}
```

## 2. Widget Testing 🎡

Widget testing involves testing a single widget. The `flutter_test` package provides tools to create widget tests.

### Setup:

The `flutter_test` package is included in every Flutter app.

### Example:

```dart
import 'package:flutter_test/flutter_test.dart';
import 'package:my_app/main.dart';  // Import your main.dart file

void main() {
  testWidgets('Finds a text widget', (WidgetTester tester) async {
    await tester.pumpWidget(MyApp());

    expect(find.text('Hello, world!'), findsOneWidget);
  });
}
```

## 3. Integration Testing 🔄

Integration testing involves testing a complete app or a large part of an app.

### Setup:

Add the `integration_test`, and `flutter_test` packages to your `pubspec.yaml` file:

```yaml
dev_dependencies:
  integration_test:
    sdk: flutter
  flutter_test:
    sdk: flutter
```

#### Example:

```dart
import 'package:integration_test/integration_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:my_app/main.dart';

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  testWidgets("failing test example", (WidgetTester tester) async {
    await tester.pumpWidget(MyApp());

    expect(find.text('Hello, world!'), findsNothing);
  });
}
```

## Complete Example Code 📜

Below is a simple Flutter app with corresponding unit, widget, and integration tests.

### The app (`lib/main.dart`):

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Testing Example')),
        body: Center(child: Text('Hello, world!')),
      ),
    );
  }
}
```

### Unit Test (`test/unit_test.dart`):

```dart
import 'package:test/test.dart';

void main() {
  test('Addition', () {
    expect(2 + 2, equals(4));
  });
}
```

### Widget Test (`test/widget_test.dart`):

```dart
import 'package:flutter_test/flutter_test.dart';
import 'package:testing_example/main.dart';

void main() {
  testWidgets('Finds a text widget', (WidgetTester tester) async {
    await tester.pumpWidget(MyApp());

    expect(find.text('Hello, world!'), findsOneWidget);
  });
}
```

### Integration Test (`integration_test/app_test.dart`):

```dart
import 'package:integration_test/integration_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:testing_example/main.dart';

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  testWidgets("finds the text widget", (WidgetTester tester) async {
    await tester.pumpWidget(MyApp());

    expect(find.text('Hello, world!'), findsOneWidget);
  });
}
```

In this example, you have a simple app with corresponding tests at unit, widget, and integration levels. This sets a good foundation for adding more complex tests as your app grows.

***

## Assignments 📝

* [ ] Write a unit test for a function in your app.
* [ ] Write a widget test for a custom widget in your app.
* [ ] Write an integration test for a key workflow in your app.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://flutteruniversity.gitbook.io/docs/learn-flutter/intermediate/testing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
