Skip to content

Instantly share code, notes, and snippets.

@Alkass
Last active April 19, 2017 20:22
Show Gist options
  • Select an option

  • Save Alkass/3ae4ca5b354329b209b6978f0617e88f to your computer and use it in GitHub Desktop.

Select an option

Save Alkass/3ae4ca5b354329b209b6978f0617e88f to your computer and use it in GitHub Desktop.
Minimal testing framework in Rust
// @Author: Fadi Hanna Al-kass
// @Brief: Minimal testing framework
// @Version: 0.2.3
#[derive(Debug)]
enum TestCaseStatus {
PASSED,
FAILED,
SKIPPED
}
struct TestCase {
title: &'static str,
description: &'static str,
timestamps: (f32, f32),
status: TestCaseStatus
}
impl TestCase {
fn new (title: &'static str, description: &'static str, timestamps: (f32, f32), status: TestCaseStatus) -> TestCase {
TestCase {
title: title,
description: description,
timestamps: timestamps,
status: status
}
}
}
type TestCasesList = Vec<TestCase>;
trait Testable {
fn get_test_results (&self) -> TestCasesList;
fn pass(&self, message: String) {
println!("PASS: {}", message);
}
fn fail(&self, message: String) {
println!("FAIL: {}", message);
}
fn warn(&self, message: String) {
println!("WARN: {}", message);
}
fn info(&self, message: String) {
println!("INFO: {}", message);
}
}
struct MyTestCase;
impl Testable for MyTestCase {
fn get_test_results(&self) -> TestCasesList {
let mut temp: Vec<TestCase> = vec![];
// TODO: Implement Test Case
self.pass(format!("all good so far"));
temp.push(TestCase{
title: "My first test case",
description: "no description",
timestamps: (0 as f32, 10 as f32),
status: TestCaseStatus::PASSED
});
temp
}
}
fn run_test_cases <T: Testable>(test_cases: Vec<T>) {
let mut total_count: i32 = 0;
let (mut passes, mut fails, mut skips): (i32, i32, i32) = (0, 0, 0);
for test_case in test_cases.iter() {
let test_results: Vec<TestCase> = test_case.get_test_results();
for result in test_results.iter() {
println!("Test Case No. {}: {}", total_count + 1, result.title);
println!(" * Description: {}", result.description);
println!(" * Test Duration: {} Second(s)", (result.timestamps.1 - result.timestamps.0));
println!(" * Status: {:?}", result.status);
match result.status {
TestCaseStatus::PASSED => passes += 1,
TestCaseStatus::FAILED => fails += 1,
TestCaseStatus::SKIPPED => skips += 1,
}
total_count += 1;
}
}
println!("\n-");
println!("Ran {} Test Case(s) in Total", total_count);
println!("{} Passes {} Fails {} SKIPS", passes, fails, skips);
}
fn main () {
run_test_cases(vec![MyTestCase{}]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment