Skip to content
tezvyn:
Menu

Unit Testing in Dart: Verify Your Logic in Isolation

Source: docs.flutter.devEasyHow cards are made

A unit test is a microscope for your code. It checks a single function or class in isolation, ensuring it behaves correctly without worrying about the UI or network. Use it for business logic, not for testing rendered widgets or API calls.

Why it exists

To catch bugs early and cheaply. Running an entire app to check one small piece of logic is slow and inefficient. Unit tests provide a fast, automated way to verify that individual components work as expected before they are assembled into a larger system, giving you confidence to refactor and add features.

The mental model

A unit test is a conversation with a single piece of your code. You provide it with some input and ask, "Given this, do you produce the expected output?" It's about isolating a "unit"—a function, method, or class—from all its dependencies (like UI, database, or network) to test its internal logic exclusively.

How it works

In Dart, you use the test package. You write test files (typically ending in _test.dart) that contain individual test() functions. Inside each test, you set up a scenario, call the code you want to test, and then use an expect() function to assert that the actual result matches the expected outcome. For example: expect(calculator.add(2, 2), 4);.

When to use it

Use unit tests for pure logic. This includes data model classes, utility functions (like a date formatter), business logic in controllers or services, and algorithms. Anything that can be tested without rendering UI or making a network call is a prime candidate. They should be fast enough to run on every save.

When not to use it

Do not use unit tests for things that require a running Flutter engine or external systems. For testing UI rendering and user interaction, use Widget Tests. For testing end-to-end flows that involve databases or APIs, use Integration Tests. A unit test that needs a network connection is not a unit test.

One canonical example

Imagine a simple Counter class that can increment and decrement. A unit test would first create an instance of Counter. Then, it would call the increment() method. Finally, it would use expect(counter.value, 1) to verify the state changed correctly. You'd write separate tests for decrementing, resetting, and handling edge cases like not going below zero.

Interview question

According to the principles of unit testing in Dart, what is its primary purpose?

  • a.To confirm that all external API calls are handled without errors.
  • b.To verify that UI components render as expected across different screen sizes.
  • c.To ensure the entire application functions correctly from a user's perspective.
  • d.To isolate and validate the internal logic of a single function or class.Correct
Why?

Unit tests are specifically designed to check a single component's logic in isolation, free from external dependencies like UI, network, or databases. Options A, B, and D describe concerns addressed by integration or widget tests, not unit tests.

Just read this? Test yourself on what you have been reading.

Read the original → docs.flutter.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on dart — each one lists the topics its interview covers.

See open roles