Features Tips & Tricks Tutorials

Feature Spotlight: Behavior-Driven Development in PyCharm

Happy Friday!

Today I’d like to shed some light on another brand-new functionality upcoming for PyCharm 4 – Behavior-Driven Development (BDD) Support. You can already check it out in the PyCharm 4 Public Preview builds available on the EAP page.

Note: The BDD support is available only in the PyCharm Professional Edition, not in the Community Edition.

BDD is a very popular and really effective software development approach nowadays. I’m not going to cover the ideas and principles behind it in this blog post, however I would like to encourage everyone to try it, since it really drives your development in more stable and accountable way. Sure, BDD works mostly for companies that require some collaboration between non-programmers management and development teams. However the same approach can be used in smaller teams that want to benefit from the advanced test-driven development concept.

In the Python world there are two most popular tools for behavior-driven development – Behave and Lettuce. PyCharm 4 supports both of them, recognizing feature files and providing syntax highlighting, auto-completion, as well as navigation from specific feature statements to their definitions. On-the-fly error highlighting, automatic quick fixes and other helpful PyCharm features are also available and can be used in a unified fashion.

Let me show you how it works in 10 simple steps:

1. To start with BDD development and in order to get the full support from PyCharm, you first need to define a preferred tool for BDD (Behave or Lettuce) in your project settings:

settings

2. You can create your own feature files within your project – just press Alt+Insert while in the project view window or in the editor and select “Gherkin feature file”. It will create a feature file where you can define your own features, scenarios, etc. PyCharm recognizes feature files format and provides syntax highlighting accordingly:

feature_file

3. Since there is no step definitions at the moment, PyCharm highlights these steps in a feature file accordingly. Press Alt+Enter to get a quick fix on a step:

new_step

4. Follow the dialog and create your step definitions:

step_definition

5. You can install behave or lettuce right from the editor. Just press Alt+Enter on unresolved reference to get the quick-fix suggestion to install the BDD tool:

install

6. Look how intelligently PyCharm keeps your code in a consistent state when working on step definitions. Use Alt+Enter to get a quick-fix action:

intelligence

7. In feature files, with Ctrl+Click you can navigate from a Scenario description to the actual step definition:

navigate

Note: Step definitions may contain wildcards as shown in the step #6 – matched steps are highlighted with blue in feature files.

8. PyCharm also gives you a handy assistance on automatic run configurations for BDD projects. In the feature file, right-click and choose the “create” option, to create an automatic run configuration for behave/lettuce projects:

run_configurations

9. In the run configurations you can specify the scenarios to run, parameters to pass and many other options:

run_configurations2

10. Now you’re all set to run your project with a newly created run configuration. Press Shift+F10 and inspect the results:

tests

That was simple, wasn’t it?
Hope you’ll enjoy the BDD support in PyCharm and give this approach a try in your projects!

See you next week!
-Dmitry

image description