我正在尝试使用qtest构建测试,以行使我的库,该库实现了包装QWebSocket的类。
QTest似乎将在“专用插槽:”下运行每个方法,并认为它是测试初始化,清除或测试用例。
但是,我也试图在localhost上建立一个服务器以连接到该服务器,并要求在侦听器接受连接并关闭等之后回调插槽。
QTest希望将这些回调视为测试用例,然后运行它们,这是不正确的。我怎么告诉它“这些是用于测试用例的插槽”和“这些是在其他地方使用的插槽?”
考虑以下测试代码:
#include <QCoreApplication>
#include <QtTest>
#include <QtWebSockets/qwebsocket.h>
#include <QtWebSockets/qwebsocketserver.h>
#include <string>
class qtwebstompclientimpl_tests : public QObject
{
Q_OBJECT
public:
qtwebstompclientimpl_tests();
~qtwebstompclientimpl_tests();
private slots:
void initTestCase();
void cleanupTestCase();
void onAccept(); // Wants to run these as test cases, but they are actually slots for QWebSocketServer
void onAcceptError(QAbstractSocket::SocketError socketError);
void onTextReceived(QString message);
void onClientClosed();
void test_case1();
private:
QWebSocketServer * m_webSocketServer;
QWebSocket * m_connectedWebsocketClient;
bool m_errorOccurred;
QString m_lastMessage;
};
qtwebstompclientimpl_tests::qtwebstompclientimpl_tests() {}
qtwebstompclientimpl_tests::~qtwebstompclientimpl_tests() {}
void qtwebstompclientimpl_tests::initTestCase()
{
m_webSocketServer = new QWebSocketServer("Test Server", QWebSocketServer::NonSecureMode);
if( !m_webSocketServer->listen(QHostAddress::Any, 5000) )
{
std::string errorMessage = m_webSocketServer->errorString().toStdString();
throw std::runtime_error("Failed to setup test websocket server - Listen failed");
connect(m_webSocketServer, &QWebSocketServer::newConnection, this, &qtwebstompclientimpl_tests::onAccept);
connect(m_webSocketServer, &QWebSocketServer::acceptError, this, &qtwebstompclientimpl_tests::onAcceptError);
}
m_connectedWebsocketClient = nullptr;
m_errorOccurred = false;
}
void qtwebstompclientimpl_tests::cleanupTestCase()
{
if( m_connectedWebsocketClient )
{
delete m_connectedWebsocketClient;
}
delete m_testClient;
delete m_webSocketServer;
}
void qtwebstompclientimpl_tests::onAccept()
{
if(m_connectedWebsocketClient)
{
m_connectedWebsocketClient->close();
}
m_connectedWebsocketClient = m_webSocketServer->nextPendingConnection();
connect(m_connectedWebsocketClient, &QWebSocket::textMessageReceived, this, &qtwebstompclientimpl_tests::onTextReceived);
connect(m_connectedWebsocketClient, &QWebSocket::disconnected, this, &qtwebstompclientimpl_tests::onClientClosed);
}
void qtwebstompclientimpl_tests::onAcceptError(QAbstractSocket::SocketError socketError)
{
m_errorOccurred = true;
}
void qtwebstompclientimpl_tests::onTextReceived(QString message)
{
QWebSocket * client = qobject_cast<QWebSocket *>(sender());
if(client == m_connectedWebsocketClient)
{
m_lastMessage = message;
}
}
void qtwebstompclientimpl_tests::onClientClosed()
{
QWebSocket * client = qobject_cast<QWebSocket *>(sender());
if(client == m_connectedWebsocketClient)
{
m_connectedWebsocketClient->deleteLater();
}
}
void qtwebstompclientimpl_tests::test_case1()
{
QVERIFY(true);
}
QTEST_MAIN(qtwebstompclientimpl_tests)
答案 0 :(得分:0)
通过组合使用
qWait的副作用是使我的单元测试变得不必要地长,这是因为我们必须以足够高的间隔“猜测”以允许其他插槽进行处理。我们也很想睡觉,就像在睡觉时一样,必须确保它足够高,并在调试时将其启动,以便有足够的时间逐步执行代码。