Reduce duplicate code

Some code had been duplicated (cut-and-pasted) between tests. This
change merges them into a common "tools" name space for better
maintainability.