class::test::Test

class test::Test
  : public api::WorkObject

The Test class is designed to be inherited to implement the following tests:

  • API Tests: Should be designed to ensure the API is documented correctly and works as expected
  • Performance Tests: Should be designed to run the same set of operations over and over
  • Stress Tests: Should be designed to stress the system and check for unexpected behavior
  • Additional Tests: Used as needed by inheriting classes to run additional tests

    #include <sapi/test.hpp>
    
    u32 o_execute_flags = Test::EXECUTE_ALL;
    Test::initialize(cli.name(), cli.version());
    if( o_flags & DATA_TEST_FLAG ){
    Test test("generic test");  //Must be constructed after Test::inialize()
    test.execute(o_execute_flags);
    //At this point test will be deconstructed -- must happen before Test::finalize()
    }
    Test::finalize();
    

See the sostest suite of applications for examples (like DataTest).

Here is an example report in JSON format:

{
  "system": {
    "name": "Stratify Alpha",
    "arch": "v7em_f4sh",
    "sys version": "1.5",
    "kernel version": "3.5.0",
    "memory size": "16384",
    "id": "-KZKdVwMXIj6vTVsbX56",
    "serial": "F5001942555D5A3EAEAE24601801F01A"
  },
  "test": {
   "name": "api-var-test",
   "version": "0.1"
  },
  "var::Data": {
    "api": {
      "msg-0": "fill test complete",
      "msg-1": "alloc test complete",
      "result": true,
      "microseconds": 10900.0
    },
    "result": true,
    "microseconds": 11172.0
  }
}

Summary

Test(

const arg::Name & name,
Test * parent
)

~Test()

bool case_result() const

void close_case(

bool result
)

void close_case()

void execute(

const sys::Cli & cli
)

virtual void execute(

int o_flags
)

virtual void execute_additional_cases()

void execute_api_case()

virtual bool execute_class_api_case()

virtual bool execute_class_performance_case()

virtual bool execute_class_stress_case()

void execute_performance_case()

void execute_stress_case()

void open_case(

const var::ConstString & case_name
)

bool result() const

void set_case_failed()

static void finalize()

static void initialize(

const arg::Name name,
const arg::VersionEncodedString version,
const arg::GitHash git_hash
)

static u32 parse_options(

const sys::Cli & cli
)

protected const chrono::Timer & case_timer() const

protected chrono::Timer & case_timer()

protected int indent() const

protected void print_case_failed(

const char * fmt,

)

protected void print_case_failed(

const api::Result & result,
int line
)

protected void print_case_message(

const char * fmt,

)

protected void print_case_message_with_key(

const var::ConstString & key,
const char * fmt,

)

protected void print_case_score()

protected u32 score() const

enum @23

Members

Test(

const arg::Name & name,
Test * parent
)

Constructs a new test object.

Parameters

  • name The name of the test

  • parent The parent test or zero

The test must be constructed after Test::initialize().

//the WRONG way
int main(int argc, char * argv[]){
  Test my_test("my empty test"); //DON'T CONSTRUCT BEFORE Test::initialize()
  Test::initialize();
  my_test.execute();
  Test::finalize();

  return 0; //my_test is deconstructed after Test::finalize() -- WRONG!
}

//the RIGHT way
int main(int argc, char * argv[]){
  bool is_run_test = true;
  Test::initialize();
  if(is_run_test){
    Test my_test("my empty test"); //DON'T CONSTRUCT BEFORE Test::initialize()
    my_test.execute();
    //my_test will deconstruct here BEFORE Test::finalize() --RIGHT
  }
  Test::finalize();
  return 0;
}

If a Test is constructed inside the case of another test, “this” should be used as the value of the parent.


~Test()

Deconstructs the test object.

The object must be deconstructed before Test::finalize() is executed.


bool case_result() const

Returns the current value of the case result.

When a case is opened, the case result is set to true (passing).

Calling set_case_failed() or print_case_failed() will set the case result value to false.

case_result() is the default return value of close_case().


void close_case(

bool result
)

Closes a test case.

Parameters

  • result true if the test case passed and false if it failed

This method is only used when re-implementing execute_additional_cases().


void close_case()

Closes the test case using the current case_result() value.


void execute(

const sys::Cli & cli
)


virtual void execute(

int o_flags
)

Executes the tests specified by o_flags.

Parameters


virtual void execute_additional_cases()

Executes additional test cases.

By default there are not additional cases. The inheriting class should reimplement this method it additional test cases are used.

void MyClassTest::execute_additional_cases(){
  open_case("first test case");
  set_case_message("message", "test case message");
  close_case(true); //case passed

  open_case("second test case");
  set_case_message("message", "test case message");
  close_case(false); //case failed
}

void execute_api_case()

Executes the API test case.


virtual bool execute_class_api_case()

Executes the class api test.

This method should be overridden by the inheriting class.


virtual bool execute_class_performance_case()

Executes the class performance test.

This method should be overridden by the inheriting class.


virtual bool execute_class_stress_case()

Executes the class stress test.

This method should be overridden by the inheriting class.


void execute_performance_case()

Executes the performance test case.


void execute_stress_case()

Executes the stress test case.


void open_case(

const var::ConstString & case_name
)

Opens a new test case.

Parameters

  • case_name The name of the case

This method is only used when re-implementing execute_additional_cases().


bool result() const

Returns the results of the test.

If any single test case has a false result, the result of the test is false. Otherwise, the result is true.


void set_case_failed()

Sets the current case result to failed.


static void finalize()

Finalizes the test report.

This should be called after the last test has been deconstructed.


static void initialize(

const arg::Name name,
const arg::VersionEncodedString version,
const arg::GitHash git_hash
)

Initializes the test report.

This must be called before any tests are even constructed.


static u32 parse_options(

const sys::Cli & cli
)

Parse command line options for running test types.

This method searches for the following options and returns a value that has which tests to execute.

  • -execute_all
  • -api
  • -performance
  • -stress
  • -additional

    #include <sapi/test.hpp>
    #include <sapi/sys.hpp>
    
    int main(int argc, char * argv[]){
    Cli cli(argc, argv);
    u32 o_execute_flags = Test::parse_options(cli);
    
    Test::initialize(cli.name(), cli.version());
    
    if( o_execute_flags ){
    Test test("do nothing");
    test.execute(o_execute_flags);
    }
    
    Test::finalize();
    
    return 0;
    }
    

protected const chrono::Timer & case_timer() const

Returns a reference to the case timer.

The case timer is used to time each case. It is started and stopped automatically.

Test methods can access the value of the case timer while the test is running.


protected chrono::Timer & case_timer()

protected int indent() const

protected void print_case_failed(

const char * fmt,

)


protected void print_case_failed(

const api::Result & result,
int line
)


protected void print_case_message(

const char * fmt,

)

Prints a message to the test report.

Parameters

  • fmt Formatted string with variable arguments.

The key to the message is assigned automatically as

  • msg-0
  • msg-1
  • msg-2
  • msg-…

protected void print_case_message_with_key(

const var::ConstString & key,
const char * fmt,

)

Prints a message to the test report.

Parameters

  • key The key value for the saved message

  • fmt Formatted string with variable arguments.


protected void print_case_score()

protected u32 score() const

enum @23
  • EXECUTE_API API Test Execution flag
  • EXECUTE_STRESS Stress Test Execution flag
  • EXECUTE_PERFORMANCE Performance Test Execution flag
  • EXECUTE_ADDITIONAL Additional Test Execution flag
  • EXECUTE_ALL API Execution flag

X

Thanks for Coming!

Subscribe to news and updates