From 49e06166af5d2cec77b0b8eec169fd9f2afcaaaa Mon Sep 17 00:00:00 2001 From: Tomasz Iwanek Date: Wed, 17 Jul 2013 16:20:53 +0200 Subject: [PATCH] [CodeCovarage] Automatic tests for DPL test framework [Issue#] LINUXWRT-664 [Feature] Tests/tests fix [Cause] Those tests are returning actual result of tests. Returned result should be catch and compared with expected. That comparsion should give pass or fail. [Solution] Add macro for catching tests results to make tests automatic. [Verification] Build "WITH_TESTS ON" and "WITH_CHILD ON". Run: wrt-commons-tests-test-runner-child --output=text --regexp='t0' wrt-commons-tests-test-runner-child --output=text --regexp='t1' All should pass (binary may have name wrt-commons-tests-test if previous do not exist). Change-Id: Ie7b008813b666ceb75de28119286e497d1c4b7ac --- modules/test/src/test_runner_child.cpp | 6 +++ tests/test/runner_child.cpp | 86 +++++++++++++++++++++++++++++----- 2 files changed, 81 insertions(+), 11 deletions(-) diff --git a/modules/test/src/test_runner_child.cpp b/modules/test/src/test_runner_child.cpp index bf773c6..223a47a 100644 --- a/modules/test/src/test_runner_child.cpp +++ b/modules/test/src/test_runner_child.cpp @@ -272,6 +272,9 @@ void RunChildProc(TestRunner::TestCase procChild) if (code == 0) { throw TestRunner::TestFailed(message); } + if (code == 2) { + throw TestRunner::TestFailed("Ignored"); + } } else { // child code @@ -296,6 +299,9 @@ void RunChildProc(TestRunner::TestCase procChild) } catch (DPL::Test::TestRunner::TestFailed &e) { msg = e.GetMessage(); code = 0; + } catch (DPL::Test::TestRunner::Ignored &e) { + msg = e.GetMessage(); + code = 2; } catch (...) { // Pokemon Catch... cache them all... msg = "unhandled exeception"; code = 0; diff --git a/tests/test/runner_child.cpp b/tests/test/runner_child.cpp index d911270..6dfc30d 100644 --- a/tests/test/runner_child.cpp +++ b/tests/test/runner_child.cpp @@ -20,35 +20,93 @@ * @brief Implementation file for test cases for engine internal tests */ #include +#include #include #include #include #include + +namespace { +enum class TestResult +{ + PASS, + FAIL, + IGNORED, + TIMEOUT, + UNKNOWN +}; +} + +#define RUNNER_CHILD_TEST_EXPECT(name, result, message) \ + static void testExpectFunction##name(); \ + RUNNER_TEST(name) \ + { \ + TestResult eResult = result; \ + TestResult rResult = TestResult::UNKNOWN; \ + std::string eMessage = message; \ + Try \ + { \ + DPL::Test::RunChildProc(&testExpectFunction##name); \ + } \ + Catch(DPL::Test::TestRunner::TestFailed) \ + { \ + std::string rMessage = _rethrown_exception.GetMessage(); \ + size_t pos = rMessage.find(")"); \ + if(pos != std::string::npos && pos+2 <= rMessage.length()) \ + { \ + rMessage = rMessage.substr(pos+2); \ + } \ + if(rMessage == "Timeout") \ + { \ + rResult = TestResult::TIMEOUT; \ + } \ + else if(rMessage == "Ignored") \ + { \ + rResult = TestResult::IGNORED; \ + } \ + else if(rMessage == eMessage) \ + { \ + rResult = TestResult::FAIL; \ + } \ + else \ + { \ + RUNNER_ASSERT_MSG(false, "Fail message do not matches"); \ + } \ + } \ + if(rResult == TestResult::UNKNOWN) \ + { \ + rResult = TestResult::PASS; \ + } \ + RUNNER_ASSERT_MSG(eResult == rResult, "Expected other result"); \ + } \ + void testExpectFunction##name() \ + + RUNNER_TEST_GROUP_INIT(DPL_TESTS_TEST_CHILD) -RUNNER_TEST(t00_pass) +RUNNER_CHILD_TEST_EXPECT(t00_pass, TestResult::PASS, "") { RUNNER_ASSERT_MSG(1, "This test should pass"); } -RUNNER_CHILD_TEST(t01_pass) +RUNNER_CHILD_TEST_EXPECT(t01_pass, TestResult::PASS, "") { RUNNER_ASSERT_MSG(1, "This test should pass"); } -RUNNER_CHILD_TEST(t02_fail) +RUNNER_CHILD_TEST_EXPECT(t02_fail, TestResult::FAIL, "This test should fail") { RUNNER_ASSERT_MSG(0, "This test should fail"); } -RUNNER_CHILD_TEST(t03_fail_timeout) +RUNNER_CHILD_TEST_EXPECT(t03_fail_timeout, TestResult::TIMEOUT, "") { sleep(20); RUNNER_ASSERT_MSG(1, "This test should fail"); } -RUNNER_CHILD_TEST(t04_fail) +RUNNER_CHILD_TEST_EXPECT(t04_fail, TestResult::FAIL, "This test should fail") { RUNNER_ASSERT_MSG(1, "This test should fail"); RUNNER_ASSERT_MSG(1, "This test should fail"); @@ -57,35 +115,41 @@ RUNNER_CHILD_TEST(t04_fail) RUNNER_ASSERT_MSG(0, "This test should fail"); } -RUNNER_CHILD_TEST(t05_fail_child_died) +RUNNER_CHILD_TEST_EXPECT(t05_fail_child_died, TestResult::FAIL, "Reading pipe error") { kill(getpid(), SIGKILL); RUNNER_ASSERT_MSG(1, "This test should fail"); } -RUNNER_CHILD_TEST(t06_pass_8_second_test) +RUNNER_CHILD_TEST_EXPECT(t06_pass_8_second_test, TestResult::PASS, "") { sleep(8); RUNNER_ASSERT_MSG(1, "This test should pass"); } -RUNNER_CHILD_TEST(t07_fail_unknown_exception) +RUNNER_CHILD_TEST_EXPECT(t07_fail_unknown_exception, TestResult::FAIL, "unhandled exeception") { throw("hello"); } -RUNNER_CHILD_TEST(t08_fail_unknown_exception) +RUNNER_CHILD_TEST_EXPECT(t08_fail_unknown_exception, TestResult::FAIL, "unhandled exeception") { throw(1); } -RUNNER_CHILD_TEST(t09_fail_you_should_see_text_normal_assert) +RUNNER_CHILD_TEST_EXPECT(t09_fail_you_should_see_text_normal_assert, TestResult::FAIL, "Normal assert") { RUNNER_ASSERT_MSG(0, "Normal assert"); } -RUNNER_CHILD_TEST(t10_pass) +RUNNER_CHILD_TEST_EXPECT(t10_pass, TestResult::PASS, "") { RUNNER_ASSERT_MSG(1, "Normal assert"); } +RUNNER_CHILD_TEST_EXPECT(t11_ignore, TestResult::IGNORED, "Test ignored") +{ + RUNNER_IGNORED_MSG("Test ignored"); +} + + -- 2.7.4