10

I use Visual studio code to edit my Behave scenarios and Python steps.

I have been able to run my steps using the Visual Studio Code console.

I have not been able to configure the python debugger to debug my Python scripts when they are called by behave.

How can I use the Visual Studion Code Python debugger with Behave.exe ? What would be the debug configuration parameters?visual studio code launch.json

Tintin_MacFly
  • 141
  • 1
  • 1
  • 5

9 Answers9

10

Add configuration for start debugin current features:

{
    "name": "Python: Behave current file",
    "type": "python",
    "request": "launch",
    "module": "behave",
    "console": "integratedTerminal",
    "args": [
        "${file}"
    ]
},
Cadoru
  • 101
  • 1
  • 2
  • 1
    This worked great, thank you so much! (Works on MacOS) just keep in mind you have to launch it on the .feature file - which is odd, but makes sense – Kamel Nov 14 '19 at 19:20
  • Thanks, this approach works for me (Ubuntu 22.04,VS Code). Configure launch.json and save it. Then move to the .feature file and click F5 for debugging. – Martin Jul 25 '23 at 10:22
6

Use the following launch.json settings to run a single Behave test by selecting the scenario name and press F5 or Start Debugging button.

    {
        "name": "Python: Behave",
        "type": "python",
        "request": "launch",
        "module": "behave",
        "console": "integratedTerminal",
        "args": [
            "--no-capture",
            "--no-capture-stderr",
            "--no-skipped",
            "${file}",
            "--tags=${selectedText}"
        ]
    }
Ram Sharma
  • 2,487
  • 1
  • 8
  • 7
Shaw Rich
  • 69
  • 1
  • 1
4

Finally, I succeeded to debug my python code by calling the Python main of behave using the following Launch.json:

 {

  "name": "Python: Terminal (integrated)",

  "type": "python",

  "request": "launch",

  "program": "C:\\Program Files\\Python37\\Lib\\site-packages\\behave\\__main__.py",

  "console": "integratedTerminal",

  "cwd":"${workspaceFolder}\\Drivers_Features"

 },

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Tintin_MacFly
  • 141
  • 1
  • 1
  • 5
2

The only thing I can see that differs between your and my launch.json setup is that I use args to set the test directory instead of switching cwd:

{
    "name": "Python: Behave (.venv)",
    "type": "python",
    "request": "launch",
    "program": "${workspaceFolder}/.venv/bin/behave",
    "console": "integratedTerminal",
    "args": [
        "tests/integration"
    ],
}
lindstromhenrik
  • 1,143
  • 9
  • 11
1

I use a conda environment with behave installed on the %path% using pip.

In my environment I also have a tests/ subdirectory which I explicitly use cwd to change path before executing behave.

This is the excerpt from my launch.json

    {
        "name": "Python: Behave",
        "type": "python",
        "request": "launch",
        "console": "integratedTerminal",
        "cwd": "${workspaceFolder}/tests",
        "args": ["-m", "behave"]
    },

What I'm effectively trying to achieve here is something resembling the following steps on the command line:

  1. cwd /path/to/workspace/folder/tests
  2. python -mbehave

I have the VSCode for Python extension installed which recognizes my conda virtual environment.

I am also running this on a Windows box, so your mileage may vary on Linux or Mac.

Simon Campbell
  • 742
  • 9
  • 16
1

You can also use this configuration

{
        "name": "Behave",
        "type": "python",
        "request": "launch",
        "module": "behave",
        "cwd": "${workspaceRoot}/behave"
}
1

I like lindstromhenrik's answer as it doesn't need to change the current working directory.

For other Windows users (as the user asking the question was), the behave .exe program is in the "Scripts" folder:

{
    "name": "Python: Behave (.venv)",
    "type": "python",
    "request": "launch",
    "program": "${workspaceFolder}\\.venv\\Scripts\\behave.exe",
    "console": "integratedTerminal",
    "args": [
      "tests"
    ]
}
Adrien C.
  • 21
  • 1
1
{
    // 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": "Behave: Current File",
            "type": "python",
            "request": "launch",
            "module": "behave",
            "args": ["features/featureDef/${fileBasenameNoExtension}.feature"]
        },
        {
            "name": "Behave @testThis",
            "type": "python",
            "request": "launch",
            "module": "behave",
            "args": [
               "--tags= testThis",
               "--no-skipped"
            ]
        },
        {
            "name": "Behave Run ALL",
            "type": "python",
            "request": "launch",
            "module": "behave"
        }


    ]
}
Aziz Benn
  • 11
  • 1
0

add json. file: add @wip tag in gerkin scenrio. select debug in Behave Current Scenerio select Gerkin scenerio and run debug.

{
    // 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": "Behave Run ALL",
            "type": "python",
            "request": "launch",
            "module": "behave",
            "console": "integratedTerminal",
            "env": {
                "BASE_DIR": "${workspaceFolder}"
                   },
            "args":[
                "--no-capture",
                "--no-capture-stderr",
                "*.feature"
            ]
        },
        {
            "name": "Behave specific tags",
            "type": "python",
            "request": "launch",
            "module": "behave",
            "console": "integratedTerminal",
            "env": {
                "BASE_DIR": "${workspaceFolder}"
                   },
            "args":[
                "GUI/CustomerWebApp",
                "--tags=@US1003",
                "--no-capture",
                "--no-capture-stderr"
            ]
        },
        {
            "name": "Behave Current file",
            "type": "python",
            "request": "launch",
            "module": "behave",
            "console": "integratedTerminal",
            "env": {
                "BASE_DIR": "${workspaceFolder}"
                   },
            "args":[
                "${file}",
                "-w",
                "--no-capture",
                "--no-capture-stderr"
            ]
        },
        {
            "name": "Behave Selected Scenario",
            "type": "python",
            "request": "launch",
            "module": "behave",
            "console": "integratedTerminal",
            "env": {
                "BASE_DIR": "${workspaceFolder}"
                   },
            "args":[
                "${file}",
                "-n",
                "${selectedtext}",
                "--no-capture",
                "--no-capture-stderr"
            ]
        }
    ]
}
Ritesh Toppo
  • 323
  • 1
  • 4
  • 13