Run python behave from Visual Studio Code

I was working at a client that uses behave and was primarily designed to work on Linux systems. It has several configuration variables stored as environmental variables. I tried running the behave tests on Windows using Visual Studio Code since I wanted debugging functionality. I documented the steps so that it will help anyone who wants to try the same.

Prerequisite

Before getting started, please install the required packages needed for an automation environment. The primary ones would be Selenium, Python, pip, behave, chromedriver (or any browser) on your Windows machine.


Setting up configuration in Visual Studio Code

  1. In Visual Studio Code, open the folder you want to work on.
  2. Click on Debug Icon and then on ‘Configure or Fix launch.json’ icon. The Select Environment dropdown gets opened. Select Node.js. 

    Select_environment 

  3. The launch.json will be displayed. This file is used to configure the debugger in Visual Studio Code.
  4. Add the following configuration details to the launch.json
    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Python: Behave current file",
                "type": "python",
                "request": "launch",
                "module": "behave",
                "console": "integratedTerminal",
                "env": {
                    "BASE_DIR": "<your_automation_repository>"
                       },
                "args":[
                    "--no-capture",
                    "--no-capture-stderr",
                    "${file}"
                ]
            }
        ]
    }

    Note: In BASE_DIR we have set the path to automation repository. So, we will have the flexibility to change it to the directory we are working on.

  5. The final file should look like this:
    Final launch.json
  6. Set the integrated terminal as gitbash so that tests will be run through it.
    Do Ctrl + Shift + P to open the settings editor. Type settings and clicking on the JSON option.
    Settings editor
    Provide the path to the git bash in the “terminal.integrated.shell.windows” option

  7. Set bashrc.

    if [ -z $BASE_DIR ]; then
     BASE_DIR=<path of your automation repository>
    fi
    cd $BASE_DIR

    The BASE_DIR specified here is a default directory which will be used in case the directory is not set in VSCode and prevent the script from failing.

  8. If you are using a virtual environment, set the path to it in settings.json.
    Do Ctrl + Shift + P to open the settings editor. Type settings and clicking on the JSON option.

    Settings editor
    Set the path of the virtual environment under “python.pythonPath”

That is it. Open the feature file you want to run and click on the Run icon to start running your test in debug mode. There are various option such as Pause, Step Over, Step Into etc. while running the test.  Debug icons

References

  1. https://code.visualstudio.com/docs/editor/variables-reference

9 thoughts on “Run python behave from Visual Studio Code

  1. Hi Sravanti, can you please help me in step 5 & 7. I am trying to run this on my local. If I do not add ‘env’ key value and skip step 7, I am not getting RUN button on my feature file. I manually added step file. Also please let me know if you have a above info in a video on your YouTube channel (if you have). It would be really helpful for my project. Thanks-Harshad.

    1. Hi Harshad,
      Can you try adding the local path of your directory in the env value and check…
      We do not have any youtube video to suggest.

    1. Hi Zeph,
      CAn you help me? My behave framework is able to identify step file. But not identifying the actual steps. I.e when i write actual a sample python code it is executing, but not the steps. Not sure what settings do I need to change.

      Your help would be highly appreciated.

      Thanks and Regards,
      Prasad Jonnada

      1. Hi Prasad,
        Its tough to figure without looking at the code. Can you check if the suggestion mentioned here helps.

    1. Hi Val,

      I have a few suggestions which might help resolve:

      1. Check if you have set the BASE_DIR to test/behave in your bashrc to help being picked as the default directory

      2. You could try renaming your test folder from test/behave to test/#anything_else_apart_from_behave# as it might cause name conflicts with the python behave package

  2. I found a visual studio code extension called “Behave VSC” that runs/debugs tests direct from the vscode test panel as long as your features folder is somewhere in your project. (It’s in pre-release but it seems to work great for my project setup.)

    Btw, did you know you can specify your features path via a behave.ini file?

Leave a Reply

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