xtd.tunit - Reference Guide  1.0.0
Modern c++17 unit testing framework on Windows, macOS, Linux, iOS and android.
xtd::tunit::event_listener Class Reference

Represent the event listener class. More...

#include <event_listener.h>

Inheritance diagram for xtd::tunit::event_listener:
xtd::tunit::ostream_event_listener

Public Member Functions

virtual void on_class_cleanup_end (const xtd::tunit::class_event_args &e) const
 Occurs when class cleanup is ended. More...
 
virtual void on_class_cleanup_start (const xtd::tunit::class_event_args &e) const
 Occurs when class cleanup is started. More...
 
virtual void on_class_end (const xtd::tunit::class_event_args &e) const
 Occurs when class is ended. More...
 
virtual void on_class_initialize_end (const xtd::tunit::class_event_args &e) const
 Occurs when class initialize is ended. More...
 
virtual void on_class_initialize_start (const xtd::tunit::class_event_args &e) const
 Occurs when class initialize is started. More...
 
virtual void on_class_start (const xtd::tunit::class_event_args &e) const
 Occurs when class is started. More...
 
virtual void on_test_aborted (const xtd::tunit::test_event_args &e) const
 Occurs when test is aborted. More...
 
virtual void on_test_cleanup_end (const xtd::tunit::test_event_args &e) const
 Occurs when test cleanup is ended. More...
 
virtual void on_test_cleanup_start (const xtd::tunit::test_event_args &e) const
 Occurs when test cleanup is started. More...
 
virtual void on_test_end (const xtd::tunit::test_event_args &e) const
 Occurs when test is ended. More...
 
virtual void on_test_failed (const xtd::tunit::test_event_args &e) const
 Occurs when test is failed. More...
 
virtual void on_test_ignored (const xtd::tunit::test_event_args &e) const
 Occurs when test is ignored. More...
 
virtual void on_test_initialize_end (const xtd::tunit::test_event_args &e) const
 Occurs when test initialize is ended. More...
 
virtual void on_test_initialize_start (const xtd::tunit::test_event_args &e) const
 Occurs when test initialize is ended. More...
 
virtual void on_test_start (const xtd::tunit::test_event_args &e) const
 Occurs when test initialize is started. More...
 
virtual void on_test_succeed (const xtd::tunit::test_event_args &e) const
 Occurs when test is succeed. More...
 
virtual void on_unit_test_cleanup_end (const xtd::tunit::tunit_event_args &e) const
 Occurs when unit test cleanup is ended. More...
 
virtual void on_unit_test_cleanup_start (const xtd::tunit::tunit_event_args &e) const
 Occurs when unit test is started. More...
 
virtual void on_unit_test_end (const xtd::tunit::tunit_event_args &e) const
 Occurs when unit test is ended. More...
 
virtual void on_unit_test_initialize_end (const xtd::tunit::tunit_event_args &e) const
 Occurs when unit test initalize is ended. More...
 
virtual void on_unit_test_initialize_start (const xtd::tunit::tunit_event_args &e) const
 Occurs when unit test initialize is ended. More...
 
virtual void on_unit_test_start (const xtd::tunit::tunit_event_args &e) const
 Occurs when unit test cleanup is started. More...
 

Detailed Description

Represent the event listener class.

Unit test call theses events when unit tests are processing.

