Tripwire Tests
TL;DR
A Tripwire Test is a way of ensuring that developers acknowledge or do something as part of a change. The test should fail when a change is made that requires additional changes or a some standard operating procedure (SOP) to be followed.
Use Cases
- Ensuring assistant / complimentary changes are completed
Concrete Example
Imagine you are building an text editor that supports multiple filetypes. For each supported file type, you want to make sure that a filetype icon is added in a separate image repository.
// file: FileProcessor.java
public Set<String> supportedFileTypes = Set.of(
".txt",
".java",
".py",
".csv"
)
Suppose you added .csv
as as a new supported filetype. The below test would then fail, prompting you to follow SOP and make sure that you've added a filetype icon.
// file: FileProcessorTests.java
@Test
public void tripwire_fileIconSupport() {
/**
* TRIPWIRE
* When adding new filetypes, make sure that
* you also add a new icon to the image
* repository.
*
* See: http://example.com/new_filetype_sop
*/
Set<String> acknowledgedFiletypes = Set.of(
".txt",
".java",
".py"
);
assertEquals(acknowledgedFiletypes, acknowlFileProcessor.supportedFileTypes,
"TRIPWIRE: Make sure you follow http://example.com/new_filetype_sop")
}
Questions
Why not just add documentation to FileProcessor.java
?
- A lot of people don't bother reading the documentation (or keeping it up to date).
Why not just test directly against the image repository?
- Ideally, you could write a test that verifies the image is contained in the repository whenever a new filetype value is added, however, in practice, this can be difficult for a number of reasons.
- It can introduce extra coupling, in this example potentially forcing you to implicitly verify the storage mode.
- It can be difficult to integrate with some external services (eg. lack of programmatic access, different ownership).
Summary
Tests don't have to exist just for the purpose of catching regressions, they can be used as a means of mechanising and "enforcing" documented SOP.
References / Footnotes
Note: I vaguely remember seeing a similar idea on a mailing list a couple years ago, I can't remember whether it referenced an existing article or the idea itself. A quick Google search returned few results.