I want to test whether a Qt Class correctly emits a signal upon function call using QSignalSpy. I use MS Visual Studio and use the Microsoft::VisualStudio::CppUnitTestFramework. Executing unit tests generally works fine, but instantiating a QSignalSpy yields a quite generic Failed to set up the execution context to run the test error which prevents all unit tests from executing. Compilation works fine, though.
This is the (simplified) class I want to test:
// simcam.h
#include <QtCore>
class SimCam {
Q_OBJECT
public:
SimCam() {};
~SimCam();
public slots:
void connect() {
emit(s_connected());
};
signals:
void s_connected();
};
and the unit test:
#include "CppUnitTest.h"
#include "simcam.h"
#include <QSignalSpy>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace devices_cameras_simcam_UnitTests {
TEST_CLASS(ConnectionTests) {
public:
TEST_METHOD(connectTest) {
auto simcam = SimCam();
// Problematic line
QSignalSpy spy(&simcam, &SimCam::s_connected);
simcam.connect();
spy.wait();
Assert::AreEqual(1, (int)spy.count());
}
};
}
I am a bit lost, how to get it to work or debug it. I didn't find any hint how to use QSignalSpy in combination with Visual Studio.
Basically with invocation of
spy.wait();(also other functionalities ofQSignalSpyas you found out by testing) you are requesting run of event loop this meansQApplicationis needed.In other test frameworks this means to provide own
mainfunction and instantiate thereQApplicationorQCoreApplication.Problem is that
CppUnitTestFrameworkuses dynamic libraries as tests suites which are loaded by some fixed tool, so you do now have access tomainfunction.The only choice is use initialization of module (whole test dll). I would try this (didn't test that, but should resolve issue):
Documentation of Microsoft.VisualStudio.TestTools.CppUnitTestFramework API