dirk.ndrvn.nl

Automatically Run Python Tests on Saving in VS Code

I was preparing a conference talk with some live coding in Python, to show how the short TDD loop can work. To speed up the process, I wanted VS Code to automatically run pytest whenever I save a .py file.

First I tried the ‘continuous run’ feature, but I quickly found out that I had no Python/pytest provider for continuously running tests. The solution I found uses the extra VS Code extension pucelle.run-on-save, which provides a feature to run VS Code commands.

Once that extension is active and the test explorer is configured for pytest, I configured the “Run on Save” extension to refresh the test explorer and run all tests in the workspace settings.json:

    "runOnSave.commands": [
        {
            "languages": ["python"],
            "command": "testing.refreshTests",
            "runIn": "vscode",
        },
        {
            "languages": ["python"],
            "command": "testing.runAll",
            "runIn": "vscode",
        },
    ]

#Python #Vs-Code