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
- In Visual Studio Code, open the folder you want to work on.
- Click on Debug Icon and then on ‘Configure or Fix launch.json’ icon. The Select Environment dropdown gets opened. Select Node.js.
- The launch.json will be displayed. This file is used to configure the debugger in Visual Studio Code.
- 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.
- The final file should look like this:
- 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.
Provide the path to the git bash in the “terminal.integrated.shell.windows” option - 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.
- 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.
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
References
- 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.
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.
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.
Hallelujah, this actually works!!!
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
Hi Prasad,
Its tough to figure without looking at the code. Can you check if the suggestion mentioned here helps.
Hi Sravanti
Thanks so much for this. Much appreciated.
My attempt to use it is failing with a module not found, presumably because I don’t properly understand what you mean by “behave repository”. My configuration etc. are visible on github here:
https://github.com/valhuber/TDD#readme
Thanks in advance,
Val
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
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?
Thanks Bob for the suggestions!