How to use VS Code debugger while running Unit Tests in Frappe (Erp Next)

In frappe we can enable vs code debugging while running the bench as described here: https://github.com/frappe/erpnext/wiki/VSCode-Debugging-for-Frappe-Python

However, how do we setup debugging while running unit tests. The Unit Testing support in frappe is described here : https://frappeframework.com/docs/user/en/guides/automated-testing/unit-testing

We are running unit test of individual module we work on with the following similar command:

bench run-tests --module \
"custom_app.doctype.custom_doctype.test_custom_file" \
--skip-test-records --skip-before-tests

The tests execute fine, when we run the above command in the command line. However, how do We enable debugger for the same in VS Code, similar to the way it is done while running the bench?

I was able to achieve this by creating a new configuration based on the exact setup linked above:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Bench Single Module Runner",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/frappe/frappe/utils/bench_helper.py",
            "args": [
                "frappe", "run-tests", "--module", "erpnext.accounts.doctype.tax_withholding_category.test_tax_withholding_category"
            ],
            "python": "${workspaceFolder}/../env/bin/python",
            "cwd": "${workspaceFolder}/../sites",
            "env": {
                "DEV_SERVER": "1"
            }
        }
    ]
    }

It does not support dynamically building the current module, e.g. you need to go into launch.json paste in whatever module you want to debug instead of tax_witholding_category. I think it is possible to have this dynamically build the module based on whatever file you are currently focused on but it didn’t seem immediately easy using the environment variables vscode makes available to you.

1 Like