Feature Spotlight: Behavior-Driven Development in PyCharm

Posted on by Dmitry Filippov

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

Comments below can no longer be edited.

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

  1. Joachim Knust says:

    October 6, 2014

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

    • Dmitry Filippov says:

      October 6, 2014

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

      • Michael Kessler says:

        May 8, 2015

        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:

        October 26, 2015

        I will be pleased to have a French Gherkins vesion

  2. Stephen Foster says:

    November 19, 2014

    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:

    November 23, 2014

    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?

    • Dmitry Filippov says:

      November 24, 2014

      We had no such plans, however this sounds as a good feature request.
      Could you please create a ticket here https://youtrack.jetbrains.com/issues/PY ?

      • Mitchell says:

        November 26, 2014

        OK thank you, I’ve now added a ticket.

  4. Blaise Kal says:

    December 3, 2014

    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:

      December 4, 2014

      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:

        December 8, 2014

        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:

          January 15, 2015

          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:

    January 11, 2015

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

    • Ilya says:

      January 15, 2015

      Hello Zoya,

      You may use Russian in feature files now, but keywords should be in English.

      If you need to use Russian keywords, you may create Feature Request: https://youtrack.jetbrains.com/newissue , we will try to implement it in future versions.

      • Zoya says:

        January 20, 2015

        Thank you, Ilya
        Unfortunately I have no permission to create a new task

  6. Afsoun says:

    May 24, 2015

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

  7. Adam says:

    June 8, 2015

    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:

    June 15, 2015

    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. JetBrains PyCharm Professional 4.5.2 Build 141.1580 says:

    July 10, 2015

    […] Take full control over your textual requirements and graphical representation of test results, and enjoy extensive and intelligent assistance at each step of the overall BDD process. behave and lettuce, two of the most popular Python tools for BDD, are supported. More details » […]

    • Dmitry Filippov says:

      July 20, 2015

      They are planned for the next major release.

  10. Greg Gauthier says:

    August 27, 2015

    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.

    • Dmitry Filippov says:

      August 28, 2015

      Hi Greg,

      Thanks for the request. Could you please create a feature request here https://youtrack.jetbrains.com/issues/PY ? It will be easier to track and manage it.

  11. Terrence Brannon says:

    September 15, 2015

    Does it format parameter tables and keep them aligned properly?

    • Ilya says:

      September 23, 2015

      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:

    November 5, 2015

    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:

      November 5, 2015

      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:

        November 6, 2015

        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:

      November 6, 2015

      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:

    December 11, 2015

    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.

    • Dmitry Filippov says:

      December 14, 2015

      BDD support is available only in PyCharm Professional Edition.

  14. Andrey says:

    April 13, 2016

    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:

    April 13, 2016

    “-pg_sleep(0)-“

  16. Andrey says:

    April 13, 2016

    1-sleep(0)-1

  17. Andrey says:

    April 13, 2016

    1-pg_sleep(0)-1

  18. Chris Lyon says:

    October 21, 2016

    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.

  19. Ashok kumar G says:

    November 9, 2016

    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.

    • Ernst Haagsman says:

      November 9, 2016

      BDD features are a Professional feature, you can get a free 30 day trial of PyCharm Professional Edition from our website: https://jetbrains.com/pycharm

      Let us know if you have any questions!

      • Ashok kumar G says:

        November 10, 2016

        Thank you. So it is not possible in Pycharm Community right.

        • Ernst Haagsman says:

          November 14, 2016

          You can always use the command line, and manually use behavioral-driven testing. The PyCharm integration is a feature that’s only available in the professional edition though.

  20. Ashok kumar G says:

    November 21, 2016

    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:

      November 21, 2016

      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:

      November 21, 2016

      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

  21. Thach Hoang says:

    November 9, 2017

    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
    ”’)

Subscribe

Subscribe for updates