Unit test and unit code design
Remember that first example – FizzBuzz. It took values from the command line and spat results out to the screen. To test it, we would have to test the entire system as a system. To automate that, we could do… something. It is possible, for example, to write a program that calls FizzBuzz from the command line, passing in inputs from a file, writing the results to a file, and comparing actual to expected results. That is what most customer-facing test automation looks like. That kind of automation is clunky, awkward, and painful.
Or, using the hexagonal model, we can break the line into independent components and test them. You saw this in the FizzBuzz example, where we essentially had three elements.
The main routine is as follows:
- Accept input, call
get_total_result
, and print it. Get_total_result
; loop from1
to input,calc_individual_result
, add to the total, and return it.calc_indvidual_result
; the business logic...