What are the best practices to unit test a webserver?

When it comes to database testing, I have come to be fond of using 'drivers' for database access, of which one of the drivers is a pure in-memory implementation. That one gets used in testing. It's easy to setup, teardown and introspect, while also being fully conformant to the interface. Mocking may still be useful to test the exceptional cases, but for regular program flow I have found this approach to be much more tenable.

The best part is that the driver is not limited to testing. It can also be used to allow your program to run without dependencies, which turns out to be pretty useful. For example, sometimes other people in the company want to run the software on their own machine, for a demo or what have you, and being able to send off a binary that runs straight out of the box makes that process a lot easier.

As a corollary to that, it is easier to manipulate memory structures than table schemas. This is quite useful when starting development work and determining how you want to structure your data. Once you are happy with the interface and how it works within your application, then you can update your sql (or whatever provider you are using) 'driver' to match.

/r/golang Thread Parent