Flutter Unit Testing: Understanding setUp() and setUpAll()

Dhrumil Shah
FlutterFlakes
Published in
3 min readJul 21, 2024

Flutter’s unit testing framework provides two very powerful functions for setting up test environments: setUp() and setUpAll(). Both are crucial in writing clean, effective, and efficient tests. But when should you use each, and what are the differences? Let’s dive in…

The Dynamic Duo of Testing: setUp() and setUpAll()

Imagine you’re a superhero preparing for battle. You wouldn’t wear your cape just once, right? Similarly, setUp() is like putting on your cape before each fight — it’s called before every single test runs. It’s perfect for initialising variables or state that should be reset before every test case.

Now, setUpAll() is like building your secret lair. You do it once, and it’s there for all your battles. It’s called once at the beginning of the test run and is ideal for expensive operations that need to happen just once, like database initialisation.

setUp()

The setUp() method is called before each test in a test suite. This means if you have multiple tests, setUp() will be executed once for each test. This is particularly useful when each test requires a fresh instance of some objects or needs to reset the state.

void main() {
setUp(() {
// Code to set up test dependencies
});

test('Test 1', () {
// Test code
});

test('Test 2', () {
// Test code
});
}

In the example above, the code inside setUp() runs before Test 1 and again before Test 2. This ensures that each test starts with a clean state, reducing the chances of flaky tests due to leftover state from previous tests.

setUpAll()

The setUpAll() method, on the other hand, is called only once before any of the tests run. This is ideal for setting up resources that are expensive to create and can be shared across multiple tests.

void main() {
setUpAll(() {
// Code to set up shared dependencies
});

test('Test 1', () {
// Test code
});

test('Test 2', () {
// Test code
});
}

In this example, the code inside setUpAll() runs only once before any of the tests are executed. This can significantly speed up your test suite if you have expensive setup operations that do not need to be repeated for each test.

When to Use setUp()

  • Isolation: When each test needs a clean start. Think resetting a database, clearing cache, or creating new object instances.
  • Test Independence: Ensuring tests don’t mess with each other. If a test changes a global state, setUp() can reset it before the next test.

When to Use setUpAll()

  • Expensive Operations: When setup involves costly operations like database connections, mock servers, or initialising shared resources.
  • Shared State: When multiple tests can share the same setup. For instance, loading a big config file or setting up shared mocks.

Practical Example

Let’s bring this to life with a practical example involving a mock database:

import 'package:flutter_test/flutter_test.dart';

class MockDatabase {
bool isConnected = false;

void connect() {
isConnected = true;
}

void disconnect() {
isConnected = false;
}
}

void main() {
late MockDatabase database;

setUpAll(() {
database = MockDatabase();
database.connect();
print('Database connected');
});

setUp(() {
// Reset the state if necessary
});

test('Database should be connected', () {
expect(database.isConnected, isTrue);
});

test('Another test that uses the database', () {
expect(database.isConnected, isTrue);
});
}

In this example:

  • setUpAll() connects to the mock database once before all tests.
  • Each test assumes the database is connected and focuses on its specific checks.

Wrapping Up

Knowing when and how to use setUp() and setUpAll() can supercharge your unit testing game. By setting up your test environment right, you ensure your tests run faster and smoother, making your code more maintainable and robust. As you continue your Flutter journey, mastering these methods will be a key skill in your testing toolkit.

Happy testing, and may your code be bug-free!

If you like this share your love by a clap or applause. If you are facing any specific problem, share it in the comment and I will try my best to solve it.

Feel free to connect me over Twitter & LinkedIn.

--

--

Dhrumil Shah
FlutterFlakes

Senior Mobile Application Developer at HighLevel | GDE for #Flutter & #Dart | Co-Organiser of #GDGAhmedabad | Founder of @Flutter_Flakes .