Unit testing helps check small parts of your app to make sure they work right. It finds mistakes early so your app is more reliable.
Unit testing in Flutter
import 'package:test/test.dart'; void main() { test('description of test', () { // code to test expect(actual, expected); }); }
The test function defines a single test case with a description.
The expect function checks if the actual value matches the expected value.
test('adding two numbers', () { var sum = 2 + 3; expect(sum, 5); });
test('string contains substring', () { var text = 'hello world'; expect(text.contains('world'), true); });
This program tests a simple multiply function with two cases: normal multiplication and multiplication by zero.
import 'package:test/test.dart'; int multiply(int a, int b) { return a * b; } void main() { test('multiply two numbers', () { expect(multiply(4, 5), 20); }); test('multiply by zero', () { expect(multiply(7, 0), 0); }); }
Write small, focused tests that check one thing at a time.
Run tests often to catch errors early.
Use clear descriptions so you know what each test checks.
Unit tests check small parts of your code to find bugs early.
Use test and expect to write tests in Flutter.
Run tests regularly to keep your app reliable.
Practice
What is the main purpose of unit testing in Flutter?
Solution
Step 1: Understand unit testing purpose and compare options
Unit testing focuses on testing small parts of code independently to find bugs early. Only To check small parts of code for bugs early matches the purpose of unit testing; others relate to different app development stages.Final Answer:
To check small parts of code for bugs early -> Option AQuick Check:
Unit testing = check small code parts early [OK]
- Confusing unit testing with UI design
- Thinking unit tests deploy the app
- Believing unit tests write documentation
Which of the following is the correct syntax to write a simple test in Flutter?
?Solution
Step 1: Recall Flutter test syntax and match options
The correct syntax uses test('description', () { expect(actual, expected); }); Only test('description', () { expect(1 + 1, 2); }); matches the correct syntax with proper function and expect call.Final Answer:
test('description', () { expect(1 + 1, 2); }); -> Option BQuick Check:
Correct test syntax = test('description', () { expect(1 + 1, 2); }); [OK]
- Missing parentheses or curly braces
- Swapping test and expect arguments
- Using wrong function call syntax
What will be the output of this test code?
int add(int a, int b) => a + b;
test('add function test', () {
expect(add(2, 3), 5);
expect(add(0, 0), 0);
expect(add(-1, 1), 0);
});Choose the correct test result.
Solution
Step 1: Evaluate add function calls and check expect statements
add(2, 3) = 5, add(0, 0) = 0, add(-1, 1) = 0, all match expected values. All expect statements compare actual and expected values correctly, so all pass.Final Answer:
All tests pass successfully -> Option DQuick Check:
Correct add results = all tests pass [OK]
- Assuming negative numbers cause failure
- Misreading expected values
- Ignoring zero as valid input
Identify the error in this Flutter unit test code:
test('string length test', () {
var str = 'hello';
expect(str.length, '5');
});Solution
Step 1: Check expect arguments types and type mismatch effect
str.length returns an integer (5), but expected value is a string ('5'). Expect compares values strictly, so integer 5 != string '5', causing test failure.Final Answer:
The expected value should be an integer, not a string -> Option CQuick Check:
Type mismatch in expect causes failure [OK]
- Using string instead of int for length
- Forgetting to define test description
- Misusing expect function parameters
You want to test a function that returns a list of even numbers from a given list. Which test code correctly verifies this behavior?
List<int> filterEven(List<int> nums) => nums.where((n) => n % 2 == 0).toList();
// Which test is correct?Solution
Step 1: Understand filterEven function and check expected lists
It returns a list of even numbers from input list. Only test('filterEven test', () { expect(filterEven([1,2,3,4]), [2,4]); }); expects the correct even numbers [2,4]. Others expect incorrect lists.Final Answer:
test('filterEven test', () { expect(filterEven([1,2,3,4]), [2,4]); }); -> Option AQuick Check:
Expected even numbers list = test('filterEven test', () { expect(filterEven([1,2,3,4]), [2,4]); }); [OK]
- Expecting odd numbers instead of even
- Including all numbers without filtering
- Mixing even and odd numbers in expected list
Ready to prove it?
Pass the test and this pattern moves unknown
