kat 0.1.0

Framework for known-answer tests
Documentation
  • Coverage
  • 96.3%
    26 out of 27 items documented1 out of 1 items with examples
  • Size
  • Source code size: 36.85 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 6.73 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 24s Average build duration of successful builds.
  • all releases: 24s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • CXNNIBVL/kat-rs
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • CXNNIBVL

kat-rs

Testing framework for known answer tests.

This crate aims to drastically reduce the boilerplate code associated with rust tests, as well as to make known-answer tests easier to write and extend.

This framework splits the tests into the test implementation and data, which is stored in .toml files.

Under the hood, Kat uses Serde and Toml-rs to deserialize test data.

kat-rs in action

Toml file layout

File WORKSPACE_ROOT/tests/data/my_data.toml

# In this section global variables are defined.

[global]

my_global_var = "This is a global variable"

my_custom_string_holder = "String Holder"



# In these sections we define test cases.

# Each test owns its own data. 

# Though every test must have the same

# data signature

[[test]]

id = 0

data = "This is data for test 0"

input = 24.8

expected = 24.8



# Multiple tests can be defined with

# consecutive "test" tables

[[test]]

id = 1

data = "This is data for test 1"

input = 3.1415

expected = 3.1415

Writing the tests

use kat::{
    types,
    kat_cfg,
    global,
    test,
    run,
};

// Some custom struct
struct CustomStringHolder {
    s: String,
    v: usize
}

impl_deserialize_from_toml_string!(
    |s| -> CustomStringHolder {
        CustomStringHolder {
            s, 
            v: 12
        }
    }
); 

// Path configuration relative to WORKSPACE_ROOT
kat_cfg(tests/data/my_data.toml);

// Define global variables
global! {
  my_global_var: types::TomlString,
  my_custom_string_holder: CustomStringHolder
}

// Define Test variables
test! {
    id: types::TomlInt,
    data = types::TomlString,
    input = types::TomlFloat
    expected = types::TomlFloat
}

// Implement Test Runner
run! {
  #[should_panic(expected = "x, y, not equal")]  
  |global, test| -> {
    
    println!("{}", global.my_global_var);
    println!("{}", global.my_custom_string_holder.s);
    assert_eq!(test.input, test.expected);

    let (x, y) = (10, 15);
    assert_eq!(x, y, "x, y, not equal");

  }

}