How to convince someone to write "impossible" tests

"Next time you find an untestable component, bring it to me, and we'll write the test together."

In one of my regular Slack groups, I was asked a great question (shared anonymously with permission):

I respect your opinion here Jonathan, and I wonder what you think of something I’ve just been mulling over. I’ve got an engineering organization full of FE engineers who are insisting to me that you just can’t meaningfully write unit tests for browser-based applications, all you can do is automate end-to-end testing with something like Selenium.

I know they’re full of it. But that’s not a very politic thing to say to my staff. So what I want to do is challenge them to implement unit tests for just 10% of their application. I’m fairly sure that what’s holding them back is their preconceptions, and once they start trying, they’ll discover ways to structure their code so that there is a set of unit-testable logic and then a presentation/visible layer that rides on top of that.

I mean, I could say “please just go try to write one” but I feel like giving them a target to shoot for should help avoid trivial gaming.

Great question! And here’s how I responded…

In my experience, when someone says “It’s impossible to test in X scenario” (FE comes up a lot… but it’s not the only place… embedded systems, DB access, etc, come up all the time, too), it’s usually a failure of imagination, and the cure is to demonstrate a test working in that scenario.

I would be a bit surprised if “Write a test for 10%” would solve this. If they comply, they’ll probably cover the least interesting parts that fit within their preconceived notion of what is testable. (They may just add tests to getters and setters, for example).

I think the solution is more along the lines of “Next time you find an untestable component, bring it to me, and we’ll write the test together.”

If you’re not able to help them with that, due to time constraints or technical ability, find someone who can. That might mean hiring someone, either permanently or temporarily (could be a 2-day test workshop or something if budget is really tight).

Have you had different experience? What worked for you?

Share this