robottestingframework::TestCase Class Referenceabstract

The base class to implememnt a test case. More...

`#include <robottestingframework/TestCase.h>`

Inheritance diagram for robottestingframework::TestCase:
[legend]

## Public Member Functions

| TestCase (std::string name, std::string param="")
| TestCase constructor. More...
virtual | ~TestCase ()
| TestCase destructor. More...
virtual bool | setup (int argc, char **argv)
| setup is called before the test run. More...
virtual void | tearDown ()
| tearDown is called after the test run More...
virtual void | run ()=0
| run is called by the TestCase class if setup is successfull; More...
void | interrupt () override
| interrupt interrupts the current test run More...
void | run (TestResult &rsl) override
| the main caller of a TestCase inherited from Test Class. More...
bool | succeeded () const override
| succeeded More...
void | failed ()
| failed Sets the test successful flag to `false` which indicates that the test was not successful. More...
TestResult * | getResult ()
| getResult Returns an instance of TestResult if run(TestResult &result) has been already called by a TestRunner More...
void | setParam (const std::string param)
| setParam Sets the optional parameters of the test. More...
std::string | getParam ()
| getParam gets the original paramter string which is set for the test case More...
void | setEnvironment (const std::string environment)
| setEnvironment Optioanlly specifies the environment in which the test case is executed. More...
std::string | getEnvironment ()
| getParam gets the environment string which is set for the test case More...
void | setRepetition (unsigned int rep)
| setRepetition sets the run repetition by default the run() method is called only once; The number of test runs (e.g., for stress testing) can be set using this function. More...
unsigned int | getRepetition ()
| getRepetition gets the tun repetition More...
Public Member Functions inherited from robottestingframework::Test
| Test (std::string name, std::string description="")
| Test constructor. More...
virtual | ~Test ()
| Test destructor. More...
const std::string | getName () const
| getName Getting test name. More...
const std::string | getDescription () const
| getDescription Getting test description More...
void | setDescription (const std::string description)
| setDescription Sets an optional string which describes the test. More...

## Private Attributes

std::string | param
std::string | environment
TestResult * | result
bool | successful
bool | interrupted
unsigned int | repetition

## Additional Inherited Members

Protected Member Functions inherited from robottestingframework::Test
void | setName (std::string name)
| setName setting the test name More...

## Detailed Description

The base class to implememnt a test case.

The user defined test cases should be inherited from TestCase class and the `run()` method must be overriden. The `'setup(int argc, char** argv)'` and `tearDown()` can be also inherited to implement any initialization or finalization of the test case.

Here's an example of using a TestCase:

