【问题标题】:How to debug Behave BDD scenario using python debugger and Visual studio Code如何使用 python 调试器和 Visual Studio Code 调试 Behave BDD 场景
【发布时间】:2019-03-14 11:24:33
【问题描述】:

我使用 Visual Studio 代码来编辑我的行为场景和 Python 步骤。

我已经能够使用 Visual Studio Code 控制台运行我的步骤。

当行为调用我的 Python 脚本时,我无法配置 python 调试器来调试它们。

如何将 Visual Studion Code Python 调试器与 Behave.exe 一起使用? 调试配置参数是什么?visual studio code launch.json

【问题讨论】:

    标签: python-3.x visual-studio-code python-behave


    【解决方案1】:

    添加启动调试当前功能的配置:

    {
        "name": "Python: Behave current file",
        "type": "python",
        "request": "launch",
        "module": "behave",
        "console": "integratedTerminal",
        "args": [
            "${file}"
        ]
    },
    

    【讨论】:

    • 效果很好,非常感谢! (适用于 MacOS)请记住,您必须在 .feature 文件上启动它 - 这很奇怪,但很有意义
    【解决方案2】:

    使用以下launch.json 设置运行单个行为测试,方法是选择场景名称并按F5 或开始调试按钮。

        {
            "name": "Python: Behave",
            "type": "python",
            "request": "launch",
            "module": "behave",
            "console": "integratedTerminal",
            "args": [
                "--no-capture",
                "--no-capture-stderr",
                "--no-skipped",
                "${file}",
                "--tags=${selectedText}"
            ]
        }
    

    【讨论】:

    • 完美!谢谢!
    【解决方案3】:

    最后,我通过使用以下 Launch.json 调用 Python 主程序,成功调试了我的 Python 代码:

     {
    
      "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"
    
     },
    
    

    【讨论】:

      【解决方案4】:

      我可以看到你和我的launch.json 设置之间唯一不同的是我使用args 来设置测试目录而不是切换cwd

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

      【讨论】:

        【解决方案5】:

        你也可以使用这个配置

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

        【讨论】:

          【解决方案6】:

          我喜欢lindstromhenrik 的回答,因为它不需要更改当前工作目录。

          对于其他 Windows 用户(正如提出问题的用户),行为 .exe 程序位于“Scripts”文件夹中:

          {
              "name": "Python: Behave (.venv)",
              "type": "python",
              "request": "launch",
              "program": "${workspaceFolder}\\.venv\\Scripts\\behave.exe",
              "console": "integratedTerminal",
              "args": [
                "tests"
              ]
          }
          

          【讨论】:

            【解决方案7】:

            我使用 conda 环境,并使用 pip 在 %path% 上安装了行为。

            在我的环境中,我还有一个 tests/ 子目录,我在执行行为之前明确使用 cwd 更改路径。

            这是我的launch.json的摘录

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

            我在这里有效地尝试实现的是类似于命令行上的以下步骤:

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

            我安装了 VSCode for Python 扩展,它可以识别我的 conda 虚拟环境。

            我也在 Windows 机器上运行它,所以你的里程在 Linux 或 Mac 上可能会有所不同。

            【讨论】:

              【解决方案8】:
              {
                  // 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"
                      }
              
              
                  ]
              }
              

              【讨论】:

                【解决方案9】:

                添加 json。文件: 在 gerkin 场景中添加 @wip 标签。 在 Behave Current Scenerio 中选择调试 选择 Gerkin 场景并运行调试。

                {
                    // 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"
                            ]
                        }
                    ]
                }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2019-03-09
                  • 2018-11-27
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-09-20
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多