0
0
Fluttermobile~20 mins

Unit testing in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 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.