1 unstable release

0.1.0 Apr 21, 2024

#422 in Testing

MIT license

49KB
1K SLoC

anytest Lint Test

Run any test from your terminal.

Many test frameworks allow running a single test, but they all use different formats. The majority require to build quite complex queries that are hard to type and even remember. This tool aims to provide a unified way to run a single test by specifying only the file and the line number (following how RSpec/ExUnit/ESpec does that). The tool is heavily inspired by the awesome vim-test plugin.

Currently, the following test frameworks are supported (and more are coming soon):

Language Test framework Identifiers
Elixir ESpec, ExUnit espec, exunit
JavaScript Jest jest
Python pytest pytest
Ruby RSpec rspec
Rust Cargotest cargotest
Zig Zigtest zigtest

Feel free to open an issue with a test framework request as those test frameworks will be added first.

Installation

Currently, anytest is distributed via crates.io so Rust is required to install it. To install Rust, follow the instructions on the official Rust website.

To build anytest from source use:

cargo install anytest

Usage

It is as simple as running anytest <file>:<line>:

anytest tests/test_rust.rs:10

The tool supports the following scopes:

  • suite - run the whole suite
  • file - run all tests in the file
  • line - run the test at the specified line

When the the path is specified with the line number (e.g. anytest tests/test_rust.rs:10), the scope is automatically set to line. When the path is specified without the line number (e.g. anytest tests/test_rust.rs), the scope is automatically set to file.

The scope can be explicitly set with the --scope/-s flag:

anytest tests/test_rust.rs:10 -s file

By default, anytest uses the current working directory as the root directory. The root directory can be explicitly set with the --root/-r flag:

anytest anytest-cli/tests/test_rust.rs:10 -r anytest-cli

When the root directory is specified, the path to the test can be either relative to the root (e.g. anytest tests/test_rust.rs:10 -r anytest-cli) or relative to the current working directory (e.g. anytest anytest-cli/tests/test_rust.rs:10 -r anytest-cli).

To run the tool in the dry-run mode, use the --dry-run/ flag:

anytest tests/test_rust.rs:10 --dry-run

To see the list of all available options, run:

anytest --help

Usage with Zed

Even though anytest is a standalone tool, the main driver behind writing it was to use it with Zed. Currently, Zed has limited extension support, so the only way to integrate anytest with Zed is to use Zed tasks.

The repository contains an example tasks.json file that can be copied to the Zed configuration directory with the following command:

wget https://raw.githubusercontent.com/timfjord/anytest-cli/main/examples/tasks.json -O ~/.config/zed/tasks.json -nc

Please note that the command above won't overwrite the existing tasks.json file. Remove the -nc flag to overwrite the existing file.

Once the tasks are set up, either pick the task: spawn command from the Command Palette or add keybindings to run the tests.

[
  {
    "context": "Workspace",
    "bindings": {
      "cmd-j cmd-s": ["task::Spawn", { "task_name": "anytest: test suite" }],
      "cmd-j cmd-f": ["task::Spawn", { "task_name": "anytest: test file" }],
      "cmd-j cmd-l": ["task::Spawn", { "task_name": "anytest: test line" }]
    }
  }
]

Usage with other crates

anytest also acts as a library crate and can be used in other Rust projects.

Currently, it exposes the build_command function that builds a std::process::Command for the given context. See tests/test_api.rs for more details.

use anytest::{Context, Scope};

let context = Context::new(
    Some("tests/fixtures/cargotest/crate"),
    "tests/integration_test.rs",
    Some(3),
    Some(Scope::Line),
)
.unwrap();
let command = anytest::build_command(&context).unwrap();

assert_eq!(command.get_program(), "cargo");
assert_eq!(
    command
        .get_args()
        .map(|arg| arg.to_str().unwrap())
        .collect::<Vec<_>>(),
    vec![
        "test",
        "--test",
        "integration_test",
        "it_adds_two",
        "--",
        "--exact"
    ]
);

Contribution

The easiest way to add a new test framework is to find it either in the AnyTest repository or the vim-test repository and try to adapt it. It is also required to cover the test frameworks with tests. Tests and fixtures can be also found in either the AnyTest repository or the vim-test repository

Credits

anytest is heavily inspired by the vim-test plugin so all credits go to the authors and maintainers of this awesome Vim plugin.

Dependencies

~5–16MB
~213K SLoC