Remarks
Override this class for create your own event listener.
Examples
The follow example shows how to create your own listener event for simulate gtest output.
#include <xtd/xtd.tunit>
#include <sstream>
using namespace std;
using namespace xtd::tunit;
// Simulate gtest event listener test output
class gtest_event_listener final : public event_listener {
private:
void on_unit_test_start(const xtd::tunit::tunit_event_args& e) const override {
cout << "[==========] Running " << e.unit_test().test_count() << " tests from " << e.unit_test().test_cases_count() << " test case." << endl;
}
//void on_unit_test_initialize_start(const xtd::tunit::tunit_event_args& e) const override {
// this->event_listener::on_unit_test_initialize_start(e);
//}
cout << "[----------] Global test environment set-up." << endl;
}
void on_class_start(const xtd::tunit::class_event_args& e) const override {
cout << "[----------] " << e.test_class().test_count() << " tests from " << e.test_class().name() << endl;
}
//void on_class_initialize_start(const xtd::tunit::class_event_args& e) const override {
// this->event_listener::on_class_initialize_start(e);
//}
//void on_class_initialize_end(const xtd::tunit::class_event_args& e) const override {
// this->event_listener::on_class_initialize_end(e);
//}
//void on_test_initialize_start(const xtd::tunit::test_event_args& e) const override {
// this->event_listener::on_test_initialize_start(e);
//}
//void on_test_initialize_end(const xtd::tunit::test_event_args& e) const override {
// this->event_listener::on_test_initialize_end(e);
//}
void on_test_start(const xtd::tunit::test_event_args& e) const override {
cout << "[ RUN ] " << e.test_class().name() << "." << e.test().name() << endl;
}
void on_test_succeed(const xtd::tunit::test_event_args& e) const override {
cout << "[ OK ] " << e.test_class().name() << "." << e.test().name() << " (" << e.test().elapsed_time().count() << " ms)" << endl;
}
//void on_test_aborted(const xtd::tunit::test_event_args& e) const override {
// this->event_listener::on_test_aborted(e);
//}
void on_test_failed(const xtd::tunit::test_event_args& e) const override {
cout << e.test().line_info().file_path() << ":" << e.test().line_info().line_number() << ": Failure" << endl;
if (e.test().actual() != "") cout << " Actual: " << e.test().actual() << endl;
if (e.test().expect() != "") cout << "Expected: " << e.test().expect() << endl;
if (e.test().message() != "") cout << e.test().message() << endl;
cout << "[ FAILED ] " << e.test_class().name() << "." << e.test().name() << " (" << e.test().elapsed_time().count() << " ms)" << endl;
}
//void on_test_ignored(const xtd::tunit::test_event_args& e) const override {
// this->event_listener::on_test_ignored(e);
//}
//void on_test_end(const xtd::tunit::test_event_args& e) const override {
// this->event_listener::on_test_end(e);
//}
//void on_test_cleanup_start(const xtd::tunit::test_event_args& e) const override {
// this->event_listener::on_test_cleanup_start(e);
//}
//void on_test_cleanup_end(const xtd::tunit::test_event_args& e) const override {
// this->event_listener::on_test_cleanup_end(e);
//}
//void on_class_cleanup_start(const xtd::tunit::class_event_args& e) const override {
// this->event_listener::on_class_cleanup_start(e);
//}
//void on_class_cleanup_end(const xtd::tunit::class_event_args& e) const override {
// this->event_listener::on_class_cleanup_end(e);
//}
void on_class_end(const xtd::tunit::class_event_args& e) const override {
cout << "[----------] " << e.test_class().test_count() << " tests from " << e.test_class().name() << " (" << e.test_class().elapsed_time().count() << " ms)" << endl;
}
//void on_unit_test_cleanup_start(const xtd::tunit::tunit_event_args& e) const override {
// this->event_listener::on_unit_test_cleanup_start(e);
//}
cout << endl << "[----------] Global test environment tear-down" << endl;
}
void on_unit_test_end(const xtd::tunit::tunit_event_args& e) const override {
cout << "[==========] " << e.unit_test().test_count() << " tests from " << e.unit_test().test_cases_count() << " test case ran. (" << e.unit_test().elapsed_time().count() << " ms total)" << endl;
cout << "[ PASSED ] " << e.unit_test().succeed_test_count() << " tests." << endl;
if (e.unit_test().failed_test_count()) {
cout << "[ FAILED ] " << e.unit_test().failed_test_count() << " test, listed below:" << endl;
for(string name : e.unit_test().failed_test_names()) {
cout << "[ FAILED ] " << name << endl;
}
cout << endl;
cout << " " << e.unit_test().failed_test_count() << " FAILED TEST" << endl;
}
if (!e.unit_test().failed_test_count() && e.unit_test().ignored_test_count()) cout << endl;
if (e.unit_test().ignored_test_count()) cout << " YOU HAVE " << e.unit_test().ignored_test_count() << " DISABLED TESTS" << endl << endl;
}
};
// Simulate gtest unit test test output
class gtest_unit_test final : public unit_test {
public:
gtest_unit_test() : unit_test(make_unique<gtest_event_listener>()) {}
};
namespace unit_tests {
class test_class_(test) {
public:
void test_method_(test_case1) {
}
void test_method_(test_case2) {
}
void test_method_(test_case3) {
assert::is_true(false, "Your message...");
}
void ignore_test_method_(test_case4) {
}
};
}
// The main entry point for the application.
int main() {
return gtest_unit_test().run();
// is same :
// return unit_test(std::make_unique<gtest_event_listener>()).run();
}
// This code can produce the following output:
//
// [==========] Running 3 tests from 1 test case.
// [----------] Global test environment set-up.
// [----------] 3 tests from test
// [ RUN ] test.test_case1
// [ OK ] test.test_case1 (0 ms)
// [ RUN ] test.test_case2
// [ OK ] test.test_case2 (0 ms)
// [ RUN ] test.test_case3
// !---OMITTED---!/xtd.tunit/examples/create_your_own_unit_test/src/create_your_own_unit_test.cpp:139: Failure
// Actual: false
// Expected: true
// Your message...
// [ FAILED ] ManualTest.TestCase3 (0 ms)
// [----------] 3 tests from test (0 ms)
//
// [----------] Global test environment tear-down
// [==========] 3 tests from 1 test case ran. (0 ms total)
// [ PASSED ] 2 tests.
// [ FAILED ] 1 test, listed below:
// [ FAILED ] test.test_case3
//
// 1 FAILED TEST
// YOU HAVE 1 DISABLED TESTS

