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.