On Monday the 29th, I wrote an article about test-driven development of Docker
containers. I’m passionate about testing and instrumenting everything, and I had
found relatively few containers that were tested before being pushed out. For
the containers I built, I wanted to include testing to ensure that any changes I
made didn’t break existing functionality.
I started off with Mamba for testing. I’ve lately found that as my
tests get more complex, my Mamba tests become nigh unreadable. So I was looking
for a different solution. I’ve started getting back into Behaviour-Driven
Development and decided to look at the possibility of using Gherkin
syntax for testing my containers. I think the final result turned out pretty
So here’s what I’ve got now for the general testing requirements:
I think this combination gives me a more expressive testing implementation than
Mamba did. It’s nice to be working with Gherkin syntax again, as it makes the
testing so easy to comprehend. My first foray into using Behave to test my
containers was in my NodeJS container, and for full details on my
implementation you’ll want to look there.
There are some special sources that we need to know about in order to get behave
- features/steps/step_definitions.py: This file is where you define your steps,
what they mean, and what they do.
- features/environment.py: This file is where you define your environmental
controls, actions that run before or after certain events during the testing
For those not familiar with Gherkin, it allows you to write your tests largely
in plain English. And in TDD you want to write your failing tests first. Here
was a quick feature I wrote to test the state of the image:
It’s very short, and I feel like the language is a little awkward, but I’ll
probably refine it in the future, and obviously expand it to be more
comprehensive. Anyway, here we’re just testing to make sure that ports 3000 and
35729 are exposed.
Afterward, I had to create the step definitions of course, otherwise the above
wouldn’t mean anything to Behave.
That’s simple enough, right? The only issue now is that we don’t actually have
an image to work with. We’ve got to get the test suite to build the image for
us before any of this will actually work. In
environment.py you can add
In a nutshell, that’s roughly what I did to get Behave working in the context of
testing Docker containers. Feel free to check out my Github profile for any of
docker-* repos, as they’ll more than likely be tested using this method.