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

About Dmitry Filippov

Product Marketing Manager at JetBrains
This entry was posted in Cool Feature. Bookmark the permalink.

45 Responses to Feature Spotlight: Behavior-Driven Development in PyCharm

  1. Joachim Knust says:

    Very nice feature and smart integration.
    Will there be Gherkin keyword support for other languages (configurable)?

    • Dmitry Filippov says:

      Well we have no such plans currently. Let me know for what languages/frameworks do you need this support?

      • Michael Kessler says:

        Greate Feature! I love it.
        Currently I’m working with guys from another Business Unit. All of them are German native speakers.
        The support of German Gherkin keywords would be very very helpful.
        Thanks.

      • TychoTa says:

        I will be pleased to have a French Gherkins vesion

  2. Stephen Foster says:

    This looks great. PyCharm 3.* has changed my world, can’t wait to see what 4 has to offer.

    The BDD (+1 lettuce) navigation and quick fixes are great.

  3. Mitchell says:

    This is a nice feature. Would it be possible to have support for behave’s default parse mode for step parameters instead of using re?

  4. Blaise Kal says:

    Normally I run my tests like this:


    python run_behave.py testcases/website.feature --browser_name=firefox --target_env=https://example.org

    How do I convert this to a Behave Run Configuration in PyCharm?

    • Ilya says:

      Hi,

      What is “run_behave.py”? Is it your custom file? What does it do in this case?

      You can pass any arguments to behave but behave does not have “browser_name” nor “target_env” arguments.

      If you need to pass some data to your step definitions you may use environment variables (they may be passed to any python configuration in PyCharm including behave).

      Look: http://s1.postimg.org/aat76bhfz/params.png

      • Blaise Kal says:

        My run_behave does the following:


        from behave import configuration
        from behave import __main__

        # Adding my wanted option to parser in behave.
        configuration.parser.add_argument('-b', '--browser_name', help='Browser to use')
        configuration.parser.add_argument('-vb', '--browser_version', help='Browser version')
        configuration.parser.add_argument('-os', '--operating_system', help='OS where the browser is running')
        ...

        __main__.main()

        I guess it shouldn’t have been done this way. I will try to rewrite this to environment variables.

        • Ilya says:

          Hello,

          PyCharm uses Behave API to run it, so you should not run it directly.

          I believe env. variable is the best way to pass something to step definitions.

  5. Zoya says:

    Could you make support for different languages? We use Russian in “feature” files but they’re shown as plain text.

  6. Afsoun says:

    why Scenario outlines are not detected by the lettuce runner. I get “Empty test suite.” error message

  7. Adam says:

    Hi,

    I’ve been using Behave BDD framework with PyCharm and it’s great!!
    One quick question, it seems like the test run terminates if one of the scenarios fails. Is there a way force the feature to run completely even some scenario fails.

  8. Ilya says:

    Hello.

    *Feature* does not stop when *scenario* fails, but *scenario* fails when one of its *steps* fails. That is how Behave works.

    Look: http://postimg.org/image/pqhkhyqdn/

    If PyCharm behaves differently in your case, please submit a bug.
    Thank you.

  9. Pingback: JetBrains PyCharm Professional 4.5.2 Build 141.1580

  10. Hello,

    I’m a tester and test automation programmer for a small python shop. The dev team I work with is heavily invested in Pytest, and as a result, insist on using the Pytest-BDD plugin for Gherkin, rather than Behave.

    Any chance you folks will ever incorporate support for Pytest-BDD into Pycharm? It’s not a big deal for the dev team (who all use emacs). However, it would be nice if I could have a lot of these IDE conveniences.

    Thanks,
    Greg.

  11. Does it format parameter tables and keep them aligned properly?

    • Ilya says:

      Hi Terrence,

      If you speak about “examples” section for scenario outlines, then answer is yes.

      When you call “reformat” (which is CTRL+ALT+L on Windows for example) in Gherkin (.feature) file, it reformats tables, so they look pretty.

  12. Veronica Taquette says:

    Hi,

    I’m have some trouble setting up my environment using lettuce and django.
    I can run lettuce inside virtualenv with ‘python manage.py harvest’, but when I try to use a lettuce configuration. I get this error:

    ValueError: Unable to configure handler ‘mail_admins’: Cannot resolve ‘assettools.common.backend.log.FormattedSubjectAdminEmailHandler’: cannot import name QuerySet when importing (…)\blockOperations-steps.py

    Here is my django configuration: https://drive.google.com/a/inoa.com.br/file/d/0B4f4mYTfQVdyM3MzbmtaVm9CUnM/view?usp=sharing
    And my lettuce configuration: https://drive.google.com/a/inoa.com.br/file/d/0B4f4mYTfQVdyS2NFTXVEMlRmMGc/view?usp=sharing

    Can you help me?

    Best regards,
    Veronica

    • Ilya says:

      Hi. Could you give me access please?

      I am not sure you can run lettuce configuration with Django. But you may run manage.py console from PyCharm and run harvest from there.

      • Veronica Taquette says:

        Hi,

        sorry about the restricted access, it should work now.
        I can run harvest from the manage.py console successfully, but then the results are presented in a very inconvenient way (plain text), and it gets hard to track the result with everything in the console. What would be the proper way to configure it so I can use it as a Lettuce configuration? Should I have a separate project just for the tests?

        Thanks!

    • Ilya says:

      Unfortunatelly PyCharm does not have harvest support for now. Please create feature request: http://youtrack.jetbrains.com

      You can use Django manage.py console in PyCharm to run tests for now.

  13. Sid says:

    Is this feature available in the community version also?
    I miss these features in the community version which I enjoyed in the paid version of RubyMine. Very helpful features. Would love to see it working … in free or paid version.

  14. Andrey says:

    I was crafting a tutorial of BDD in PyCharm and I noticed several things I didn’t like:

    1) “Create all steps definition” doesn’t work well. It creates definitions for “some” expressions and I can’t figure out how it choose them.
    2) Automatically created step_impl functions have “pass” in it. That means auto-generated tests will pass by default. That’s not nice. Test must fail!
    3) It adds “use_step_matcher(‘re’)” by default. 99% of times you wont need that. I mean if you have a problem you want to solve with regular expressions then you have two problems :)

    But overal nice support and great work!

  15. Andrey says:

    “-pg_sleep(0)-“

  16. Andrey says:

    1-pg_sleep(0)-1

  17. Chris Lyon says:

    When you start to have a few features files it seems sensible to produce a directory structure to manage them.
    This is reasonably easy to establish and a right click on a directory name allows you to Run all the tests in that directory.
    However if the directory structure is deeper than that you cannot run the all the contained feature files.

    This would be useful and allow a granular approach to test running.

  18. Ashok kumar G says:

    I have downloaded the Pycharm Community and I have installed the behave 1.2.5 through pip.
    In the project interpreter I could see the installed packages. When I try to create a new feature file, I don’t see the “new Gherkin file” option in the context menu itself.Now how can i create a feature file in my project. Do i need to add any other plugin.

  19. Ashok kumar G says:

    How can I run multiple feature files in Pycharm Professional edition.
    I have used behave “one.feature”, “two.feature” but it fails. Do we need to setup any configuration file or anything else

    • As says:

      Actually I have removed the comma separator in between two feature files and it started to work.

      When I have more feature files how can I give it another way apart from command line/terminal.

      Any configuration file or .bat file or any runner file?

    • Ashok kumar G says:

      I could run the multiple features by removing the comma which is in-between the feature files. Now, how could I run apart from terminal/command line. Any way like configuration setup, runner file or .bat file

  20. Thach Hoang says:

    Hi,

    I am using Pycharm with behave. is there any way that i can navigate from a Scenario description in execute_steps() block to the actual step definition ?

    eg:
    @Given(‘i am on the home page of the website’)
    def step_on_home_page(context, publisher):
    context.execute_steps(u”’
    Given I am on the login page
    When I login
    Then I am redirected to Home Page
    ”’)

Leave a Reply

Your email address will not be published. Required fields are marked *