Member Function Documentation

§ on_class_cleanup_end()

virtual void xtd::tunit::event_listener::on_class_cleanup_end ( const xtd::tunit::class_event_args e) const
inlinevirtual

Occurs when class cleanup is ended.

Parameters
class_event_argsContains class event parameters.

§ on_class_cleanup_start()

virtual void xtd::tunit::event_listener::on_class_cleanup_start ( const xtd::tunit::class_event_args e) const
inlinevirtual

Occurs when class cleanup is started.

Parameters
class_event_argsContains class event parameters.

§ on_class_end()

virtual void xtd::tunit::event_listener::on_class_end ( const xtd::tunit::class_event_args e) const
inlinevirtual

Occurs when class is ended.

Parameters
class_event_argsContains class event parameters.

§ on_class_initialize_end()

virtual void xtd::tunit::event_listener::on_class_initialize_end ( const xtd::tunit::class_event_args e) const
inlinevirtual

Occurs when class initialize is ended.

Parameters
class_event_argsContains class event parameters.

§ on_class_initialize_start()

virtual void xtd::tunit::event_listener::on_class_initialize_start ( const xtd::tunit::class_event_args e) const
inlinevirtual

Occurs when class initialize is started.

Parameters
class_event_argsContains class event parameters.

§ on_class_start()

virtual void xtd::tunit::event_listener::on_class_start ( const xtd::tunit::class_event_args e) const
inlinevirtual

Occurs when class is started.

Parameters
class_event_argsContains class_event parameters.

§ on_test_aborted()

virtual void xtd::tunit::event_listener::on_test_aborted ( const xtd::tunit::test_event_args e) const
inlinevirtual

Occurs when test is aborted.

Parameters
test_event_argsContains test event parameters.

Reimplemented in xtd::tunit::ostream_event_listener.

§ on_test_cleanup_end()

virtual void xtd::tunit::event_listener::on_test_cleanup_end ( const xtd::tunit::test_event_args e) const
inlinevirtual

Occurs when test cleanup is ended.

