In your editor#

Since 0.10.0.

A VS Code extension marks the findings of the last test run on their lines (see below; in the VS Code Marketplace). Without it, the output is regular enough for the tools editors already have: a problem matcher turns the test run and the static check into entries of VS Code's Problems panel, and its SARIF file opens in a SARIF viewer. Nothing here is specific to a release of pytest-queryguard beyond the lines it reads, which are not a stable interface (see Stability): if a matcher stops matching after an upgrade, check this page.

VS Code: the extension#

Since 1.1.0 the repository has a VS Code extension in editors/vscode. It reads the --queryguard-json report (or the --queryguard-sarif file) of the last run and shows each finding as a squiggle and a Problems entry on its line. The hover has the SQL, how many times it ran, the suggested fix and the tests that found it; the quick actions open the documentation of the finding and, with a SARIF report, apply the fix that adds the loader option. Its commands run pytest on the current file, the test at the cursor or the whole project and mark what the run found; it also reloads the report whenever something else writes it, such as a run from the terminal.

python -m pytest --queryguard-json=queryguard.json

is all it needs from the project: queryguard.json in the workspace folder is the file it reads by default.

Install it from the VS Code Marketplace (Extensions view, search for pytest-queryguard), or with code --install-extension maciejziel.pytest-queryguard. It is not on Open VSX yet, so in VSCodium, Cursor and other editors that use Open VSX, install the .vsix that the repository's vscode workflow builds (the pytest-queryguard-vsix artifact of a run on main) with code --install-extension pytest-queryguard-<version>.vsix, or build it yourself; the settings, the limits and the build and publishing steps are in its README. The extension has its own version number and is not part of the package's API (see Stability).

VS Code: tasks with problem matchers#

Save as .vscode/tasks.json (or merge the two tasks into yours), then run Terminal > Run Task... and pick one. Each finding becomes an entry of the Problems panel and a squiggle on its line.

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "pytest (queryguard)",
      "type": "process",
      "command": "${command:python.interpreterPath}",
      "args": ["-m", "pytest", "-q"],
      "group": "test",
      "problemMatcher": {
        "owner": "queryguard",
        "source": "queryguard",
        "fileLocation": ["autoDetect", "${workspaceFolder}"],
        "severity": "error",
        "pattern": [
          {"regexp": "^  (\\S.*)$", "message": 1},
          {"regexp": "^  first at (.+):(\\d+) in (.+)$", "file": 1, "line": 2}
        ]
      }
    },
    {
      "label": "queryguard lint",
      "type": "process",
      "command": "${command:python.interpreterPath}",
      "args": ["-m", "pytest_queryguard.lint", "--models", "app.models", "app"],
      "problemMatcher": {
        "owner": "queryguard-lint",
        "source": "queryguard lint",
        "fileLocation": ["autoDetect", "${workspaceFolder}"],
        "severity": "warning",
        "pattern": {
          "regexp": "^(.+?):(\\d+):(\\d+): (.+)$",
          "file": 1,
          "line": 2,
          "column": 3,
          "message": 4
        }
      }
    }
  ]
}

VS Code: the SARIF file#

Microsoft's SARIF Viewer extension (MS-SarifVSCode.sarif-viewer) shows a SARIF file as a list of results, with the location, the message, the rule's help and, for the plugin's relationship-load findings, the fix it carries:

  1. Write the file: python -m pytest --queryguard-sarif=queryguard.sarif, or for the static check python -m pytest_queryguard.lint --models app.models --format sarif --output lint.sarif app.
  2. Open it in VS Code (File > Open File...): the extension opens its panel for files ending in .sarif. SARIF: Show Panel from the command palette opens the panel again.

The locations in the file are relative to the repository root when it was written in GitHub Actions, and to pytest's rootdir otherwise (see SARIF output); open the file from the folder they are relative to, or the viewer asks you to find the files.

PyCharm and other JetBrains IDEs#

The cheap path is narrower here:

Edit this page on GitHub