Bird
Raised Fist0
Fluttermobile~20 mins

Unit testing in Flutter - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Unit Testing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
What is the output of this Flutter unit test?
Consider this simple Dart function and its unit test. What will the test output be when run?
Flutter
int add(int a, int b) {
  return a + b;
}

void main() {
  test('add returns correct sum', () {
    expect(add(2, 3), 5);
  });
}
ATest fails because add returns 6 instead of 5.
BTest throws a runtime exception because add is undefined.
CTest fails due to a syntax error in the test code.
DTest passes successfully with no errors.
Attempts:
2 left
💡 Hint
Check what the add function returns and what the expect statement checks.
📝 Syntax
intermediate
2:00remaining
Which option contains a syntax error in this Flutter unit test?
Identify the option that will cause a syntax error when running this Flutter unit test code.
Flutter
void main() {
  test('string length test', () {
    String text = 'hello';
    expect(text.length, 5);
  });
}
A
void main() {
  test('string length test', () {
    String text = 'hello'
    expect(text.length, 5);
  });
}
B
void main() {
  test('string length test', () {
    String text = 'hello';
    expect(text.length, 5);
  });
}
C
void main() {
  test('string length test', () {
    String text = 'hello';
    expect(text.length == 5);
  });
}
D
void main() {
  test('string length test', () {
    String text = 'hello';
    expect(text.length, 6);
  });
}
Attempts:
2 left
💡 Hint
Look for missing semicolons or wrong expect usage.
❓ lifecycle
advanced
2:00remaining
What happens if you forget to call setUp() in a Flutter test suite?
In a Flutter test file, you have a setUp() function that initializes some variables before each test. What is the effect of not calling setUp() explicitly?
AsetUp() is automatically called before each test, so no effect.
BTests will fail because variables are not initialized properly.
CTests will run but with stale or null variables causing unpredictable results.
DTests will throw a runtime error immediately.
Attempts:
2 left
💡 Hint
Think about how the test framework manages setUp functions.
🔧 Debug
advanced
2:00remaining
Why does this Flutter unit test fail unexpectedly?
Given this test code, why does the test fail even though the function seems correct? int multiply(int a, int b) { return a * b; } void main() { test('multiply test', () { expect(multiply(2, 3), 5); }); }
AThe multiply function has a syntax error causing failure.
BThe test framework is not imported, so test() is undefined.
CThe expected value 5 is incorrect; multiply(2, 3) returns 6.
DThe test fails because multiply returns a string instead of int.
Attempts:
2 left
💡 Hint
Check the expected value against the actual function output.
🧠 Conceptual
expert
2:00remaining
What is the main benefit of using mocks in Flutter unit testing?
Why do developers use mock objects in Flutter unit tests?
ATo speed up the app by replacing real code with faster mock code in production.
BTo simulate dependencies and isolate the unit under test for reliable testing.
CTo automatically generate UI components during tests without manual coding.
DTo avoid writing any test code by using prebuilt mock tests.
Attempts:
2 left
💡 Hint
Think about how mocks help when your code depends on external services or complex objects.

Practice

(1/5)
1.

What is the main purpose of unit testing in Flutter?

easy
A. To check small parts of code for bugs early
B. To design the user interface
C. To deploy the app to the store
D. To write documentation for the app

Solution

  1. 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.
  2. Final Answer:

    To check small parts of code for bugs early -> Option A
  3. Quick Check:

    Unit testing = check small code parts early [OK]
Hint: Unit tests check small code parts, not UI or deployment [OK]
Common Mistakes:
  • Confusing unit testing with UI design
  • Thinking unit tests deploy the app
  • Believing unit tests write documentation
2.

Which of the following is the correct syntax to write a simple test in Flutter?

?
easy
A. expect('description', test(() => 1 + 1 == 2));
B. test('description', () { expect(1 + 1, 2); });
C. test 'description' { expect(1 + 1 == 2); }
D. test(description, expect(1 + 1, 2));

Solution

  1. 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.
  2. Final Answer:

    test('description', () { expect(1 + 1, 2); }); -> Option B
  3. Quick Check:

    Correct test syntax = test('description', () { expect(1 + 1, 2); }); [OK]
Hint: Remember test() takes a description and a function with expect() inside [OK]
Common Mistakes:
  • Missing parentheses or curly braces
  • Swapping test and expect arguments
  • Using wrong function call syntax
3.

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.

medium
A. All tests fail
B. First test fails, others pass
C. Second test fails, others pass
D. All tests pass successfully

Solution

  1. 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.
  2. Final Answer:

    All tests pass successfully -> Option D
  3. Quick Check:

    Correct add results = all tests pass [OK]
Hint: Check function output matches expected values exactly [OK]
Common Mistakes:
  • Assuming negative numbers cause failure
  • Misreading expected values
  • Ignoring zero as valid input
4.

Identify the error in this Flutter unit test code:

test('string length test', () {
  var str = 'hello';
  expect(str.length, '5');
});
medium
A. The test description is missing
B. The variable str is not defined
C. The expected value should be an integer, not a string
D. The expect function is used incorrectly

Solution

  1. 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.
  2. Final Answer:

    The expected value should be an integer, not a string -> Option C
  3. Quick Check:

    Type mismatch in expect causes failure [OK]
Hint: Match expected value type exactly with actual value type [OK]
Common Mistakes:
  • Using string instead of int for length
  • Forgetting to define test description
  • Misusing expect function parameters
5.

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?
hard
A. test('filterEven test', () { expect(filterEven([1,2,3,4]), [2,4]); });
B. test('filterEven test', () { expect(filterEven([1,2,3,4]), [1,3]); });
C. test('filterEven test', () { expect(filterEven([1,2,3,4]), [2,3]); });
D. test('filterEven test', () { expect(filterEven([1,2,3,4]), [1,2,3,4]); });

Solution

  1. 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.
  2. Final Answer:

    test('filterEven test', () { expect(filterEven([1,2,3,4]), [2,4]); }); -> Option A
  3. Quick Check:

    Expected even numbers list = test('filterEven test', () { expect(filterEven([1,2,3,4]), [2,4]); }); [OK]
Hint: Expect only even numbers in the output list [OK]
Common Mistakes:
  • Expecting odd numbers instead of even
  • Including all numbers without filtering
  • Mixing even and odd numbers in expected list
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard