Can you catch a wrong rail fare without opening the booking screen?
You’re testing a booking app for a made-up rail service in Galway. At exactly ten kilometres, the fare should go up. Could you catch a mistake at that distance without opening the booking screen?
Think back to your test plan. You use the fare rules to decide what to expect. The input is a whole number of kilometres, zero or more. Zero costs nothing, one to nine costs two euro, and ten or more costs three euro.
Watch the test send ten straight to the fare function. The function returns a number, and the test compares it with three, the expected fare. You can check that calculation without loading a screen or looking up a booking in a database.
You want to check the zero fare by calling the calculation directly. Which option calls the fare function with the right argument for that case? Pick one.
A unit test checks one piece of code in isolation against its contract, the inputs and results it promises. A function test checks a required behaviour, such as quoting the right fare, so these tests can overlap. A system test checks the integrated app, for example by completing a booking.
A false assertion stops this Python run. First, check that fare of zero equals zero. It passes. Next, check that fare of ten equals three. It fails. In the Python shell, call fare with ten to inspect the actual return. You get two euro instead of three.
You’ve tested the first distance in the higher band. Now test the distance just below it. Which assertion checks the fare at nine kilometres against the stated rules? Pick one.
You might be tempted to change the expected fare at ten kilometres to two euro. That would make the assertion pass while leaving the fault in place. Keep three as the expected result, because the fare rules say the higher band starts at ten.
Keep the zero rule. Change greater than ten to greater than or equal to ten so ten returns three. Rerun both tests and add the check for nine. All three pass. Keep them to catch future mistakes. A negative distance still returns two, so add input validation next.
If a question says, ‘Describe one unit test you would carry out,’ give one precise case. Name the fare function, state that you pass in ten kilometres, and say you expect it to return three euro. Describing the whole booking would leave the calculation you tested unclear.