Another one in response to a question on Quora, actually two questions that Iâve combined into one for the âheadlineâ of this blog post:
This is really two questions.
First is: âHow can I write unit test cases to ensure the good health of my application architecture?â
The short and snarky answer is: âyou donâtâ. Why not? Because youâre mixing levels here. Think of âarchitectureâ as a diagram with boxes (possibly containing other boxes) and arrows and such. Unit tests test functionality â one piece of what a given box does. The âunitâ part implies that itâs a small piece, and is usually taken to mean one indivisible piece, analogous to an atom. âArchitectureâ is how the boxes fit together â think more of the arrows. At the very least, youâre probably thinking more of integration tests, which test mainly a particular arrow, or maybe feature or end-to-end tests.
The second is: âShould these testcases be written by an architect or a lead developer?â
That depends on whether you really mean unit tests after all, or some other kind. Generally speaking, unit tests per se (i.e., the actual runnable tests) should be written by the developer implementing them. Otherwise, itâs forcing a particular API on that developer, and thatâs an implementation detail that nobody else should be dictating. (Unless the whole task in question is to develop a function with that specific API.) Unit test cases, in the sense of test descriptions (like âif I give the fizzbuzz function a number thatâs a multiple of three but not five, it should pass back the string âfizzââ), may be written by someone overseeing that dev. In both cases, though, unless that dev is very junior and needs such handholding, that borders on micromanagement.
On the other claw, if you really mean integration or end-to-end tests, yes, those might be written by an architect or lead dev, depending how âdown in the weedsâ they are (both the tests and the people). Someone even higher away from the weeds might write some end-to-end tests. Frex, if the system is a geolocation name lookup system they might specify something like âif I give the system the lat/long coordinates for my house, it should return my addressâ, or maybe substitute some well-known landmark like 10 Downing Street, London. That might be in code, or something actually interpreted by a test-runner such as Cucumber and its Gherkin language, but more likely it will be in plain human-language, to be interpreted by the developers.
This is actually why I advocate developers writing such test cases themselves, especially in the âGiven/When/Thenâ format, for approval by the stakeholders, to ensure that we understand their needs correctly. Having a test case rejected/corrected can save us hours of implementing code that does the wrong thing (and a test for it).
UPDATE:
I have just been informed that Part 1 is no longer true! Check out https://www.archunit.org/ â okay, it still doesnât seem to be what I would call âunit testsâ, so much as âenvironment testsâ, but itâs a step in that direction. Currently meant for Java, with ports already working for C#/.NET. Credit to Sayf Jawad.