Skip to content

Adding tests has a high friction #15691

@kornelski

Description

@kornelski

Contributing anything to Cargo needs tests, but writing tests for Cargo is not easy.

Unit tests are usually quick to write, cheap to run. Unfortunately, a lot of code in Cargo needs a GlobalContext, Workspace, etc. and I don't see any helpers that would create mocked or temporary ones, and doing so seems non-trivial to do from scratch for a single test.

This means that majority of things need to use integration tests in testsuite, but these tests are slow.

These tests have such a high overhead, that I feel bad for adding them, because testing small things like a new error message is just not worth the cost of spawning an entire cargo process. It also needs several files, and unreasonable amount of boilerplate.

e.g.

use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::Project;

#[cargo_test]
fn case() {
    cargo_test_support::registry::init();
    let project = Project::from_template(current_dir!().join("in"));
    let project_root = project.root();
    let cwd = project_root.join("primary");

    snapbox::cmd::Command::cargo_ui()
        .arg("add")
        .arg_line("cargo-list-test-fixture-dependency --path ../dependency --base my_base")
        .current_dir(&cwd)
        .masquerade_as_nightly_cargo(&["path-base"])
        .assert()
        .success()
        .stdout_eq(str![""])
        .stderr_eq(file!["stderr.term.svg"]);

    assert_ui().subset_matches(current_dir!().join("out"), &project_root);
}
use cargo_test_support::compare::assert_ui;
use cargo_test_support::current_dir;
use cargo_test_support::file;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::Project;

#[cargo_test]
fn case() {
    cargo_test_support::registry::init();
    let project = Project::from_template(current_dir!().join("in"));
    let project_root = project.root();
    let cwd = project_root.join("primary");

    snapbox::cmd::Command::cargo_ui()
        .arg("add")
        .arg_line("cargo-list-test-fixture-dependency --path ../dependency --base my_base")
        .current_dir(&cwd)
        .masquerade_as_nightly_cargo(&["path-base"])
        .assert()
        .success()
        .stdout_eq(str![""])
        .stderr_eq(file!["stderr.term.svg"]);

    assert_ui().subset_matches(current_dir!().join("out"), &project_root);
}

These differ by a single line! But each test requires several files itself, and some SVG!? I don't see any documentation for how to make the SVG required by these tests.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions