Generate Flutter Unit, Widget and Integration Tests with AI

Use @test agent to add code testing to your Flutter app and catch bugs early

Generate Flutter Unit, Widget and Integration Tests with AI

Testing is something that is very close to our team. With AI writing a big chunk of our code, it becomes signicantly important to test it to ensure all requirements are met.

With unit, widget, and integration tests in place, you can make your codebase strong to handle any edge case scenario, save time spent on fixing bugs later, and deploy with peace of mind.

The Challenges of Writing Tests

Writing tests can be time-consuming, involving:

  1. Figuring out the exhaustive test cases
  2. Setting up mocks correctly
  3. Writing the tests themselves
  4. Fixing errors in both tests and code

The @test Agent to the Rescue

We built the @test agent to help you auto generate the cases, mocks and tests. It auto takes your coding style into consideration and provides the best possible results.

It consists of 3 commands /unit, /widget and /integration to write all kinds of test autonomously. To try it out, we will together write, run and debug an integration test for a LoginScreen:

1. Choose your command from the dropdown.

Available commands within @test agent
Available commands within @test agent

In our case, we will be choosing the /integration command.

2. Provide the flow of test.

We will be testing for a simple scenario ensuring that user is taken to home screen when correct credentials are added.

Entering test flow for the integration test
Entering test flow for the integration test

3. Attach the widgets involved

Attach the high level screens that are involved in the workflow like LoginScreen and HomeScreen. The nested widgets will be auto attached by CommandDash.

  • It is also recommended to first add keys to these screens and their widgets to make the elements selectable in the tests.
Attach widgets using `Attach Snippet to Dash`
Attach widgets using `Attach Snippet to Dash`

4. Provide an existing integration test as reference.

This is helpful when you have a specific pattern, code style or reusable steps that you want to provide as an example when generating this test.

CommandDash auto selects a few integration tests from your codebase as well if they exist.

We will skip this, since this is our first test.

5. Generate your test

Hit the send button and receive your generated integration test. Add it to your integration test file.

Send and get your Integration test
Send and get your Integration test

Enjoy a stable codebase 😌

Run your test locally to ensure the workflow works. Setup CI/CD to run all your tests before every deploy and release confidently.

Run your test to ensure the workflow works!
Run your test to ensure the workflow works!

Hope you find the article useful and would generate tests for your codebase!

Tips for Effective Testing

Here are a few tips for effective testing:

  1. If you have an existing codebase, smartly add tests as you modify or refactor code. Don't aim for 100% coverage in a single go.
  2. The cases you cover are more important than the code coverage.
  3. 10 minutes writing tests saves 100 minutes of debugging and fixing later.

All the best for your testing jouney!