Skip to content

Commit b966ed6

Browse files
committed
QtTest based test driver for cucumber-cpp.
Implementation based on temporary file. Unit tests included. When building QtTest driver C++11 is enabled globally to prevent issue #141.
1 parent 5f7ffe0 commit b966ed6

File tree

13 files changed

+177
-1
lines changed

13 files changed

+177
-1
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ if(NOT CUKE_DISABLE_QT)
124124
if(${Qt5Core_FOUND} AND ${Qt5Widgets_FOUND} AND ${Qt5Test_FOUND})
125125
message(STATUS "Found Qt version: ${Qt5Core_VERSION_STRING}")
126126
set(QT_LIBRARIES Qt5::Core Qt5::Widgets Qt5::Test)
127+
if(NOT Qt5Core_VERSION_STRING VERSION_LESS 5.7 AND (NOT DEFINED CMAKE_CXX_STANDARD OR NOT CMAKE_CXX_STANDARD STREQUAL 98))
128+
message(STATUS "C++11 is needed from Qt version 5.7.0, building with c++11 enabled")
129+
set(CMAKE_CXX_STANDARD 11)
130+
endif()
127131
else()
128132
find_package(Qt4 COMPONENTS QtCore QtGui QtTest)
129133
if(QT4_FOUND)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ It relies on a few libraries:
3333
Optional for the GTest driver. By default downloaded and built by CMake.
3434
* [GMock](http://code.google.com/p/googlemock/) 1.6 or later.
3535
Optional for the internal test suite. By default downloaded and built by CMake.
36-
* [Qt 4 or 5](http://qt-project.org/). Optional for the CalcQt example.
36+
* [Qt 4 or 5](http://qt-project.org/). Optional for the CalcQt example and QtTest driver (only Qt 5).
3737

3838
This header-only library is included in the source code:
3939

examples/Calc/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ if(Boost_UNIT_TEST_FRAMEWORK_FOUND)
2020
add_executable(BoostCalculatorSteps features/step_definitions/BoostCalculatorSteps)
2121
target_link_libraries(BoostCalculatorSteps Calc ${CUKE_LIBRARIES} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
2222
endif()
23+
24+
if(Qt5TEST_FOUND)
25+
add_executable(QtTestCalculatorSteps features/step_definitions/QtTestCalculatorSteps)
26+
target_link_libraries(QtTestCalculatorSteps Calc ${QT_LIBRARIES} ${CUKE_LIBRARIES})
27+
endif()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include <QTest>
2+
// Pretend to be GTest
3+
#define EXPECT_EQ QCOMPARE
4+
#include "CalculatorSteps.cpp"

examples/CalcQt/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,20 @@ if(QT_LIBRARIES)
99
add_executable(calcqt src/CalcQt.cpp)
1010
target_link_libraries(calcqt libcalcqt ${QT_LIBRARIES})
1111

12+
if(Qt5TEST_FOUND)
13+
add_executable(QtTestCalculatorQtSteps features/step_definitions/QtTestCalculatorQtSteps)
14+
set_target_properties(QtTestCalculatorQtSteps PROPERTIES AUTOMOC ON)
15+
target_link_libraries(QtTestCalculatorQtSteps PRIVATE libcalcqt ${QT_LIBRARIES} ${CUKE_LIBRARIES})
16+
endif()
17+
1218
if(Boost_UNIT_TEST_FRAMEWORK_FOUND)
1319
add_executable(BoostCalculatorQtSteps features/step_definitions/BoostCalculatorQtSteps)
1420
target_link_libraries(BoostCalculatorQtSteps libcalcqt ${CUKE_LIBRARIES} ${QT_LIBRARIES} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
1521
endif()
22+
1623
if(GTEST_FOUND)
1724
add_executable(GTestCalculatorQtSteps features/step_definitions/GTestCalculatorQtSteps)
1825
target_link_libraries(GTestCalculatorQtSteps libcalcqt ${CUKE_LIBRARIES} ${CUKE_GTEST_LIBRARIES} ${QT_LIBRARIES})
1926
endif()
27+
2028
endif()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include <QTest>
2+
// Pretend to be GTest
3+
#define EXPECT_EQ QCOMPARE
4+
#include "CalculatorQtSteps.cpp"

include/cucumber-cpp/internal/drivers/DriverSelector.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
#include "GTestDriver.hpp"
33
#elif defined(BOOST_TEST_CASE)
44
#include "BoostDriver.hpp"
5+
#elif defined(QTEST_H)
6+
#include "QtTestDriver.hpp"
57
#endif
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef CUKE_QTTESTDRIVER_HPP_
2+
#define CUKE_QTTESTDRIVER_HPP_
3+
4+
#include "../step/StepManager.hpp"
5+
#include <QObject>
6+
7+
namespace cucumber {
8+
namespace internal {
9+
10+
class QtTestStep : public BasicStep{
11+
friend class QtTestObject;
12+
public:
13+
QtTestStep(): BasicStep() {}
14+
15+
protected:
16+
const InvokeResult invokeStepBody();
17+
};
18+
19+
#define STEP_INHERITANCE(step_name) ::cucumber::internal::QtTestStep
20+
21+
class QtTestObject: public QObject {
22+
Q_OBJECT
23+
public:
24+
QtTestObject(QtTestStep* qtTestStep): step(qtTestStep) {}
25+
virtual ~QtTestObject() {}
26+
27+
protected:
28+
QtTestStep* step;
29+
30+
private slots:
31+
void test() const {
32+
step->body();
33+
}
34+
};
35+
36+
}
37+
}
38+
39+
#endif /* CUKE_QTTESTDRIVER_HPP_ */

src/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ if(Boost_UNIT_TEST_FRAMEWORK_FOUND)
2626
list(APPEND CUKE_SOURCES drivers/BoostDriver.cpp)
2727
endif()
2828

29+
if(Qt5TEST_FOUND)
30+
qt5_wrap_cpp(MOC_FILE ../include/cucumber-cpp/internal/drivers/QtTestDriver.hpp)
31+
list(APPEND CUKE_SOURCES ${MOC_FILE})
32+
list(APPEND CUKE_SOURCES drivers/QtTestDriver.cpp)
33+
list(APPEND CUKE_DEP_LIBRARIES ${QT_LIBRARIES})
34+
endif()
35+
2936
if(CMAKE_EXTRA_GENERATOR OR MSVC_IDE)
3037
message(STATUS "Adding header files to project")
3138
file(GLOB_RECURSE CUKE_HEADERS "${CUKE_INCLUDE_DIR}/cucumber-cpp/*.hpp")

src/drivers/QtTestDriver.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "cucumber-cpp/internal/drivers/QtTestDriver.hpp"
2+
3+
#include <QtTest/QtTest>
4+
#include <QTextStream>
5+
#include <QTemporaryFile>
6+
7+
namespace cucumber {
8+
namespace internal {
9+
10+
const InvokeResult QtTestStep::invokeStepBody() {
11+
QTemporaryFile file;
12+
QString fileName;
13+
if(!file.open()) {
14+
return InvokeResult::failure("Unable to open temporary file needed for this test");
15+
}
16+
file.close();
17+
18+
QtTestObject testObject(this);
19+
int returnValue = QTest::qExec(&testObject, QStringList() << "test" << "-o" << file.fileName());
20+
if(returnValue == 0)
21+
return InvokeResult::success();
22+
else
23+
{
24+
file.open();
25+
QTextStream ts(&file);
26+
return InvokeResult::failure(ts.readAll().toLocal8Bit());
27+
}
28+
}
29+
30+
}
31+
}
32+

tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ if(Boost_UNIT_TEST_FRAMEWORK_FOUND)
4848
cuke_add_driver_test(integration/drivers/BoostDriverTest ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
4949
endif()
5050

51+
if(Qt5TEST_FOUND)
52+
cuke_add_driver_test(integration/drivers/QtTestDriverTest ${QT_LIBRARIES})
53+
endif()
54+
5155
cuke_add_driver_test(integration/drivers/GenericDriverTest)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <QtTest>
2+
#include <cucumber-cpp/autodetect.hpp>
3+
4+
#include "../../utils/DriverTestRunner.hpp"
5+
6+
using namespace cucumber;
7+
8+
THEN(SUCCEED_MATCHER) {
9+
ScenarioScope<SomeContext> ctx;
10+
QVERIFY(true);
11+
}
12+
13+
THEN(FAIL_MATCHER) {
14+
ScenarioScope<SomeContext> ctx;
15+
QVERIFY(false);
16+
}
17+
18+
THEN(PENDING_MATCHER_1) {
19+
pending();
20+
}
21+
22+
THEN(PENDING_MATCHER_2) {
23+
pending(PENDING_DESCRIPTION);
24+
}
25+
26+
using namespace cucumber::internal;
27+
28+
class QtTestStepDouble : public QtTestStep {
29+
public:
30+
QtTestStepDouble() : QtTestStep() {
31+
testRun = false;
32+
}
33+
34+
const InvokeResult invokeStepBody() {
35+
return QtTestStep::invokeStepBody();
36+
}
37+
38+
void body() {
39+
testRun = true;
40+
}
41+
42+
bool testRun;
43+
};
44+
45+
class QtTestDriverTest : public DriverTest {
46+
public:
47+
virtual void runAllTests() {
48+
stepInvocationRunsStepBody();
49+
DriverTest::runAllTests();
50+
}
51+
52+
private:
53+
void stepInvocationRunsStepBody() {
54+
QtTestStepDouble framework;
55+
expectFalse("The test body has not been run", framework.testRun);
56+
framework.invokeStepBody();
57+
expectTrue("The test body has been run", framework.testRun);
58+
}
59+
};
60+
61+
int main() {
62+
QtTestDriverTest test;
63+
return test.run();
64+
}
65+

travis.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ fi
5353

5454
for TEST in \
5555
build/examples/Calc/GTestCalculatorSteps \
56+
build/examples/Calc/QtTestCalculatorSteps \
5657
build/examples/Calc/BoostCalculatorSteps \
5758
build/examples/Calc/FuncArgsCalculatorSteps \
5859
; do
@@ -66,6 +67,7 @@ done
6667

6768
for TEST in \
6869
build/examples/CalcQt/GTestCalculatorQtSteps \
70+
build/examples/CalcQt/QtTestCalculatorQtSteps \
6971
build/examples/CalcQt/BoostCalculatorQtSteps \
7072
; do
7173
if [ -f "${TEST}" -a -n "${DISPLAY:-}" ]; then

0 commit comments

Comments
 (0)