zed icon indicating copy to clipboard operation
zed copied to clipboard

Test runner: Ability to customize test command

Open ytham opened this issue 1 year ago • 7 comments

Check for existing issues

  • [X] Completed

Describe the feature

I'd like the ability to customize the test runner command so that it can display backtraces. Right now, when I click the ▷ symbol next to a Rust test, it will run

cargo test -p <crate> <testname> -- --nocapture

But it'd sure be nice to be able to add/remove flags, tmp env vars, etc.

If applicable, add mockups / screenshots to help present your vision of the feature

No response

ytham avatar Sep 26 '24 14:09 ytham

I think you might be able to do this by creating a task and then adding "tags": ["rust-test"] to it.

See: Binding runnable tags to task templates in the docs.

notpeter avatar Oct 10 '24 15:10 notpeter

Hello, could you mind tell how to add custom flag to rust-test, after reading the docs, I couldn't managed to do it. I use the follwing to test if the default task can be replaced by my definition, but the triangle button does the default task instead of my definition.

  {
    "label": "my rust test",
    "command": "echo $ZED_CUSTOM_RUST_PACKAGE",
    "tags": ["rust-test"]
  }

And I have no idea of how to get the full name of current test function name.

sunliang711 avatar Mar 16 '25 13:03 sunliang711

I don't know how to replace my pytest command too. I tried using "python-test", "pytest", "pytest-test" and it still run the default command.

Tom-Boscher avatar Apr 16 '25 18:04 Tom-Boscher

@Tom-Boscher There are different tags for different levels within the test file. There's also support for different frameworks.

You would want to specify either python-pytest-class or python-pytest-method.

Discoverable here: https://github.com/zed-industries/zed/blob/95a1a8632dc102eea6afe1c3a15be8df26e5bcbc/crates/languages/src/python.rs#L465

joe-p avatar May 09 '25 11:05 joe-p

Thanks @joe-p , this helped me work around the issue in #29807 where the python toolchain selector no longer works. In tasks.json, for pytest you can do something like:

[
  {
    "label": "pytest runner",
    "command": "pyenv activate your-python-env && pytest $ZED_CUSTOM_PYTHON_TEST_TARGET",
    "tags": ["python-pytest-class", "python-pytest-method"]
  }
]

Note you have to restart zed every time you change tasks.json for it to take effect.

all-iver avatar May 10 '25 22:05 all-iver

Thank you @joe-p !

Tom-Boscher avatar May 11 '25 11:05 Tom-Boscher

Here is what i use for rust test with backtrace:

.zed/tasks.json:

[
  {
    "label": "rust test with backtrace",
    "command": "cargo test -p '$ZED_CUSTOM_RUST_PACKAGE' --test '$ZED_CUSTOM_RUST_TEST_FRAGMENT' -- --exact '$ZED_CUSTOM_RUST_TEST_NAME'",
    "env": {
      "RUST_BACKTRACE": "full"
    },
    "tags": ["rust-test"]
  },
  {
    "label": "rust lib test with backtrace",
    "command": "cargo test -p '$ZED_CUSTOM_RUST_PACKAGE' --lib '$ZED_CUSTOM_RUST_TEST_NAME'",
    "env": {
      "RUST_BACKTRACE": "full"
    },
    "tags": ["rust-test"]
  }
]

With it you can run task: spawn command search for rust test with backtrace while the selection is inside some test and it will run it.

dkuhnert avatar May 13 '25 07:05 dkuhnert

I'm trying to achieve the following snippet from VSCode's project settings.json file with my python project:

{
    "python.testing.pytestArgs": [
        "tests",
        "--reuse-db",
        "--disable-warnings",
        "--ds=conf.settings.test"
    ],
}

Anyone know if this is possible in Zed?

dabeeeenster avatar Jul 18 '25 07:07 dabeeeenster

@all-iver

Note you have to restart zed every time you change tasks.json for it to take effect.

This was an invaluable comment, thank you.

vittorius avatar Oct 14 '25 07:10 vittorius