Unit and Integration Testing

Unit testing verifies individual modules in isolation using mocks and stubs, while integration testing verifies that combined modules interact correctly; together they catch defects at increasing scope.

Key formulas & points

Skim these first — then read the full notes below.

  • AAA pattern: Arrange, Act, Assert
  • A mock verifies interaction; a stub returns canned data
  • Integration testing exposes interface mismatches

Topic details

Introduction

This topic covers the lower levels of the test pyramid. You write isolated unit tests following the Arrange-Act-Assert pattern, use test doubles (mocks and stubs) to isolate dependencies, and choose integration strategies to combine modules while localising failures.

Key relations & formulas

Formulas (Indian textbook notation)

  • aunittestisolatesonemoduleusingmocksorstubsa unit test isolates one module using mocks or stubs

Formulas (Indian textbook notation)

  • integrationstrategies:bigbang,topdown,bottomup,sandwichintegration strategies: big-bang, top-down, bottom-up, sandwich

Formulas (Indian textbook notation)

  • testcoverage=executedstatementstotalstatementstest coverage = executed \frac{statements}{total} statements

Notation and sign conventions

Relation 1 —
aunittestisolatesonemoduleusingmocksorstubsa unit test isolates one module using mocks or stubs

Formulas (Indian textbook notation)

  • aunittestisolatesonemoduleusingmocksorstubsa unit test isolates one module using mocks or stubs
Write this relation with symbols exactly as in Kaner Testing — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 2 —
integrationstrategies:bigbang,topdown,bottomup,sandwichintegration strategies: big-bang, top-down, bottom-up, sandwich

Formulas (Indian textbook notation)

  • integrationstrategies:bigbang,topdown,bottomup,sandwichintegration strategies: big-bang, top-down, bottom-up, sandwich
Write this relation with symbols exactly as in Kaner Testing — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 3 —
testcoverage=executedstatementstotalstatementstest coverage = executed \frac{statements}{total} statements

Formulas (Indian textbook notation)

  • testcoverage=executedstatementstotalstatementstest coverage = executed \frac{statements}{total} statements
Write this relation with symbols exactly as in Kaner Testing — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.

Concept in depth

Unit tests pin down a single module’s behaviour, and isolation is what makes them fast and precise — dependencies are replaced by test doubles so a failure points to the unit under test, not its collaborators. A stub feeds the unit predetermined inputs, while a mock additionally checks that the unit called its collaborators correctly. Integration testing then verifies the wiring between modules, where mismatched assumptions about interfaces surface; the strategy (top-down using stubs for lower modules, bottom-up using drivers for upper ones, or sandwich combining both) trades off how early different parts are exercised. Coverage measures how much code the tests execute, a necessary but not sufficient indicator of quality.

Assumptions and validity limits

State assumptions explicitly before using any relation for unit and integration testing — steady state, uniform properties, linear elastic material, ideal gas, incompressible flow, etc., as applicable.
Wrong assumptions invalidate the entire solution even when the formula is correct. In Software Testing viva and GATE descriptive questions, listing valid assumptions often earns separate marks.

Step-by-step problem approach

1. Read the question and list given data with SI units (common in Software Testing papers).
2. Draw a neat labelled diagram where applicable — examiners in Indian universities award diagram marks even when arithmetic slips.
3. Identify which relation from this topic applies to unit and integration testing.
4. Use equation 1:
aunittestisolatesonemoduleusingmocksorstubsa unit test isolates one module using mocks or stubs
.
5. Use equation 2:
integrationstrategies:bigbang,topdown,bottomup,sandwichintegration strategies: big-bang, top-down, bottom-up, sandwich
.
6. Substitute values, compute, and verify units and sign (direction).
7. State conclusion in one line — e.g. safe/unsafe, stable/unstable, feasible/infeasible.

Applications & exam relevance

Unit and Integration Testing appears in QA teams and CI pipelines. In Indian it software curricula this topic is tested because it connects theory to verification and validation.
GATE and semester exams often combine unit and integration testing with earlier units — revise prerequisites before attempting mixed problems.
Industry interview panels sometimes ask: "Where did you use unit and integration testing?" — answer with a lab, mini-project, or plant visit example if possible.

Common mistakes in exams

Students confuse mocks (verify interactions) with stubs (provide data), write unit tests that actually hit real dependencies (making them integration tests), and treat 100% coverage as proof of correctness. Forgetting that coverage measures execution, not assertion quality, is common.

Quick revision checklist

Before attempting unit and integration testing problems, confirm you can:
1. AAA pattern: Arrange, Act, Assert
2. A mock verifies interaction; a stub returns canned data
3. Integration testing exposes interface mismatches
Revise the solved examples in Kaner Testing — Standard reference and one previous-year GATE or university paper for this unit.

Worked examples

Try the problem first — open the solution when you are ready to check.

Mock versus stub

Problem

A test needs a payment gateway to return "approved" and must also confirm the gateway was charged exactly once. Which double(s) are needed?

Solution

A stub provides the "approved" response; a mock verifies the charge method was called exactly once. Here a mock suffices since it can both return canned data and assert the interaction.

Conceptual check — Unit and Integration Testing

Problem

In a Software Testing semester or GATE paper you are asked: "State the main assumption, the governing relation, and one practical consequence of unit and integration testing." What should a complete answer include?

📖 Standard books (India)

  • Kaner TestingStandard reference

    Read: Syllabus unit

    Referenced in Indian B.Tech syllabus