From 2c8adb2cceb5cc90814ddb4b5a0a65b1e3c9b9b4 Mon Sep 17 00:00:00 2001 From: "kayne.saridjo" Date: Tue, 29 Aug 2023 12:43:15 +0000 Subject: [PATCH 1/3] Added Test Name environemnt variable Added path input to cucubberjson feature report --- CMakeLists.txt | 4 ++-- include/GUnit/GSteps.h | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6561c8b..5a9369d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,7 +79,7 @@ if (ENABLE_MEMCHECK AND MEMORYCHECK_COMMAND) add_test(${out} ${MEMORYCHECK_COMMAND} --leak-check=full --error-exitcode=1 ./${out}) set_tests_properties(${out} PROPERTIES - ENVIRONMENT ${scenario} + ENVIRONMENT "${scenario};TEST_NAME=${out}" ) target_link_libraries(${out} gunit) endfunction() @@ -90,7 +90,7 @@ else() add_test(${out} ./${out}) set_tests_properties(${out} PROPERTIES - ENVIRONMENT ${scenario} + ENVIRONMENT "${scenario};TEST_NAME=${out}" ) target_link_libraries(${out} gunit) endfunction() diff --git a/include/GUnit/GSteps.h b/include/GUnit/GSteps.h index d70a98c..4fb1089 100644 --- a/include/GUnit/GSteps.h +++ b/include/GUnit/GSteps.h @@ -301,12 +301,15 @@ class Steps : public ::testing::EmptyTestEventListener { public: Steps() { const auto scenario = std::getenv("SCENARIO"); - const auto output = std::getenv("OUTPUT"); + if (scenario) { ::testing::UnitTest::GetInstance()->listeners().Append(this); // If the output is set, then add a report to the features holder + const auto output = std::getenv("OUTPUT"); + std::string path = std::string(output) + std::string(std::getenv("TEST_NAME")); + std::cout << path << std::endl; if (output) { - Features::getInstance()->addReport("gunit_result"); + Features::getInstance()->addReport(path); } for (const auto& feature : detail::split(scenario, ':')) { info_.file = feature; From 0a3dc933d76a9b65ddfe97a3d4826bcee8f37474 Mon Sep 17 00:00:00 2001 From: "kayne.saridjo" Date: Tue, 12 Sep 2023 21:10:09 +0000 Subject: [PATCH 2/3] Added Location selection for Cucumber.json output Added Fail message to Cucumber.json output --- include/GUnit/GSteps.h | 6 ++++-- include/formatters/cucumberjson.hpp | 1 + include/formatters/gherkinCpp/step.hpp | 6 ++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/GUnit/GSteps.h b/include/GUnit/GSteps.h index a07acd3..6356001 100644 --- a/include/GUnit/GSteps.h +++ b/include/GUnit/GSteps.h @@ -306,9 +306,10 @@ class Steps : public ::testing::EmptyTestEventListener { ::testing::UnitTest::GetInstance()->listeners().Append(this); // If the output is set, then add a report to the features holder const auto output = std::getenv("OUTPUT"); - std::string path = std::string(output) + std::string(std::getenv("TEST_NAME")); - std::cout << path << std::endl; + if (output) { + std::string path = std::string(output) + std::string(std::getenv("TEST_NAME")); + std::cout << path << std::endl; Features::getInstance()->addReport(path); } for (const auto& feature : detail::split(scenario, ':')) { @@ -644,6 +645,7 @@ class Steps : public ::testing::EmptyTestEventListener { ; if (currentStep == nullptr) return; currentStep->setResult(!(test_part_result.failed())); + currentStep->setFailMessage(test_part_result.summary()); } std::shared_ptr diff --git a/include/formatters/cucumberjson.hpp b/include/formatters/cucumberjson.hpp index 4ca082d..b742089 100644 --- a/include/formatters/cucumberjson.hpp +++ b/include/formatters/cucumberjson.hpp @@ -65,6 +65,7 @@ class CucumberJson { std::string resultStr = step.second->getResult()?"passed":"failed"; curStep["result"] = { {"status", resultStr}, + {"error_message", step.second->getFailMessage()}, {"duration", 1} }; curElement["steps"].push_back(curStep); diff --git a/include/formatters/gherkinCpp/step.hpp b/include/formatters/gherkinCpp/step.hpp index 0939ee8..502a685 100644 --- a/include/formatters/gherkinCpp/step.hpp +++ b/include/formatters/gherkinCpp/step.hpp @@ -24,10 +24,16 @@ class Step : public GenericInfo{ result = result_; } + void setFailMessage(const char* message_) { + message = std::string(message_); + } + bool getResult() { return result;} + std::string getFailMessage() {return message;} private: bool result=true; + std::string message; }; } // GherkinCpp From 8281a6b82dcc2dab1fab6d44e43acf323458464c Mon Sep 17 00:00:00 2001 From: "kayne.saridjo" Date: Wed, 13 Sep 2023 08:03:01 +0000 Subject: [PATCH 3/3] Added GSTEPS documentation Changed Cucumber.json environment variable from OUTPUT to OUTPUT_CUCUMBER_JSON --- docs/GSteps.md | 49 ++++++++++++++++++++++++++++++++++++++++++ include/GUnit/GSteps.h | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/docs/GSteps.md b/docs/GSteps.md index 8f94551..c69715e 100644 --- a/docs/GSteps.md +++ b/docs/GSteps.md @@ -176,6 +176,55 @@ GSTEPS("Calc*") { SCENARIO="test/Features/Calc/addition.feature" ./test --gtest_filter="Calc Addition.Add two numbers" ``` +##### Cucumber.json output + +GSteps also supports the output of Cucumber.json files +enable this by setting the following environment variables: + +```sh +OUTPUT_CUCUMBER_JSON= +TEST_NAME= +``` +the OUTPUT_CUCUMBER_JSON variable can be set in the CMakePreset.json file. +and the test name will be automatically set by the test runner if you define the tests using the `test()` function in the CMakeLists.txt file and use the ctest command to run your tests. + +##### Example + +###### Define tests +The CMake preset file test preset: +```json +"testPresets": [ + { + "name": "gcc", + "configurePreset": "gcc", + "output": { + "outputOnFailure": true + }, + "execution": { + "noTestsAction": "error", + "stopOnFailure": false + }, + "environment": + { + "OUTPUT_CUCUMBER_JSON": "${sourceDir}/TestResults/" + } + } +] +``` + +The CMakeLists.txt file defines a test executable with multiple scenarios: +``` +test( test/Features/Calc/Steps/CalcSteps SCENARIO=${CMAKE_CURRENT_SOURCE_DIR}/test/Features/Calc/addition.feature:${CMAKE_CURRENT_SOURCE_DIR}/test/Features/Calc/additionfile2.feature) +``` + +The test runner will automatically set the TEST_NAME environment variable to the name of the test executable. + +###### Build and run tests +```sh +cmake --build -j --preset=gcc +ctest --preset=gcc +``` + ### GWT and Mocking? ```cpp diff --git a/include/GUnit/GSteps.h b/include/GUnit/GSteps.h index 6356001..37e8f00 100644 --- a/include/GUnit/GSteps.h +++ b/include/GUnit/GSteps.h @@ -305,7 +305,7 @@ class Steps : public ::testing::EmptyTestEventListener { if (scenario) { ::testing::UnitTest::GetInstance()->listeners().Append(this); // If the output is set, then add a report to the features holder - const auto output = std::getenv("OUTPUT"); + const auto output = std::getenv("OUTPUT_CUCUMBER_JSON"); if (output) { std::string path = std::string(output) + std::string(std::getenv("TEST_NAME"));