
Polish
Polish is a mini testing framework for the Rust system programming language meant to allow the writing of test-driven applications in a unified and expressive manner.
Getting Started
Add the following under [dependencies] inside your project's Cargo.toml file:
polish = { git = "https://github.com/alkass/polish", branch = "next" }
At minimum, you'll need the following:
extern crate polish;
use ;
use Logger;
TestCaseis the object you'll use to instantiate your test case(s)
TestCaseStatusis an enum of 4 possible options (PASSED,FAILED,SKIPPED, andUNKNOWN). One of these options will need to be returned from each test case, and therefore,TestCaseStatusmust be the return type of every test case you write.
The bare minimum for a syntactically valid test case would look like this:
new;
if you want to log some formatted output, you can use the logger object that's passed to your test case.
Logger has 4 logging functions you can use (pass, fail, info, warn). These functions take a message of type String which means you can pass formatted text to it using the format! macro, e.g.:
logger.info;
These logging statements are counted by the logger object, and the final statistics are shown to you at the end of the execution of each test case.
Note that the
failfunction doesn't cause your application to halt.
You can use the built-in
print!andprintln!macros to log your output just fine, butLoggerwill better format your logs.
Putting it All Together
To run a single test case, include the run_test function from polish::test_case as follows:
use run_test
Implement your test case as follows:
let my_test_case = new
And then pass your test case to the run_test function:
run_test;
And that's it! Your test case is ready to roll _
This test case produces the following output:

run_testhas a return value, but we'll get back to that later.
This single test case demo is available here
Working with Multiple Test Cases
If you have multiple test cases to work with, you can store them in a vector and pass them to run_tests instead of run_test, e.g.:
let my_test_cases = vec!;
run_tests;
This produces the following output:

This test case demo is available here
Writing Tests as Part of an Object
You may have the desire to implement your object each with its own set of test cases. For that, you need to implement the polish::test_case::Testable trait in each of your objects, then pass each object to a function called run_tests_from_class you can include from polish::test_case as well, e,g.:
;
run_tests_from_class;
This produces the following:

This test case demo is available here
More Statistics
All runner functions (
run_test,run_tests, andrun_tests_from_class) return aTestCaseResultsobject that can be passed to a function calledstatifyfor more statistical information, e,g.:
let res = run_tests_from_class;
statify;
This produces the following:

statifyreturns a boolean value that'strueif no test cases returnedFAILEDorUNKNOWN.