Unit Tests vs Integration Tests: Scope, Dependencies and a Worked Example
Company: Canonical
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
What is a unit test, and what is an integration test? Explain how they differ, and show what each would look like for the same piece of code.
```hint One function, two tests
Picture a function that loads records from a database and computes a result from them. Ask which test would still pass with the database switched off, and which one would catch a misspelled column name.
```
```hint Cost of each
Compare how fast each kind runs, how often it fails for reasons unrelated to your change, and how quickly a failure points you at the broken line.
```
### Clarifying Questions
- Should the examples use a particular language and test framework, or stay language-neutral?
- Should end-to-end tests through the user interface be part of the comparison, or only unit and integration tests?
### What a Strong Answer Covers
- Definitions in terms of scope and of which real dependencies are involved
- Trade-offs in speed, determinism, failure localization and the confidence each kind gives
- The role and the risks of test doubles such as stubs, mocks and fakes
- A concrete example of both kinds of test for the same code
- How to balance the two in a test suite, and where each runs in continuous integration
### Follow-up Questions
- A unit test with a mocked database passed, but production failed because a column was renamed. Which test would have caught it?
- How do you keep integration tests that use a real database fast and independent of each other?
- When would you choose a fake (a working in-memory implementation) over a mock?
Overview: A software engineering fundamentals question asking what unit tests and integration tests are, how they differ, and what each looks like for the same code. It tests understanding of test scope, real versus replaced dependencies, test doubles, and how to balance speed against confidence in a test suite.
Read the full Canonical Software Engineer interview experience this question came from