Skip to content

QtTest driver v3 #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 4, 2017
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CO

### Added

* QtTest based test driver for cucumber-cpp ([#165](https://github.com/cucumber/cucumber-cpp/pull/165) Kamil Strzempowicz)
* Listen on localhost by default to avoid firewall warnings ([#158](https://github.com/cucumber/cucumber-cpp/pull/158) Nik Reiman)
* Better integrate Qt into buildsystem, it can now be disabled, and test it in CI ([#160](https://github.com/cucumber/cucumber-cpp/pull/160) Kamil Strzempowicz & Giel van Schijndel)
* Support taking regex captures as arguments to the step definition's function ([#159](https://github.com/cucumber/cucumber-cpp/pull/159) Giel van Schijndel)
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ if(NOT CUKE_DISABLE_QT)
if(${Qt5Core_FOUND} AND ${Qt5Widgets_FOUND} AND ${Qt5Test_FOUND})
message(STATUS "Found Qt version: ${Qt5Core_VERSION_STRING}")
set(QT_LIBRARIES Qt5::Core Qt5::Widgets Qt5::Test)
if(NOT Qt5Core_VERSION_STRING VERSION_LESS 5.7 AND (NOT DEFINED CMAKE_CXX_STANDARD OR NOT CMAKE_CXX_STANDARD STREQUAL 98))
message(STATUS "C++11 is needed from Qt version 5.7.0, building with c++11 enabled")
set(CMAKE_CXX_STANDARD 11)
endif()
else()
find_package(Qt4 COMPONENTS QtCore QtGui QtTest)
if(QT4_FOUND)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ It relies on a few libraries:
Optional for the GTest driver. By default downloaded and built by CMake.
* [GMock](http://code.google.com/p/googlemock/) 1.6 or later.
Optional for the internal test suite. By default downloaded and built by CMake.
* [Qt 4 or 5](http://qt-project.org/). Optional for the CalcQt example.
* [Qt 4 or 5](http://qt-project.org/). Optional for the CalcQt example and QtTest driver (only Qt 5).

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

Expand Down
5 changes: 5 additions & 0 deletions examples/Calc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ if(Boost_UNIT_TEST_FRAMEWORK_FOUND)
add_executable(BoostCalculatorSteps features/step_definitions/BoostCalculatorSteps)
target_link_libraries(BoostCalculatorSteps Calc ${CUKE_LIBRARIES} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
endif()

if(Qt5TEST_FOUND)
add_executable(QtTestCalculatorSteps features/step_definitions/QtTestCalculatorSteps)
target_link_libraries(QtTestCalculatorSteps Calc Qt5::Test ${CUKE_LIBRARIES})
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <QTest>
// Pretend to be GTest
#define EXPECT_EQ QCOMPARE
#include "CalculatorSteps.cpp"
7 changes: 7 additions & 0 deletions examples/CalcQt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ if(QT_LIBRARIES)
add_executable(calcqt src/CalcQt.cpp)
target_link_libraries(calcqt libcalcqt ${QT_LIBRARIES})

if(Qt5TEST_FOUND)
add_executable(QtTestCalculatorQtSteps features/step_definitions/QtTestCalculatorQtSteps)
target_link_libraries(QtTestCalculatorQtSteps PRIVATE libcalcqt ${QT_LIBRARIES} ${CUKE_LIBRARIES})
endif()

if(Boost_UNIT_TEST_FRAMEWORK_FOUND)
add_executable(BoostCalculatorQtSteps features/step_definitions/BoostCalculatorQtSteps)
target_link_libraries(BoostCalculatorQtSteps libcalcqt ${CUKE_LIBRARIES} ${QT_LIBRARIES} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
endif()

if(GTEST_FOUND)
add_executable(GTestCalculatorQtSteps features/step_definitions/GTestCalculatorQtSteps)
target_link_libraries(GTestCalculatorQtSteps libcalcqt ${CUKE_LIBRARIES} ${CUKE_GTEST_LIBRARIES} ${QT_LIBRARIES})
endif()

endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <QTest>
// Pretend to be GTest
#define EXPECT_EQ QCOMPARE
#include "CalculatorQtSteps.cpp"
2 changes: 2 additions & 0 deletions include/cucumber-cpp/internal/drivers/DriverSelector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
#include "GTestDriver.hpp"
#elif defined(BOOST_TEST_CASE)
#include "BoostDriver.hpp"
#elif defined(QTEST_H)
#include "QtTestDriver.hpp"
#endif
40 changes: 40 additions & 0 deletions include/cucumber-cpp/internal/drivers/QtTestDriver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef CUKE_QTTESTDRIVER_HPP_
#define CUKE_QTTESTDRIVER_HPP_

#include "../step/StepManager.hpp"
#include <QObject>

namespace cucumber {
namespace internal {

class QtTestStep : public BasicStep {
friend class QtTestObject;

public:
QtTestStep() : BasicStep() {}

protected:
const InvokeResult invokeStepBody();
};

#define STEP_INHERITANCE(step_name) ::cucumber::internal::QtTestStep

class QtTestObject : public QObject {
Q_OBJECT
public:
QtTestObject(QtTestStep* qtTestStep) : step(qtTestStep) {}
virtual ~QtTestObject() {}

protected:
QtTestStep* step;

private slots:
void test() const {
step->body();
}
};

}
}

#endif /* CUKE_QTTESTDRIVER_HPP_ */
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ if(Boost_UNIT_TEST_FRAMEWORK_FOUND)
list(APPEND CUKE_SOURCES drivers/BoostDriver.cpp)
endif()

if(Qt5TEST_FOUND)
qt5_wrap_cpp(MOC_FILE ../include/cucumber-cpp/internal/drivers/QtTestDriver.hpp)
list(APPEND CUKE_SOURCES ${MOC_FILE})
list(APPEND CUKE_SOURCES drivers/QtTestDriver.cpp)
list(APPEND CUKE_DEP_LIBRARIES Qt5::Test)
endif()

if(CMAKE_EXTRA_GENERATOR OR MSVC_IDE)
message(STATUS "Adding header files to project")
file(GLOB_RECURSE CUKE_HEADERS "${CUKE_INCLUDE_DIR}/cucumber-cpp/*.hpp")
Expand Down
33 changes: 33 additions & 0 deletions src/drivers/QtTestDriver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "cucumber-cpp/internal/drivers/QtTestDriver.hpp"

#include <QtTest/QtTest>
#include <QTextStream>
#include <QTemporaryFile>

namespace cucumber {
namespace internal {

const InvokeResult QtTestStep::invokeStepBody() {
QTemporaryFile file;
if (!file.open()) {
return InvokeResult::failure("Unable to open temporary file needed for this test");
}
file.close();

QtTestObject testObject(this);
int returnValue = QTest::qExec(&testObject,
QStringList() << "test"
<< "-o"
<< file.fileName());
if (returnValue == 0) {
return InvokeResult::success();
} else {
file.open();
QTextStream ts(&file);
return InvokeResult::failure(ts.readAll().toLocal8Bit());
}
}

}
}

4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ if(Boost_UNIT_TEST_FRAMEWORK_FOUND)
cuke_add_driver_test(integration/drivers/BoostDriverTest ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
endif()

if(Qt5TEST_FOUND)
cuke_add_driver_test(integration/drivers/QtTestDriverTest ${QT_LIBRARIES})
endif()

cuke_add_driver_test(integration/drivers/GenericDriverTest)
62 changes: 62 additions & 0 deletions tests/integration/drivers/QtTestDriverTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <QtTest>
#include <cucumber-cpp/autodetect.hpp>

#include "../../utils/DriverTestRunner.hpp"

using namespace cucumber;

THEN(SUCCEED_MATCHER) {
ScenarioScope<SomeContext> ctx;
QVERIFY(true);
}

THEN(FAIL_MATCHER) {
ScenarioScope<SomeContext> ctx;
QVERIFY(false);
}

THEN(PENDING_MATCHER_1) {
pending();
}

THEN(PENDING_MATCHER_2) {
pending(PENDING_DESCRIPTION);
}

using namespace cucumber::internal;

class QtTestStepDouble : public QtTestStep {
public:
QtTestStepDouble() : QtTestStep(), testRun(false) {}

const InvokeResult invokeStepBody() {
return QtTestStep::invokeStepBody();
}

void body() {
testRun = true;
}

bool testRun;
};

class QtTestDriverTest : public DriverTest {
public:
virtual void runAllTests() {
stepInvocationRunsStepBody();
DriverTest::runAllTests();
}

private:
void stepInvocationRunsStepBody() {
QtTestStepDouble framework;
expectFalse("The test body has not been run", framework.testRun);
framework.invokeStepBody();
expectTrue("The test body has been run", framework.testRun);
}
};

int main() {
QtTestDriverTest test;
return test.run();
}
2 changes: 2 additions & 0 deletions travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ startXvfb # Start virtual X display server

for TEST in \
build/examples/Calc/GTestCalculatorSteps \
build/examples/Calc/QtTestCalculatorSteps \
build/examples/Calc/BoostCalculatorSteps \
build/examples/Calc/FuncArgsCalculatorSteps \
; do
Expand All @@ -72,6 +73,7 @@ done

for TEST in \
build/examples/CalcQt/GTestCalculatorQtSteps \
build/examples/CalcQt/QtTestCalculatorQtSteps \
build/examples/CalcQt/BoostCalculatorQtSteps \
; do
if [ -f "${TEST}" -a -n "${DISPLAY:-}" ]; then
Expand Down