#testing #cargo #binary #cargo-subcommand #cargo-test

nightly bin+lib cargo-is-tested

Ensure that your items are checked with high-quality testing!

2 releases

0.1.1 Jan 2, 2023
0.1.0 Jan 2, 2023

#614 in Cargo plugins

MIT license

28KB
451 lines

cargo-is-tested


cargo-is-tested is a way to check which of your items are tested and which aren't, very configurable and with a scripting-friendly API ready to use with your Git hooks.

Also, is uses ✨ pretty colors ✨ for error reporting.

You can also read the book for a more user-friendly guide.

Example

To check if all functions are tested in the current directory.

$ cargo is-tested .
# Cargo.toml
# [...]

[dependencies]
is_tested = "0.1.1" # Check crates.io for the current version of the crate.

# [...]
// src/main.rs

#! is-tested functions
// Yes, it uses shebangs to enable testing and flags!

use is_tested::is_tested;

#[is_tested("test/myfunction_testing_path.rs")]
fn my_function() {
	// [...]
}

Then, it will check if test/myfunction_testing_path.rs exists, if it doesn't, it will output an error.

If the file exists, the program checks all lints against your test, assuring the best quality possible. For example, the lint emptiness will check that your functions aren't empty.

Installation and usage

To get started using cargo-is-tested, install the binary.

$ cargo install cargo-is-tested

Now document yourself about all the lints you can apply to your tests. You can also use strict to activate them all (currently, recommendable)

More lints will be added with time.


Once you know the lints you want to apply, import the attribute is_tested, then choose a struct, function or any item that you want to test, and add to that item the attribute #[is_tested("<path to the test>.rs")]

The result should be something like:

#! is-tested strict

use is_tested::is_tested;

#[is_tested("tests/mystruct.rs")]
struct MyStruct<'a> {
	string: &'a str
}

Don't worry, the attribute won't change anything in your code, it's just a marker for a later-parser to know that you're testing the item.


It's time to run the parser, it will read check that all tested items are tested, and with the correct code quality dicted using the lints.

// tests/mystruct.rs

use mycrate::MyStruct;

fn main() {
	// [...]
}
$ cargo is-tested .

This will check that all tests are well written. You can use flags to customize your experience, for example, use --structs to check that all structs have tests associated, or use --test to, if all tests are confirmed, run cargo test automatically.


Stargazers

Stargazers repo roster for @blyxyas/cargo-is-tested

License

cargo-is-tested uses the MIT License, 2023. You can check the LICENSE for more information about licensing.

Dependencies

~4–16MB
~166K SLoC