Developers often dream of starting a new code project from scratch, but most have to make do with a legacy project. The best they can hope for is a rewrite.
I was lucky. In my previous role as a development manager at a large financial institution, I had the pleasure to work on a greenfield project, one of the company’s first truly agile efforts. How did we start? The first step was to form scrum teams and fill the backlog of work. One of the problems that arose early on involved software testing.
Many developers on our team were big fans of test-driven development (TDD), a software development process where you write tests before writing the code. The goal is to write a failing test first, although this test is really more of a specification before you start to flesh out the code. The major problem with TDD is that it focuses primarily on developers rather than embracing the entire organization. Enter BDD.
BDD: A Better Approach
Behavior Driven Development, or BDD, is a modern software testing method of getting testers, developers and the business to work together. The idea is to take things up a level and start talking about what your business and customers really want from a software product.
Once you manage to get these guys talking and collaborating with each other, you need a common language. This is often achieved with the famous Given-When-Then method of writing user stories. We used on our project, in fact.
However, there’s a risk that this formula gets abused, and that business leaders start writing stories with Given-When-Then syntax, only without the collaboration. This simply does not work.
Why? In my experience, the business often didn’t understand the terminology and would use it in a way that made no sense at all. More importantly, their approach to Given-When-Then was all wrong. For this method to work, the business needs to collaborate with developers and testers to write user stories.
Automating Your Tests
Once your team has a shared understanding of user behavior—and has articulated this in a common language—it’s time to start automating tests to prove the success of your business definition.
Unfortunately, many companies have a bad strategy when it comes to software testing: too many end-to-end tests and not enough unit tests. The problem here is that end-to-end tests take a long time to run and can be very difficult to maintain. A better approach is to run end-to-end tests and manual tests to cover only the business specification of the user story.
You want to avoid looking at edge cases and bad data in heavy end-to-end (e2e) tests, which are complicated, time-consuming and expensive to run. It’s smarter to do this in other parts of the test package, mainly unit, component, integration and contract tests.
In a unit test, for instance, you only have to exercise a tiny piece of code to check for proper operation. The idea here is to devise a test that achieves a small goal. But too often companies find themselves in a situation with a million end-to-end tests, and a test suite that takes weeks and weeks to run. The end result? They’re not getting any value from their tests.
Your ratio of UI and manual tests to unit tests should resemble the pyramid below. The higher the spot on the pyramid, the lower the number of tests:
Due to the complexity of maintaining end-to-end tests, some companies have ditched them altogether in favor of synthetic testing in production, something that AppDynamics is uniquely positioned to provide with our browser synthetic monitoring. For example, most organizations use Selenium to write their end-to-end tests, and we use Selenium browser automation on the backend for synthetic testing.
The Virtues of Contract Testing
For microservices testing, contract tests can provide the greatest value. When a consumer binds to a producer, a contract forms between them. Since components in a microservices architecture have many contracts, this area of testing becomes critical. In the old days of the monolithic application, code would compile together. Although there would still be implied contracts between various parts of the code, these contracts would be enforced at compile time. Since this safety net isn’t available in a microservices environment, you absolutely need reliable contract tests in place.
Each consumer forms a different contract based on how it uses a service. Here’s a quick example I’ve borrowed from Martin Fowler:
A service exposes a resource with three fields: identifier, name and age.
Three consumers, each coupled to different parts of the resource, have adopted the service.
Consumer A couples only to the identifier and name fields. The corresponding contract test suite asserts that the resource response contains those fields, but makes no assertion concerning the age field.
Consumer B uses the identifier and age fields, but not the name field. Consumer C uses all three fields. The contract test suite for Consumer B makes no assertion for the name field. But for Consumer C, the test suit asserts that the resource response contains all three fields.
It’s important in microservices that these contracts be satisfied, even though downstream services are likely to change over time.
With contract testing, each consuming service runs a suite of unit tests that check whether the inputs and outputs are valid for their purposes. Those tests are then run by the producer as part of its build pipeline should any changes occur to that service.
In my next blog, I’ll cover the practical aspects of setting up a successful build pipeline, which covers contract and microservice testing.