📊 We use cookies & analytics to improve your experience. Learn more
Latest

Software Testing — Manual and Automation Module 4 · Lesson 11 of 15

0% Exit to course

Module 4 · Testing behind the screen Lesson 11 of 15

Database and test-data checks

Learning objective

Some rules cannot be seen from outside: a status that must move through an allowed set, an order total that must equal its lines. Query the database and assert the rule directly.

The concept

Point automated database tests at a throwaway database seeded by the test itself, never at production - a test that writes to live data is a test that will one day cause the incident.

Test data should be created by the test, not borrowed from whatever was in the table, or the suite passes on Monday and fails on Friday for no visible reason.

SQL
-- every application must point at a real job
SELECT a.id FROM applications a
LEFT JOIN jobs j ON a.job_id = j.id
WHERE j.id IS NULL;

-- every order total must equal the sum of its lines
SELECT o.id, o.total, SUM(l.price) AS line_total
FROM orders o JOIN order_lines l ON l.order_id = o.id
GROUP BY o.id, o.total
HAVING o.total <> SUM(l.price);

Worked example

A query counting orders whose total is not the sum of their line items should return zero rows - a one-line test for a rule no UI test can prove.

Your practice task

Write two SQL checks: one for an orphan row, one for a total that does not match its lines.

Do this before moving on. Reading a solution teaches the shape of it; typing it teaches you.

Key takeaway

Point automated database tests at a throwaway database seeded by the test itself, never at production - a test that writes to live data is a test that will one day cause the incident.

Create free account or sign in

Almost there

15 lessons left of 15

Finish every lesson to unlock the certificate. Each one ends in a practice task — do them and the final project brief writes itself.

Next recommended course Full-Stack Web Development Bootcamp