/*
* Robot Testing Framework
*
* Copyright (C) 2015-2019 Istituto Italiano di Tecnologia (IIT)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <robottestingframework/ConsoleListener.h>
#include <robottestingframework/TestAssert.h>
#include <robottestingframework/TestCase.h>
#include <robottestingframework/TestResult.h>
#include <robottestingframework/TestResultCollector.h>
#include <cstdio>
#include <iostream>
using namespace robottestingframework;
class MyTest : public TestCase
{
public:
MyTest() :
TestCase("MyTest")
{
}
bool setup(int argc, char** argv) override
{
ROBOTTESTINGFRAMEWORK_TEST_REPORT("running MyTest::setup...");
return true;
}
void tearDown() override
{
ROBOTTESTINGFRAMEWORK_TEST_REPORT("running MyTest::teardown...");
ROBOTTESTINGFRAMEWORK_ASSERT_ERROR("this is just for example!");
}
void run() override
{
ROBOTTESTINGFRAMEWORK_TEST_CHECK(2 < 3, "two is less than three");
ROBOTTESTINGFRAMEWORK_TEST_CHECK(2 == 3, "two is equal to three");
int a = 5;
int b = 3;
ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(a < b, Asserter::format("%d is not smaller than %d.", a, b));
}
};
int main(int argc, char** argv)
{
// create a test listener to collect the result
// and enable the verbose mode
ConsoleListener listener(true);
// create a collector to get computer readable
// test results
TestResultCollector collector;
// create a test result and add the listeners
TestResult result;
result.addListener(&listener);
result.addListener(&collector);
// calling a test case
MyTest atest;
atest.TestCase::run(result);
// return 0 if the test passed
// otherwise the number of failed test
return collector.failedCount();
}
ConsoleListener.h
TestAssert.h
ROBOTTESTINGFRAMEWORK_ASSERT_ERROR
#define ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(message)
Assertion with throwing error exception.
Definition: TestAssert.h:90
ROBOTTESTINGFRAMEWORK_TEST_CHECK
#define ROBOTTESTINGFRAMEWORK_TEST_CHECK(condition, message)
ROBOTTESTINGFRAMEWORK_TEST_CHECK combines ROBOTTESTINGFRAMEWORK_TEST_REPORT and ROBOTTESTINGFRAMEWORK...
Definition: TestAssert.h:188
ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE
#define ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(condition, message)
Conditional failure report.
Definition: TestAssert.h:149
ROBOTTESTINGFRAMEWORK_TEST_REPORT
#define ROBOTTESTINGFRAMEWORK_TEST_REPORT(message)
Reporting a message to the TestResult.
Definition: TestAssert.h:131
TestCase.h
TestResultCollector.h
TestResult.h
robottestingframework::Asserter::format
static std::string format(const char *msg,...)
format can be used to format data to in the same way that sprintf formats.
robottestingframework::TestCase::tearDown
virtual void tearDown()
tearDown is called after the test run
robottestingframework::TestCase::TestCase
TestCase(std::string name, std::string param="")
TestCase constructor.
robottestingframework::TestCase::run
virtual void run()=0
run is called by the TestCase class if setup is successfull;
robottestingframework::TestCase::result
TestResult * result
Definition: TestCase.h:159
robottestingframework::TestCase::setup
virtual bool setup(int argc, char **argv)
setup is called before the test run.
robottestingframework::TestResult::addListener
void addListener(TestListener *listener)
Adding a new listener.
robottestingframework
Definition: Arguments.h:25

Definition at line 41 of file TestCase.h.

## Constructor & Destructor Documentation

## TestCase()

robottestingframework::TestCase::TestCase | ( | std::string | name,
|  | std::string | param = `""`
| ) |  |

TestCase constructor.

Parameters

  name | The TestCase name
  param | The optional parameters for the test case

## ~TestCase()

virtual robottestingframework::TestCase::~TestCase | ( |  | ) |

virtual

TestCase destructor.

## Member Function Documentation

## failed()

void robottestingframework::TestCase::failed | ( |  | ) |

failed Sets the test successful flag to `false` which indicates that the test was not successful.

## getEnvironment()

std::string robottestingframework::TestCase::getEnvironment | ( |  | ) |

getParam gets the environment string which is set for the test case

Returns
  A environment string

## getParam()

std::string robottestingframework::TestCase::getParam | ( |  | ) |

getParam gets the original paramter string which is set for the test case

Returns
  a string paramter

## getRepetition()

unsigned int robottestingframework::TestCase::getRepetition | ( |  | ) |

getRepetition gets the tun repetition

Returns
  the number of repetitions

## getResult()

TestResult* robottestingframework::TestCase::getResult | ( |  | ) |

getResult Returns an instance of TestResult if run(TestResult &result) has been already called by a TestRunner

Returns
  an instance of TestResult

## interrupt()

void robottestingframework::TestCase::interrupt | ( |  | ) |

override virtual

interrupt interrupts the current test run

Reimplemented from robottestingframework::Test.

## run() [1/2]

virtual void robottestingframework::TestCase::run | ( |  | ) |

pure virtual

run is called by the TestCase class if setup is successfull;

Implemented in robottestingframework::plugin::RubyPluginLoaderImpl, robottestingframework::plugin::PythonPluginLoaderImpl, and robottestingframework::plugin::LuaPluginLoaderImpl.

## run() [2/2]

void robottestingframework::TestCase::run | ( | TestResult & | rsl | ) |

override virtual

the main caller of a TestCase inherited from Test Class.

Parameters

  result | an instance of a TestResult to collect the result of the test.

Implements robottestingframework::Test.

## setEnvironment()

void robottestingframework::TestCase::setEnvironment | ( | const std::string | environment | ) |

setEnvironment Optioanlly specifies the environment in which the test case is executed.

The TestCase class does not make any use of it. The environment can be used in the implementation of the test cases to know in which setup (e.g., simulation, real world environment) the test case is employed.

Parameters

  environment | The optional environment string

## setParam()

void robottestingframework::TestCase::setParam | ( | const std::string | param | ) |

setParam Sets the optional parameters of the test.

The parameter string is parsed into (argc/argv) format and will be passed to the test case using setup(int argc, char** argv).

Parameters

  param | the paramter string

## setRepetition()

void robottestingframework::TestCase::setRepetition | ( | unsigned int | rep | ) |

setRepetition sets the run repetition by default the run() method is called only once; The number of test runs (e.g., for stress testing) can be set using this function.

Parameters

  rep | number of repetitions

## setup()

virtual bool robottestingframework::TestCase::setup | ( | int | argc,
|  | char ** | argv
| ) |  |

virtual

setup is called before the test run.

Note
  the first value in argv is the name of the current testcases

Parameters

  argc | The number of the pasred argument from string paramter
  argv | The arguments which is parsed using string paramter

Returns
  true or false depending of the test initialization

Reimplemented in robottestingframework::plugin::RubyPluginLoaderImpl, robottestingframework::plugin::PythonPluginLoaderImpl, and robottestingframework::plugin::LuaPluginLoaderImpl.

## succeeded()

bool robottestingframework::TestCase::succeeded | ( |  | ) | const

override virtual

succeeded

Returns
  true or false representing whether the test was successful or not.

Implements robottestingframework::Test.

## tearDown()

virtual void robottestingframework::TestCase::tearDown | ( |  | ) |

virtual

tearDown is called after the test run

Reimplemented in robottestingframework::plugin::RubyPluginLoaderImpl, robottestingframework::plugin::PythonPluginLoaderImpl, and robottestingframework::plugin::LuaPluginLoaderImpl.

## Member Data Documentation

## environment

std::string robottestingframework::TestCase::environment

private

Definition at line 158 of file TestCase.h.

## interrupted

bool robottestingframework::TestCase::interrupted

private

Definition at line 161 of file TestCase.h.

## param

std::string robottestingframework::TestCase::param

private

Definition at line 157 of file TestCase.h.

## repetition

unsigned int robottestingframework::TestCase::repetition

private

Definition at line 162 of file TestCase.h.

## result

TestResult* robottestingframework::TestCase::result

private

Definition at line 159 of file TestCase.h.

## successful

bool robottestingframework::TestCase::successful

private

Definition at line 160 of file TestCase.h.

The documentation for this class was generated from the following file:

- src/robottestingframework/include/robottestingframework/TestCase.h