Skip to main content

Search...

Software Testing Mistakes: When Green Tests Mean Nothing

Tests that check internals, lean on mocks or chase coverage are typical software testing mistakes. They go green anyway, for the wrong reasons.

• • 10 min read
Cover of the expert talk on 'Software Testing Mistakes: When Green Tests Mean Nothing' with Golo Roden and Richard Seidl.

Software testing mistakes are recurring patterns in automated tests that produce green results without verifying how the software behaves. They include testing the internal implementation instead of the behavior, mocks throughout the business logic and test coverage as a target. A green test then only shows that one specific bug did not occur.

Key Takeaways

  • Golo Roden considers tests that go green for the wrong reasons worse than no tests, because they suggest that everything is fine until the software fails in production anyway.
  • A test that checks the implementation instead of the behavior breaks during refactoring or performance tuning, although the software still does the same thing from the outside.
  • Nobody guarantees that a mock behaves the way reality does; if the next database version behaves differently, production code breaks while the tests stay green.
  • High test coverage only shows that some test executed every line or statement once, and says nothing about whether anything meaningful was checked.
  • Reading “one assert per test” as one line of code is a software testing mistake: the rule means one thing per test, so three asserts can check one API response.

Software Testing Mistakes Start With Chasing Green

A team changes one line of code, and a large number of tests turn red. The team deletes every red test, the suite is green again, and the good mood is restored. Golo Roden saw this happen at a client whose team was fairly new to automated testing.

The case is extreme, but the pattern behind it is ordinary. Software testing mistakes pile up wherever a team treats the green test run as the goal and loses sight of what the test is supposed to check. Golo names four that he has kept running into as a consultant over the years: tests written against the internals of a system, mocks sprinkled everywhere, the chase for test coverage, and a misread rule about one assert per test.

The four have something else in common. Teams argue at length about symptoms, such as which mock framework to pick, and rarely about the approach that produces those symptoms.

Why Should Tests Check Behavior Instead of Internals?

Because a test exists to make sure the software still behaves the same after it has been reworked. A test that checks how the software is built inside falls over with every refactoring and every performance optimization, even though nothing has changed on the outside.

Golo explains it with a box that has two buttons and an LED. The left button switches the LED on, the right one switches it off. A behavioral test presses the buttons and watches the LED. A test against the internals checks whether current flows through a particular wire, a particular resistor and a relay. Rewire the box so it draws less power, and the second test goes red while the first stays green.

The black-box view is hard at the start, because some things seem impossible to check from the outside. The discussion in the team then quickly turns to how to get at the internals. What would help more is someone who takes a step back and says that the outer shell is enough.

When Do Mocks Become a Problem?

As soon as the tests for the business logic contain more mocks than anything else. Golo reads that as a sign that the architecture is off, and sees no reason in it to go looking for a better mock framework.

The road there usually starts with the internals. Anyone who wants to know whether a certain method was called, and how many times, ends up with stubs, mocks and spies. There are situations where these test helpers make sense. What bothers Golo is how widely they get spread around.

His example is the message queue. If a test has to remote-control it to check business logic, the domain logic is tied to the technology. Golo wants the dependency the other way round: the technology depends on the domain, and the business logic doesn’t care whether a message queue exists at all.

The way he sorts tests fits in here. The textbook line between unit test and integration test matters little to him, especially since there is more than one definition. He separates small, fast tests that run reproducibly without external infrastructure from slow tests that need a database or other external components. He wants as many of the first kind as possible.

A Green Test Says Less Than a Red One

A red test tells you something. A green one does not. Green only means that the one bug the test is aimed at didn’t happen this time, for whatever reason.

Golo compares it to a virus scanner. If the scanner raises the alarm, there is a problem. If it finds nothing, the machine is still not proven clean; it only has no virus that this scanner would have found.

Mocks make the imbalance worse. A mock fakes a behavior, and nobody guarantees that reality behaves the same way. Mock out the database, and you won’t notice when its next version behaves differently from the previous one. Production code breaks while the tests stay green, because nobody updated the mocks.

“Worse than no tests at all are tests that pass for the wrong reasons.”

(Golo Roden)

