Zed: specify custom parameters for pytest

I am trying to replace VS Code with Zed for all my dev work.

I was pleasantly surprised to find that it supports running pytest out of the box. However, it didn't work well for my particular workflow: I need my tests to use custom environment variables for connecting to the test database and such.

In VS Code, I could solve this like so:

  1. Create .vscode/python_env_test with my custom variables.
  2. Add "python.envFile": "${workspaceFolder}/.vscode/python_env_test" to .vscode/settings.json. However, with this setting, VS Code will use the env file for running all Python code, including my Flask app which needs different settings.
  3. To fix that, I created a custom configuration for running Flask in .vscode/launch.json that specifies its own env file: "envFile": "${workspaceFolder}/.vscode/python_env".

This setup is awkward, but it works.

With Zed, it is possible to override default test runners with custom tasks. And custom tasks allow having custom environment variables, just what I need!

All I needed to do was create a custom task definition in .zed/tasks.json like so:

[
  {
    "label": "pytest (custom)",
    "command": "$ZED_CUSTOM_PYTHON_ACTIVE_ZED_TOOLCHAIN",
    "args": ["-m", "pytest", "$ZED_CUSTOM_PYTHON_TEST_TARGET"],
    "env": {
      "MY_CUSTOM_VAR": "A_NICE_VALUE"
    },
    "tags": ["python-pytest-class", "python-pytest-method"],
    "cwd": "$ZED_WORKTREE_ROOT"
  }
]

Now I have a custom test runner for my tests.

I tried running a test and it worked like a charm; the variable was set as expected. However, running it in debug mode failed: the variable was missing..

After digging in, I found that this was a bug specific to Python test runners (other languages like Go worked fine), so I opened a pull request to fix it. This marks my first contribution to Zed.