How do I debug individual Django tests in vscode?

僤鯓⒐⒋嵵緔 提交于 2020-06-17 08:55:08

问题


I added a launch configuration that allows me to run all tests in Django and another that allows me to run the server, both of these work fine.

I am looking for a way to debug an individual file, but using ${file} in the arguments gives a normal path which django doesn't like.

I want a way to change ${file} into a python path so that I can debug my tests on a single file.

python manage.py test --noinput --keepdb python.path.to.my.file

works in the command line.

The following configuration seems to be almost right:

      {   "name": "Test File",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "test",
                "--noinput",
                "--keepdb",
                "${file}"
            ],
            "django": true
        }

However, when I run this configuration I get an error, which I think is because ${file} turns into

path/to/my/file instead of path.to.my.file.


回答1:


There is no translation of file path to dotted name, so you will need to hard-code that in your launch.json.




回答2:


If you're on a Mac or Linux, the following launch config should work for a single unit test executed by Django:

    {
        "name": "Python: Django Debug Single Test",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}/manage.py",
        "args": [
            "test",
            "`echo -n ${relativeFileDirname} | tr \/ .`.${fileBasenameNoExtension}"
        ],
        "django": true
    },

This uses the tr command to translate / into . in the relative path.




回答3:


If you're using Windows and the git bash, the launch config with tr will also work, but you'll need to quote the variable substitution and quote the double backslashes.

    {
        "name": "Python: Django Debug Single Test",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}/manage.py",
        "args": [
            "test",
            "`echo -n \"${relativeFileDirname}\" | tr \\\\ .`.${fileBasenameNoExtension}"
        ],
        "django": true
    },


来源:https://stackoverflow.com/questions/56858808/how-do-i-debug-individual-django-tests-in-vscode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!