Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
49 changes: 49 additions & 0 deletions docs/GSteps.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<your output location>
TEST_NAME=<your 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
Expand Down
9 changes: 7 additions & 2 deletions include/GUnit/GSteps.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,16 @@ 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_CUCUMBER_JSON");

if (output) {
Features::getInstance()->addReport("gunit_result");
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, ':')) {
info_.file = feature;
Expand Down Expand Up @@ -641,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<GherkinCpp::Step>
Expand Down
1 change: 1 addition & 0 deletions include/formatters/cucumberjson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions include/formatters/gherkinCpp/step.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down