Tests like these leave the team feeling it did everything right. When things fall over later anyway, everyone asks how that could happen with so many tests. Golo’s yardstick is closeness to production. The closer a test is to the reality there, the better it is.

What Does High Test Coverage Tell You?

Not much. Test coverage only shows that every line or every statement was executed by some test at some point. Whether that test checked anything meaningful is not in the number.

That makes the metric as one-sided as the virus scanner. At 10 percent test coverage a lot of tests are obviously missing, and that is a clear statement. A figure of 95 or 100 percent says nothing about whether the existing tests are any good.

Teams rest on the number anyway, or they jump through remarkable hoops to reach a threshold someone has set. The result is a test that contributes nothing useful but lifts the percentage. From that point on the team is optimizing the metric and no longer what the metric was meant to measure.

Golo sees this with every metric. His line that there are no good metrics in software development means exactly that: someone always finds a creative way to improve the number and miss the goal behind it.

Can a Test Contain More Than One Assert?

Yes. Two, five or a hundred, as long as together they check one thing. The rule from the textbooks on test-driven development, usually stated alongside the Arrange-Act-Assert pattern, asks for one thing to be verified per test, however many lines with an assert that takes.

Teams have seriously asked Golo how to squeeze a check into one line, because two asserts were supposedly forbidden. That is a misunderstanding.

His example is an API endpoint that should return a correct result for a specific input. Correct here means status code 200, a specific content type and specific content in the response body. That makes three asserts for one statement. If any of the three changes, the response as a whole is wrong, and a test should fail that says exactly that.

The endpoint could also be checked with separate tests, and where to draw the line is open to argument. But status code, content type and body rarely change independently of one another, because they belong to one domain construct. So Golo puts them into a single test.

What to Test Is a Domain Question

What counts as a meaningful test is decided by the domain. Test code, test framework and test runner are technology, but the question of what to check can only be answered by someone who knows what the software is for.

Golo sees a problem here that is bigger than testing. Too many developers let technology play first fiddle. A new project starts, the team spends three weeks debating Java versus .NET, and nobody asks about the details of the domain. Two years later it becomes clear the project is going wrong, and someone says they should have gone with .NET. That would have changed nothing, because nobody would have understood the domain problem then either.

Golo gets blunt at this point. Developers earn more than people in a lot of other jobs, so it is fair to expect them to leave their “cozy little technical comfort zone” now and then. The customer doesn’t care whether the UI is built in Angular or in React, or what percentage of the tests were green. What matters more are the goals and wishes of the people who will use the application later.

Generated code makes this more pressing. Code can be produced very fast today, and so can the wrong code that solves the wrong problem. So before you write the first test, ask how the software is supposed to behave and for whom. The same question has a different answer depending on the context, and a team that knows the context writes tests that are better aimed at what they are meant to check.

Simple Tests, Hard Scenarios

A test should be a simple sequence of steps. The more complex the setup a team has to build for it, the more likely the team is to get something wrong along the way.

Textbooks make it look easy. TDD is often explained with a function that adds two numbers: two numbers in, a sum out, test green. Once the code has side effects and reaches for external resources, testing stops being that easy.

Golo is currently working on how to test what a system does during a power outage. There is no function to call for that, and a computer switched off halfway through does not turn itself back on. Verifying that the system loses no data even then is a good deal harder than the sum of two numbers.

Teams fail at such scenarios in two ways. Some imagine the job to be too simple and give up too early, others build something far too complicated.

Who Pays for Testing?

Nobody, at least not willingly. Everyone assumes that delivered software is bug-free and does what it is supposed to do. That this takes effort is easily overlooked, and there is little understanding for it.

Golo compares it to a tradesman installing underfloor heating. He checks that everything is watertight before the parquet goes back on, and no customer tells him to skip those two hours because it would be cheaper. In software development that is exactly what happens. And whoever then skips the check ends up as the idiot, because the software doesn’t work.

That is also how Golo explains the team with the deleted tests. When a small change turns a great many tests red, it has seriously changed the behavior, even if it is only one line long. The team was overwhelmed and didn’t ask. Anyone who has to explain to management why no new feature has been built for four weeks, and then says “we’re working on quality”, quickly hears the follow-up question of whether garbage was being produced before. So many prefer to say nothing.

Share this page

Related Posts