How Not To Write Golden Master Tests

I recently encountered a code base in which someone had applied the Golden Master technique, but done so in a way I find risky, so I wanted to warn you against letting this happen in your code. This code exhibits a bad idea that probably started out as a good idea or seemed like a good idea at the time. This makes it plausible-but-risky, and this is where a lot of legacy code comes from.


This is a companion discussion topic for the original entry at http://blog.thecodewhisperer.com/permalink/how-not-to-write-golden-master-tests

That's a great tip. I've never considered adding a main method to one of my tests. Seems obvious after reading this.

Good idea. Team I was on a couple years ago had this exact problem... and a gold-master file *was* overwritten - didn't notice for a while unfortunately.

And from where, when and how would you call the main() function?

I would only run main() when I wanted to generate a new Golden Master. The most common case involves the output changing (a test run fails) and then deciding that the new output is OK. (This is generally how to use Golden Master.)

Since I don't do this often, I don't need to make this part of any automated build or test run.

I have another article here that explains the Golden Master technique in more detail. http://blog.thecodewhispere...

Finally, I built this main() so that it didn't even need a command line argument, so you run it like any other command line application, either from a shell or using the Run command in your favorite IDE.

I know. I felt the same way when I first saw it. It's funny how we limit our own thinking.

A key reason to commit the golden master to version control. When it changes, you notice.

Thanks much JB!

Another approach would be to use approval tests: https://github.com/approval...

Yes. I like the general mechanism of Approval Tests, although I very slightly prefer "generate golden master" and "run the test" as separate activities with different ways to launch. (Just an expression of my desire for control, I guess.) I imagine the difference is so small that I would get quite comfortable with both ways of thinking about it. (If a file named X happens to be there, then we know the expected result, and if not, then we don't and the test can't pass.)