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

I have been in the IT industry from 9 years. I worked as a curriculum validation engineer at Oracle for the past 5 years validating various courses on products developed by them. Before Oracle, I worked at TCS as a Manual tester. I like testing – its diverse, challenging, and satisfying in the sense that we can help improve the quality of software and provide better user experience. I also wanted to try my hand at writing and got an opportunity at Qxf2 as a Content Writer before transitioning to a full time QA Engineer role. I love doing DIY crafts, reading books and spending time with my daughter.

Be First to Comment

Leave a Reply

Your email address will not be published.