SOFTWARE ENGINEERING / WEEK 09

Test the behavior.
Build confidence.

48 lessons with 47 illustrations cover test design, pytest, TDD, integration and system testing, followed by revision questions and practice problems.

48 study sections47 illustrations2 resources
Begin with software testing
Software testingSOFTWARE TESTING & QUALITY

Study sections

Follow the order or choose a section
01

Why Do We Test Software?

Even simple errors such as integer overflow can crash entire systems. Airport system failures and spacecraft crashes show why software…

1 illustrations
02

Errors Can Crash Entire Systems

Integer overflow, an airport system failure and a spacecraft crash illustrate the consequences of software errors.

1 illustrations
03

Building the Right Thing vs Building It Right

Earlier weeks focused on finding the right requirements and communicating with clients: building the right thing.

1 illustrations
04

Basic Activities in Testing

Provide inputs; check the output; see whether it matches expectations; locate the error; identify its cause; fix the error; test again.…

1 illustrations
05

What Is a Test Case?

A test case is the triplet [I, S, R]. I is the data input to the program; S is the state of the program at which that data is input; R…

1 illustrations
06

Test Case vs Test Suite

A test suite is the set of all test cases designed to test a given program.

1 illustrations
07

Why Design Test Cases?

Random tests do not guarantee that most errors will be found. Consider the faulty minimum calculation: if (x < y) min = x; else min = x…

1 illustrations
08

Can We Test Every Input?

The input domain is too large to test exhaustively. Design a minimal test suite in which each test case detects a different type of err…

1 illustrations
09

Levels of Software Testing

Unit testing tests individual functions or units. Integration testing incrementally combines units and tests them after each step.

1 illustrations
10

Unit vs Integration Testing

Unit testing checks an individual unit. Integration testing detects errors at module interfaces, including parameter passing and commun…

1 illustrations
11

Alpha vs Beta Testing

Alpha testing is performed by the test team within the organisation. Beta testing is performed by a select group of customers on the fu…

1 illustrations
12

Acceptance Testing

The customer checks whether the system meets the requirements or user stories and decides whether to accept delivery.

1 illustrations
13

Black-box Testing

Black-box testing is also called functional testing. Treat the program as a black box and design tests by examining input and output va…

1 illustrations
14

Equivalence Class Partitioning

Partition the input domain into equivalence classes: the program behaves similarly for every input in a class. Select one representativ…

1 illustrations
15

Equivalence Classes for min(x, y)

For x < y, min(3, 8) returns 3. For x > y, min(9, 4) returns 4. For x = y, min(5, 5) returns 5. Each pair represents one equivalence cl…

1 illustrations
16

Equivalence Classes for isPrime(num)

isPrime(num) takes a positive integer. Its equivalence classes are prime numbers, which should return True, and non-prime numbers, whic…

1 illustrations
17

Boundary Value Analysis

Programmers may confuse for i in range(n) with range(n-1), or < with <=. Examine values at the boundaries of equivalence classes. For i…

1 illustrations
18

Boundary Cases for is_prime()

For isPrime(num), 0 and 1 are boundary values and are not prime. The values 2 and 3 are prime.

1 illustrations
19

Designing Black-box Tests

From requirements or specifications, identify input and output structures and values. Identify equivalence classes. Choose one represen…

1 illustrations
20

White-box Testing

White-box testing is also called structural testing. Analyse the structure of the program using heuristics such as coverage. Test cover…

1 illustrations
21

Black-box vs White-box Testing

Black-box testing is functional testing: use input and output values. White-box testing is structural testing: examine the program stru…

1 illustrations
22

Branch Coverage

Branch coverage requires every branch to be taken at least once. For min(x, y), one test has x < y and another has x >= y.

1 illustrations
23

Testing Both Branches of min(x, y)

With (3, 8), x < y is True and the result is 3. With (9, 4), x < y is False and the result is 4.

1 illustrations
24

Branch Coverage: is_prime()

The test suite {0, 2, 3, 4} guarantees branch coverage for the lecture’s isPrime code.

1 illustrations
25

Multiple Condition Coverage

