Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug individual Django tests in vscode?

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.

like image 406
Albert Rothman Avatar asked Jul 02 '19 19:07

Albert Rothman


People also ask

How do you debug test cases in VS Code?

Once you have your launch configuration set, start your debug session with F5. Alternatively, you can run your configuration through the Command Palette (Ctrl+Shift+P) by filtering on Debug: Select and Start Debugging or typing 'debug ' and selecting the configuration you want to debug.


1 Answers

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.

like image 152
Niels Avatar answered Sep 18 '22 16:09

Niels