Run your tests with multiple variants

Flutter tips Published on

Running tests for different input values can often mean writing very similar tests over and over.

But there's a smarter way in Flutter!

Run Tests Easily

Using a tool called ValueVariant, you can test the same code block with a list of different values without writing a separate test for each one.

This makes your test files much cleaner and easier to read.

How it Works

You define a list of values you want your test to use. This list becomes your ValueVariant. You can put any data type you need into this list.

Setting up Your Test

When you use testWidgets (or similar test functions), you can add a special parameter called variant. You pass your ValueVariant list here.

This tells the Flutter testing framework to run this single test multiple times, once for each item in your variant list.

Get the Current Value

Inside the test code itself, you often need to know which specific value from your list the current test run is using.

You can easily get this value by accessing the .currentValue property of your ValueVariant variable.

Using variants helps ensure your code works correctly across a range of inputs efficiently.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

What types of values can I use in a ValueVariant?

You can use any data type you want in a ValueVariant list, like numbers, strings, booleans, or even custom objects.

Does this replace writing individual tests for each value?

Yes, for cases where the core test logic is the same but only the input value changes, ValueVariant allows you to write the test once and run it with all your desired inputs.

Is ValueVariant only for widget tests?

While commonly shown with widget tests, ValueVariant can be used with other test types in Flutter that support the `variant` parameter, making it versatile for different testing needs.

Read more
You may also be interested in
Detect running platform  blog card image
Detect running platform
Published on 2025-05-12T12:24:52.620Z
Mocking rest api calls  blog card image
Mocking rest api calls
Published on 2025-05-12T09:52:33.783Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved