One of the greatest advantages that one gains from writing good unit tests is that they help to improve code quality. I actually think that unit tests give even more value through improving code quality than what they provide for testing itself. I have seen quite a fair share of code during my career and I’ve never seen a complex system with good structural quality without unit tests on place. Sure, there are good bits every now and there, but usually always something bit messy as well.
The connection between well structured code and unit tests is so strong that actually unit tests can act as a good source in finding structural issues in your production code. As the unit tests by nature are often more easy to understand quickly than understanding the whole system through production code, unit test reviews can be excellent spot to start finding some of the production code issues.
Let me share few of those.
Testing private methods
One code smell that is very easy to spot is testing of the private methods. There are different ways of doing this, but for example in C# you can spot it by looking for usage of PrivateObjects in your test methods.
Private method testing can indicate issue from both perspectives of tests themselves but also in structure of production code.
From the perspective of the test, testing private methods can lead to fragile test. While you are tightly binding your test into implementation detail (something that is encapsulated by the class), you are making a test that gets easily broken through minor changes on detail level of the class even though the logic would not change at all. This can easily add extra work and lead into resistance towards refactoring.
What is more important from the perspective of production code, it is often a code smell. Very usual case is that when you start investigating why there was need for testing of private method you will find out that your class is breaking single responsibility principle. Often there is logic that is chained through private methods and you end up finding logic that would be better to be extracted onto class of its own, which would obey single responsibility and at the same time be testable through public methods.
Other possible thing is that you accessing private property to either override some private value or to read results of test from private property. In first case this is often indication that there are issues with dependency injection and the latter often indicates either issue with single responsibility or encapsulation of the class.
Long test execution paths
Have you ever been debugging through a test code where single unit test goes through about 500 lines of production code only to really test three lines of logic from one private method at the end of the chain? Well, I have.
When reviewing unit tests, I usually always debug them through to see what possible edge conditions we are missing from the tests and how the test really flows through. Sometimes such a debugging session ends up as a marathon run through depths of the production code. And it stinks.
In these cases, the smell most often indicates the issues with following single responsibility principle. and/or issues with dependency injection. Very often it is both.
One way to start thinking about such an issue is to look at the part of the code (those three lines out of 500) that really are the ones that should be under the test and thinking of what preconditions and dependencies only that part needs for being tested. Does it seem like something that is actually doing responsibility of its own and should be part of class of its own? How could you separate it into unit where to you can nicely inject those dependencies?
Often that is just a tip of the iceberg for the needs of refactoring. But at least it is a start where you can start working on restructuring your code bit by bit.
This kind of changes usually also makes your test code quite a bit simpler than they used to be.
Complex test initialization
When bumping into test where there is lots of initialization (in other words, if you are following AAA with your unit tests, the arrange part is complex or bloated) it always rings a bell in my head and I ask from myself: “If test code is so complex and hard to setup, what might the code itself look like?”. The usual answer is “complex, messy and structurally bad”.
What does that indicate most often? Once again, most likely our class under test has multiple responsibilities. This often comes as combination with lack of dependency injection. Due to that we will possibly setup multiple different classes with valid test data to get us run all the way to simple part of logic that we would like to test.
Lack of exception handling
It is not uncommon to run into set of tests that are only testing positive cases, going just through the happy flows when nothing bad happens. In the case where I don’t find a single test that would be testing if exceptions are thrown as they should, I feel a bit of nasty smell in my nostrils coming from the production code. In the best case scenario, only the test set is bad and is only touching the surface of what is should be doing, but too often it is also case there is something fishy in production code.
When the exception tests are missing, it is often indicator that exception handling is also either missing or not well designed. It is actually awfully common to see code where the exception handling is something that is “glued” on top of everything and hacked together to just keep things not crashing in few special cases. And that is bad. If that is the case, it is a good time to start thinking about whole exception handling strategy and get your basics back on the shape.
Usage of service locator / IOC container in tests
Whenever I see tests setting up things in some kind of IOC container or something other that looks like a service locator, I’ll end up swearing out loud (and I’m not a guy who swears a lot). I have yet found only one case when it has not been indicator that the production code is using service locator anti-pattern and ruining the structure of the code. It is, in the end, one of the worst things that you can do for maintainability of your code.
This is very clear and specific smell that is very easy to notice either by debugging through the code through test or refactoring those setups away from the test code.
So what does this all really mean?
So, we found few concrete smells that I told to point out some structural issues in the code. You might be wondering if those really are that bad as you was able to test your code and “it works as it was defined to do”. You are of course allowed think however you like to, and I must admit that there was time that I did not think those as a big issues. Those might not be big issues if you are doing very simple and non-critical system that is to be thrown away and not maintained. But if you have even small hunch that your system might end up being either complex, being critical for business or that somebody might be maintaining it after it is “finished”, then those are really big things.
I will not go through in-detail as why those things are such a big issue, as there are plenty of good articles in the depths of interwebs that dive into details. So if any of those topics is strange for you, just use our common friend Google search to enlighten you.
To summarize things, reviewing the unit tests is an excellent way to find issues in the production code and often is much better stepping stone into improvement than just start digging through the lines of production code directly.
I introduced some smells that can be found through such a review, but there are plenty of others as well. So, if you have a good smell to report and help others to give attention to those, feel free to comment and share your experiences!












