Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6062559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:03:58+00:00 2026-05-23T09:03:58+00:00

I have successfully built a thin client/server using Qt’s Tcp Sockets API. I know

  • 0

I have successfully built a thin client/server using Qt’s Tcp Sockets API. I know it works very well, because I have sent plenty of data over the wire and verified it. However, my project manager wants a suite of unit-tests, and I’m implementing them using Qt’s Test Library.

Anyhow, I’m trying to set up some dummy server to simply receive data from a QTcpSocket to verify a sendData() method in a unit test. When I connect the test socket, is shows that it is connected, but the slot for connecting the dummy server and its dummy socket is never called!

Can anyone see what I’m doing wrong here?
( I’ve stripped the code down down to just the parts that seem broken in the test class )

From tst_tcpcommsocket.h

#ifndef TST_TCPCOMMSOCKET_H
#define TST_TCPCOMMSOCKET_H

#include <QtCore/QString>
#include <QtTest/QtTest>

#include <iostream>

#include <QTcpSocket>
#include <QTcpServer>

class TcpCommSocketTest : public QObject
{
    Q_OBJECT

private:
    QTcpSocket* qTestSocket;
    QTcpSocket* qDummySocket;
    QTcpServer* qDummyServer;

public:
        TcpCommSocketTest();

public slots:
    void connectDummyServer();

private Q_SLOTS:
    void initTestCase();    
    void sendDataTest();    
    void cleanupTestCase();
};

#endif // TST_TCPCOMMSOCKET_H

From tst_tcpcommsocket.cpp

#include "tst_tcpcommsocket.h"

void TcpCommSocketTest::connectDummyServer()
{
    cout << "connection attempt" << endl;
    qDummySocket = qDummyServer->nextPendingConnection();
}

void TcpCommSocketTest::initTestCase()
{
    qDummySocket = NULL;
    qDummyServer = new QTcpServer();
    qDummyServer->listen( QHostAddress("127.0.0.1"), 9000 );
    connect( qDummyServer, SIGNAL(newConnection()), SLOT(connectDummyServer()) );

    qTestSocket = new QTcpSocket();
    qTestSocket->connectToHost( QHostAddress("127.0.0.1"), 9000 );

    QVERIFY( qTestSocket->waitForConnected( 5000 ) );
    QVERIFY( qTestSocket->state() == QTcpSocket::ConnectedState );
}

void TcpCommSocketTest::sendDataTest()
{
    int i=0;
    QVERIFY( qDummySocket != NULL );
}

QTEST_MAIN(TcpCommSocketTest);

The Test Run’s output:

********* Start testing of TcpCommSocketTest *********
Config: Using QTest library 4.7.3, Qt 4.7.3
PASS   : TcpCommSocketTest::initTestCase()
FAIL!  : TcpCommSocketTest::sendDataTest() 'qDummySocket != NULL' returned FALSE. ()
   Loc: [-]
cleanup
PASS   : TcpCommSocketTest::cleanupTestCase()
Totals: 2 passed, 1 failed, 0 skipped
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:03:59+00:00Added an answer on May 23, 2026 at 9:03 am

    While QTEST_MAIN creates a QApplication and runs all your tests, it does so sequentially and not with an event loop. Therefore, while your socket can indeed connect to the server (meaning qTestSocket->waitForConnected() returns true), since the app doesn’t return to an event loop, the QTcpServer‘s signal will not get emitted, and TcpCommSocketTest::connectDummyServer() never gets called.

    Try adding a call to qApp->processEvents() at the end of initTestCase(). It should let connectDummyServer() be called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using Hibernate very successfully, but today I noticed a bizarre phenomenon
I have successfully created a PHP REST API which resides on my server. I
I have successfully built ViennaCL using cmake . and make and run some of
I have built a very simple CakePHP website using the Auth component and have
using the Code Snippet for sending email in VB.Net I have successfully sent an
I have a Delphi exe that is built with packages. The exe processes successfully
I have a C# class that I have inherited. I have successfully built the
I have a small site I built using the Play framework that I'm trying
I have successfully connected to an Oracle database (10g) from C# (Visual Studio 2008)
I have a Rails app that I have successfully tested with Mongrel and Webkit.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.