In multiple condition coverage, each component of a composite condition takes True and False values. For num == 0 or num == 1, the inpu…

1 illustrations
26

Branch vs Multiple Condition Coverage

For if (num < 50 || num > 150), {49, 100} achieves branch coverage. The set {49, 100, 200} achieves multiple condition coverage. Multip…

1 illustrations
27

Control-Flow Graph

In a control-flow graph, nodes represent line numbers and an edge means control flows from one line to another. For path coverage, the…

1 illustrations
28

Independent Paths in is_prime()

The lecture’s independent paths are {1, 2, 3} for 0 or 1; {1, 2, 4, 7} for 2; {1, 2, 4, 5, 6} for the non-prime 4; and {1, 2, 4, 5, 4,…

1 illustrations
29

Cyclomatic Complexity

Cyclomatic complexity is the number of decision and loop statements plus 1. Here it is 3 + 1 = 4: four independent paths, requiring at…

1 illustrations
30

Why Unit Testing Matters

Other modules may not be ready when a module is written. Testing a unit separately also makes debugging easier because the module conta…

1 illustrations
31

Unit Testing During Development

WHEN: during coding of the module, rather than waiting for the testing phase. WHO: the person writing the code. WHAT: design unit test…

1 illustrations
32

Unit Testing Environment

The module under test may access non-local data and call functions that are not yet available, such as function_1, function_2 and funct…

1 illustrations
33

What Is a Stub?

A stub is a dummy procedure with the same input/output parameters as the function the module calls, with highly simplified behaviour.

1 illustrations
34

What Is a Driver?

A driver contains the non-local data structures accessed by the module under test and calls the module. A stub stands in for a function…

1 illustrations
35

Python Unit Testing Tools

unittest is part of Python’s built-in standard library. pytest is popular and minimises boilerplate code. Install it with pip install p…

1 illustrations
36

How pytest Finds Tests

The lecture names test files with the test_ prefix, such as test_seller_portal.py, and test functions with the test_ prefix. Without th…

1 illustrations
37

Understanding assert

An assertion checks a condition: assert actual == expected. If the comparison is False, Python raises AssertionError.

1 illustrations
38

Seller Portal Inventory

The lecture uses a Product class and a SellerInventory class. seller_inventory_map stores {product_id: count}. add_items_to_inventory a…

1 illustrations
39

Testing Inventory Addition

Add 10 items of product P1: seller_inventory_map[P1] is 10. Add 10 more: the expected count is 20.

1 illustrations
40

A Unit Test Catches a Bug

Forgetting the + replaces the count instead of adding to it. The second addition produces 10 instead of 20, causing AssertionError: 10…

1 illustrations
41

Testing Inventory Deletion

Add 10 items and delete 5. The expected remaining count is 5.

1 illustrations
42

pytest Fixtures: Reusable Setup

A fixture method such as inventory() initialises the object once for use in all test cases in the lecture example.

1 illustrations
43

Testing Expected Exceptions

Add 10 items and attempt to delete 20. A test using pytest.raises(ValueError) passes when ValueError is raised. If the test deletes onl…

1 illustrations
44

Traditional Development vs TDD

Traditionally, tests are written after the code. Test-driven development, used especially in Agile, writes tests first and drives desig…

1 illustrations
45

Red–Green–Refactor

RED: express the requirement as a test, run it and see it fail. GREEN: write the minimum code needed to pass. REFACTOR: make the code m…

1 illustrations
46

Building is_prime() with TDD

First test a non-prime such as 4: expect False. The test fails; add a loop that returns False when num % i == 0, then it passes.

1 illustrations
47

Testing is_prime(): Important Cases

The important cases are is_prime(5) → True, is_prime(4) → False, is_prime(2) → True, is_prime(1) → False and is_prime(0) → False.

1 illustrations
48

Integration and System Testing

Begin integration testing when a few or all modules have been unit-tested. The objective is to detect interface errors such as paramete…

0 illustrations

48 lessons cover testing, functional and structural techniques, unit testing, pytest, TDD, integration and system testing. Follow them with the mini revision questions and practice problems.