Most of us use PyCharm to automate the BDD tests using Behave using Python, however, it adds to the cost of test automation. The community version is also available for free, but we can’t run behave files through it. Visual Studio Code also supports Python. However, this blog explains how to use Visual Studio Code for Python + Behave BDD tests for FREE. PyCharm and VS Code are both excellent tools for writing Python code.

PyCharm Community edition and Visual Studio Code (VSCode) are both capable integrated development environments for Python coding. To conduct this evaluation, I chose the most frequently used IDE feature and compared the experience of PyCharm and VSCode.

Let’s see how can we use VSCode for “Python behave” Automation:

First, you should make sure that you have installed the Python extension. I recommend installing it because it will allow you to install libraries and run code. To run Python and Python behave in Visual Studio Code (VSCode), visit the Visual Studio Code Marketplace to get all the extensions you’ll need.

The following three extensions must be installed before proceeding to Python:-
  • Gherkin- for creating feature files
  • Pylance: This will install all libraries which are required to run Python
  • Python extension for VSCode
All of these extensions are available in the VSCode Marketplace, and you can install them manually.
  • Firstly, we have to create a launch.json file. From the “Run and Debug” option & this file can be generated.·      
  • Go to the “Run and Debug” option, where you will see “Create a launch.json file,” and put the below code into that file.

For reference, visit: https://go.microsoft.com/fwlink/?linkid=830387

{
  "version": "0.2.0",
  "configurations": [
{"name":"Python:Current File",  // You can change this name as per your convenient
"type":"python",
"request":"launch",
"program":"${file}",
"console":"integratedTerminal",
"justMyCode":true
},
 {
    "version": "0.2.0",
    "configurations": [
        {"name":"Python: Current File","type":"python","request":"launch","program":"${file}","console":"integratedTerminal","justMyCode":true},
        {
            "name": "Python: Behave current file",
            "type": "python",
            "request": "launch",
            "module": "behave",
            "console": "integratedTerminal",
            "env": {
                "BASE_DIR": "Your repository location",
                   },
            "args":[
                "--no-capture",
                "--no-capture-stderr",
                "${file}"
            ],
        }
    ]
}

With this code, you will be able to create a “launch.json” file that will aid in running Python behavior in Visual Studio Code. As soon as you are done with the “launch.json” file, you need to add some libraries to the “settings.json” file, which will help you run the code through the command line. Open a settings JSON file by pressing (ctrl + shift + p) and selecting “Open Settings.” JSON is an option.

Add the below code to the “Settings.json” file. The code below will assist users in integrating with GitHub and running files via the command line.

"terminal.external.windowsExec":"C:\\Program Files\\Git\bin\\bash.exe",
    "terminal.integrated.shell.windows":"C:\\ProgramFiles\\Git\bin\\bash.exe",
    "python.pythonPath":"D:\\testing-native\\venv",    

The final line of code, “python.pythonpath,” will show the virtual environment path. For instructions on how to set up a virtual environment for this, see https://code.visualstudio.com/docs/python/environments Now, the user can go to the “feature file” that he/she wanted to run by selecting “Run” from the “Run and Debug” option.

Cloud Environment:

To automate an application in a cloud environment like (BrowserStack/SauceLabs), add the following code to the “Settings.json” file. Users will be able to run code in a cloud environment. Execution mode determines which cloud environment the user wishes to automate the application in, whether it is BrowserStack or SauceLabs. You can run your tests on the cloud environment of your choice by using the username and access key provided.  

	"executionMode":"Browserstack/Saucelabs",
        "userName":"Your username Key",
        "accessKey":"Your acess key"
 Navigate to Specific Function or Method.

We’d all like to be able to navigate directly to that specific function or method from the feature file step, as we can in PyCharm (ctrl+click). This can be achieved by including the following code in Settings.json in VSCode: Your work in VS code will be faster and more flexible.  

"cucumberautocomplete.steps": [
        "Features/steps/*.py"
                              ],
    "cucumberautocomplete.syncfeatures": "Features/*feature",
Conclusion:-

Pycharm is used to run a Python behave file, but many of us are aware that it’s not free, so we’ve found a solution with Visual Studio Code that will help people save money and run Python behave freely on the VSCode IDE.

Read more blogs here

25