Reading Time: 3 minutes
This is the sixth episode of our Testing Tuesday series. Every week we will share our insights and opinions on the software testing space. Drop by every Tuesday to learn more! Last week we talked about testing web applications with Selenium. This week we focus on cucumber best practices.
How to use Cucumber? – Cucumber best practices
Once you get started with Cucumber, the question is how to write your features. How can you keep your features maintainable so you don’t have to correct them after each change in the application? How can you reuse steps most efficiently? What are typical Cucumber smells?
Following I list 5 best practices that help us in our daily Cucumber life at Codeship.
1. Write declarative features
Scenarios should be written like a user would describe them. Beware of scenarios that only describe clicking links and filling in form fields, or of steps that contain code or CSS selectors. This is just another variant of programming, but certainly not a feature description.
Declarative features are vivid, concise and contain highly maintainable steps.
2. Insert a narrative
Narratives describe in about one sentence what a feature does. Typical narratives contain a benefit for the user, a role that needs the feature and the feature itself. Narratives are important to envision why you are implementing a feature in the first place. They also give a short overview of the feature so others get a rough understanding what it is about without reading the scenarios.
3. Avoid conjunctive steps
When you encounter a Cucumber step that contains two actions conjuncted with an “and”, you should probably break it into two steps. Sticking to one action per step makes your steps more modular and increases reusability. This is not a general rule though. There may be reasons for conjunctive steps. However, most of the time it’s best to avoid them.
4. Reuse step definitions
In Cucumber you can reuse steps in other steps. This comes in handy when a step extends another step’s behavior or defines a superior behavior that consists of multiple steps. You should try to reuse steps as often as possible. This will improve the maintainability of your app: If you need to change a certain behavior, you just need to change a single step definition.
5. Use backgrounds wisely
If you use the same steps at the beginning of all scenarios of a feature, put them into the feature’s Background. Background steps are run before each scenario. But take care that you don’t put too many steps in there as your scenarios may become hard to understand.
Up next week: Managing Test Data with factory girl
In next week’s Testing Tuesday #7 we’ll talk about test data. What’s the best way to create test data? How can you keep test data maintainable when your application data model changes? If you’ve got some tips on test data management or you can recommend a good test data tool, please drop me a line!
PS: If you liked this article you might also be interested in one of our free eBooks from our Codeship Resources Library. Download it here: Why Continuous Integration Is Important