One of the Biggest Mistakes Django Developers Make When Using Lettuce
This post is the first in a series of posts about best practices when using Lettuce, a testing framework for Django.
When I first released Lettuce, a framework for writting automated tests in Django with user stories, I had no idea that it would have become so widely used. It’s been truly amazing to have seen it expand from Brazil to the United States to China and many other countries. It’s even been translated into 15 languages.
However, over the last 6 months, I’ve observed a common usage that, for the reasons below, developers should avoid.
Steps from Step Definition
Like Cucumber, Lettuce supports calling other steps from a step definition. This can be a very handy functionality, but can easily become a source of code that is hard to maintain.
So why is this functionality available?
Although Lettuce is a testing tool,
step.behave_as was a patch) that was incorporated in the codebase without complete test coverage.
step.behave_as causes a step to call many others by parsing the text and calling them synchronously and sequentially.
Some people like to use this functionality in order to make their scenario look leaner, which is fine. The actual problem is that this workflow is sub-optimal, so I would advise using this functionality with caution.
An example of step.behave_as usage (please avoid doing the same) As an example, let’s consider the following feature and its respective step definitions:
So… it looks kinda nice, why is it bad?
step.behave_asimplementation has issues.
if you have to bypass parameters to the target steps, you will need to concatenate or interpolate strings, which will easily become a mess.
if the string you pass as a parameter has typos, it’s a pain to debug.
internally in Lettuce’s codebase, every single step is built from an object which is bound to the parent scenario, and metadata such as where it is defined. The current
step.behave_asimplementation doesn’t remount those aspects properly, leading to craziness when debugging.
once you hardcode strings in your step definitions, your test’s codebase will get hard to scale to more developers, and thus, hard to maintain.
The solution: refactor generic step definitions into @world.absorb methods
@world.absorb, a handy decorator, for storing useful and generic functions in a global test scope. The
@world.absorb decorator literally absorbs the decorated function into the world helper and can be used right away in any other python files.
This decorator was created precisely for leveraging the refactoring of step definitions and terrain helpers by not requiring the developer to make too many imports from different paths, as well as to avoid making relative imports. Let’s see how the first example would look like when using
The step definition
def i_log_in_as now calls helpers that are available in the
You can easily notice that in the example above,
**@world.absorb** allows for better maintainability and cleaner step definitions.
Hardcoded strings would require manual updates when any related step-definitions has its regex changed.
Step definitions that are multiple-lines long now just bypass the parameters into single-line function calls.
When the hardcoded string has typos, no syntax error will occur yet the test will fail with a misleading error message.
Gabriel Falcao is a developer at Yipit and the creator of Lettuce. You can follow him on twitter and github.