Skip to main content

Design for Testing

Good testing influences your design. Too many developers (and managers) are stuck in the waterfall mentality of design, code, test, and deploy. Forces across the industry are pushing to move the testing phase to the beginning of this line, and many just don't understand how that works. Testing should be the first consideration, and thus is influential to all other aspects of the development process.

The problem with testing as a secondary consideration is the design and architecture of the software never lends itself to proper testing when you don't plan way ahead. The consideration of testing can drive your design to be easier to test, but also can encourage generally good programming practices and well-made designs in the architecture. We can use a persisted class as a good example, because this is a use-case where testing is very important, but we have to consider the burden of a full database tied into the class we are testing.

When we develop our database item class before any testing is considered, we create it fairly in a straight-forward manner. After everything is coded, we decide to do some testing but we have a couple problems to face. The first thing to concern us is that our class inherits some ItemSchema super-class, and instances of it must exist in context to some database, which creates a large dependency on the test and thus leads to the test being unreliable. Secondly, we have many functions not easily testable (perhaps they can only be confirmed by locals within the function, which we can not access). We need to redesign everything to solve these issues, but we could have avoided this by using a more testable design in the first place.

To solve the first problem, we have to consider what we are actually testing. We are not testing the persistence framework our application utilizes, but just one ItemSchema sub-class we had to write. Obviously, separation is key. We only care about the functionality we wrote into the type, and we can extract all this into a mixin class, which our original ItemSchema can inherit. However, in our tests, a special TestItem class may also inherit it and perform the testing we need, without bringing a database into the picture.

Tackling the second problem of individual functions, the solutions can vary. If any internal data is important, perhaps it is too internal, and the code generating it could be extracted into its own method, which we can test independently. If our method does not return anything, perhaps this is something it could return and thus we could test for it. However, don't return things you do not need in production, as this bloats the interface and inevitably some code will come to depend on this contract that was only intended as a testing mechanism. It can even be acceptable to wrap a returning method with a public API non-returning version, simply to push a more stable API (it is easier to add returns than remove them) to the consumers of the API.

We have to stop looking at the testing as a second, or third class citizen in the steps we take in development. Our design, development time-lines, and architecture should all be done with testing and quality assurance first in mind. We must focus on how to ensure the stability and accuracy of our code before we can ever trust it, and if we can not trust it, all time developing it is a waste. It is this fact that offsets all arguments by opponents of proper testing (yes, they exist) who are afraid of the time wasted by testing. Wasted time is illusionary, as we only see the time it takes but we don't see the time it save us. However, isn't that the point?

Comments

Popular posts from this blog

On Pruning Your Passions

We live in a hobby-rich world. There is no shortage of pastimes to grow a passion for. There is a shortage of one thing: time to indulge those passions. If you're someone who pours your heart into that one thing that makes your life worthwhile, that's a great deal. But, what if you've got no shortage of interests that draw your attention and you realize you will never have the time for all of them?

If I look at all the things I'd love to do with my life as a rose bush I'm tending, I realize that careful pruning is essential for the best outcome. This is a hard lesson to learn, because it can mean cutting beautiful flowers and watching the petals fall to the ground to wither. It has to be done.

I have a full time job that takes a lot of my mental energy. I have a wife and a son and family time is very important in my house. I try to read more, and I want to keep up with new developments in my career, and I'm trying to make time for simple, intentional relaxing t…

The Insidiousness of The Slow Solution

In software development, slow solutions can be worse than no progress at all. I'll even say its usually worse and if you find yourself making slow progress on a problem, consider stopping while you're a head.

Its easy to see why fast progress is better: either you solve the problem or you prove a proposed solution wrong and find a better one. Even a total standstill in pushing forward on a task or a bug or a request can force you to seek out new information or a second opinion.

Slow solutions, on the other hand, is kind of sneaky. Its insidious. Slow solution is related the Sunk Cost Fallacy, but maybe worse. Slow solutions have you constantly dripping more of your time, energy, and hope into a path that's still unproven, constantly digging a hole. Slow solutions are deceptive, because they still do offer real progress. It is hard to justify abandoning it or trying another route, because it is "working", technically.

We tend to romanticize the late night hacking…

Why I Switched From Git to Microsoft OneDrive

I made the unexpected move with a string of recent projects to drop Git to sync between my different computers in favor of OneDrive, the file sync offering from Microsoft. Its like Dropbox, but "enterprise."

Feeling a little ashamed at what I previously would have scoffed at should I hear of it from another developer, I felt a little write up of the why and the experience could be a good idea. Now, I should emphasize that I'm not dropping Git for all my projects, just specific kinds of projects. I've been making this change in habit for projects that are just for me, not shared with anyone else. It has been especially helpful in projects I work on sporadically. More on why a little later.

So, what drove me away from Git, exactly?

On the smallest projects, like game jam hacks, I just wanted to code. I didn't want to think about revisions and commit messages. I didn't need branching or merges. I didn't even need to rollback to another version, ever. I just …