Parameters
test_event_argsContains test event parameters.

§ on_test_cleanup_start()

virtual void xtd::tunit::event_listener::on_test_cleanup_start ( const xtd::tunit::test_event_args e) const
inlinevirtual

Occurs when test cleanup is started.

Parameters
test_event_argsContains test event parameters.

§ on_test_end()

virtual void xtd::tunit::event_listener::on_test_end ( const xtd::tunit::test_event_args e) const
inlinevirtual

Occurs when test is ended.

Parameters
test_event_argsContains test event parameters.

§ on_test_failed()

virtual void xtd::tunit::event_listener::on_test_failed ( const xtd::tunit::test_event_args e) const
inlinevirtual

Occurs when test is failed.

Parameters
test_event_argsContains test event parameters.

Reimplemented in xtd::tunit::ostream_event_listener.

§ on_test_ignored()

virtual void xtd::tunit::event_listener::on_test_ignored ( const xtd::tunit::test_event_args e) const
inlinevirtual

Occurs when test is ignored.

Parameters
test_event_argsContains test event parameters.

Reimplemented in xtd::tunit::ostream_event_listener.

§ on_test_initialize_end()

virtual void xtd::tunit::event_listener::on_test_initialize_end ( const xtd::tunit::test_event_args e) const
inlinevirtual

Occurs when test initialize is ended.

Parameters
test_event_argsContains test event parameters.

§ on_test_initialize_start()

virtual void xtd::tunit::event_listener::on_test_initialize_start ( const xtd::tunit::test_event_args e) const
inlinevirtual

Occurs when test initialize is ended.

Parameters
test_event_argsContains test event parameters.

§ on_test_start()

virtual void xtd::tunit::event_listener::on_test_start ( const xtd::tunit::test_event_args e) const
inlinevirtual

Occurs when test initialize is started.

Parameters
test_event_argsContains test event parameters.

§ on_test_succeed()

virtual void xtd::tunit::event_listener::on_test_succeed ( const xtd::tunit::test_event_args e) const
inlinevirtual

Occurs when test is succeed.

Parameters
test_event_argsContains test event parameters.

Reimplemented in xtd::tunit::ostream_event_listener.

§ on_unit_test_cleanup_end()

virtual void xtd::tunit::event_listener::on_unit_test_cleanup_end ( const xtd::tunit::tunit_event_args e) const
inlinevirtual

Occurs when unit test cleanup is ended.

Parameters
tunit_event_argsContains unit test event parameters.

§ on_unit_test_cleanup_start()

virtual void xtd::tunit::event_listener::on_unit_test_cleanup_start ( const xtd::tunit::tunit_event_args e) const
inlinevirtual

Occurs when unit test is started.

Parameters
tunit_event_argsContains unit test event parameters.

§ on_unit_test_end()

virtual void xtd::tunit::event_listener::on_unit_test_end ( const xtd::tunit::tunit_event_args e) const
inlinevirtual

Occurs when unit test is ended.

Parameters
tunit_event_argsContains unit test event parameters.

Reimplemented in xtd::tunit::ostream_event_listener.

§ on_unit_test_initialize_end()

virtual void xtd::tunit::event_listener::on_unit_test_initialize_end ( const xtd::tunit::tunit_event_args e) const
inlinevirtual

Occurs when unit test initalize is ended.

Parameters
tunit_event_argsContains unit test event parameters.

§ on_unit_test_initialize_start()

virtual void xtd::tunit::event_listener::on_unit_test_initialize_start ( const xtd::tunit::tunit_event_args e) const
inlinevirtual

Occurs when unit test initialize is ended.

Parameters
tunit_event_argsContains unit test event parameters.

§ on_unit_test_start()

virtual void xtd::tunit::event_listener::on_unit_test_start ( const xtd::tunit::tunit_event_args e) const
inlinevirtual

Occurs when unit test cleanup is started.

Parameters
tunit_event_argsContains unit test event parameters.

Reimplemented in xtd::tunit::ostream_event_listener.


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