Tests that cannot pass by accident
You will: Recognise and write tests that cannot go green for the wrong reason.
Builds on Four layers of test, and what each is for.
A test that passes by accident is worse than no test. It is a green light wired to nothing, and it buys confidence you have not earned. This project uses five patterns against it, each learned the hard way.
1. Check the matcher. The test that stops the robot's package
depending on Gazebo uses a regular expression. A regex that matches
nothing would pass forever. So a companion test feeds it names it must
flag — ros_gz, gz_ros2_control — and names it must not — xacro,
controller_manager.
2. Make the input move the output. The SO-101's overlay derives every joint's effort and velocity limit from the servo's datasheet figures. Its test feeds it a toy arm whose servo stalls at 2.0 N·m, derated by half, and asserts that exactly 1.0 comes out, where upstream's file says 10. The numbers are chosen so the answer could only have come from the input. Without that, a generator that ignored its input and passed upstream's value straight through would pass every other test.
3. Use an independent oracle. Forward kinematics is computed twice
— by this project's own matrix code, and by the standard library that
robot_state_publisher uses. A test asserts they agree to a
millimetre. Code tested only against its own arithmetic is merely
self-consistent, including when it is consistently wrong. On this
project's first robot it found a camera mount tilted up at the
ceiling before any camera existed. For the same reason, a few expected positions in the tests
are worked out by hand and stay that way: an expected value
computed by the code under test checks nothing.
4. Run a control. This project's grasp verifier also tries to grasp empty air. A detector that says yes to nothing is not a detector.
5. Notice when a metric stops discriminating. Twice on this project, a summary number quietly stopped measuring anything: a dataset leakage check that passed everything once the data grew, and a heading error that averaged turns in both directions until they cancelled. The signal both times: the metric could no longer tell apart two things known to be different. That means the measurement is broken, not the system.
In the simulation
Two of the lesson's patterns, done with the arm. Work out an expected value by hand before you look, so it can disagree with the model; then read two poses known to differ, and see what a careless summary number would make of them.
Kinematic: joints move at their real speed limit, but there is no gravity, no load and no physics. That is what the steps below are for.
The SO-101 loads as you reach it.
make drive keysIn this lesson
- Before moving: with only
shoulder_panat +90°, which way does the tool go, and roughly how far? Write it down, then turn it past +89° and read the tf view. - Press pick and read the tool's y in the tf view.
- Press place and read y again. It is +0.122 one way and -0.122 the other. Averaged, the two give 0, as if neither pose were off-centre: the summary can no longer tell them apart.
Joints · SO-101 follower
1–5 joint · , . jog · [ ] jaw · - + step · h home · f frames
the keys of make drive
The buttons do what the keys do: hold one to repeat.
Limits from the description; speed capped at 1.7434 rad/s, the servo's datasheet derated. Poses from the profile.
Inspector
Code
The repository's own code behind what you do here appears once you move the arm.
Do it
Read the matcher's own test.
grep -n -B2 -A12 "def test_test_dependencies_are_not_forbidden_by_accident" src/threevn_bringup/test/test_package_dependencies.pyRead why the kinematics are checked against a second implementation.
sed -n 1,60p src/threevn_bringup/test/test_tf_matches_kinematics.pyFind the overlay test that makes the generator prove it reads its input.
grep -n -A13 "def test_limits_are_derived_from_the_servo_not_taken_from_upstream" src/threevn_robot_description/test/test_vendor_overlay.pyNow mutate. In
test_package_dependencies.py, temporarily replace the pattern inFORBIDDENwithr'(?!)'— a regex that never matches anything — and run the suite.make testExpect: The real boundary test still passes — it has nothing to object to — and the matcher's own test fails. That is the companion test doing its job.
Revert it.
git checkout src/threevn_bringup/test/test_package_dependencies.py
When it goes wrong
A test has never failed, in its whole life.
Why: Possibly it cannot.
Fix: Break the code it guards, once, and watch it go red. If it does not, you have found a test to fix.
Terms
- Mutation testing
- Deliberately breaking the code under test to confirm the tests notice. A test that survives a mutation is not testing that code.
- Control case
- A run where the expected answer is "no" — grasping empty air, an image with no target — to prove the check can say no.
- Discriminating metric
- A measure that gives different answers for situations known to differ. One that stops doing so is broken, whatever it reports.
Every term in the course: glossary.
Check yourself
Answer in your head first, then open it.
Why keep hand-computed expected values in a test, rather than computing them with the function being tested?
An expected value computed by the code under test agrees with that code by construction. A hand-computed one is an independent claim that can disagree.
A leakage check passes every dataset you give it. Is that good news?
Not necessarily. Feed it a dataset you know leaks. If it still passes, it has stopped discriminating — which is exactly what happened here when the dataset grew.
Stretch
Pick a test you own at work that has never failed. Mutate the code it guards in the most plausible way a real bug would, and run it. Did it go red? If not, write down what it would need to check.