Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-70194

Adding QBENCHMARK to a test causes the code outside the QBENCHMARK brackets to be called several times

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Not Evaluated
    • Resolution: Duplicate
    • Affects Version/s: 5.10.1
    • Fix Version/s: None
    • Component/s: Testing: qtestlib
    • Labels:
      None

      Description

      Sample code to reproduce the issue :

      TestExample.h

      #ifndef TESTEXAMPLE_H
      #define TESTEXAMPLE_H
      
      #include <QDebug>
      #include <QObject>
      #include <QtTest/QtTest>
      
      class TestExample : public QObject {
          Q_OBJECT
         private slots:
          void initTestCase();
          void cleanupTestCase();
      
          void testWithoutBenchmark();
          void testWithBenchmark();
      
         private:
          long long _value;
          long long _benchmarkValue;
      };
      
      #endif  // TESTEXAMPLE_H

      TestExample.cpp

       

      #include "TestExample.h"
      
      void TestExample::initTestCase() {
          _value=0;
          _benchmarkValue=0;
      }
      
      void TestExample::cleanupTestCase() {
          QVERIFY(_benchmarkValue > 0);
          QVERIFY(_value == 2);
      }
      
      void TestExample::testWithoutBenchmark() {
          _value++;
          qDebug() << QString::number(_value);
          QVERIFY(_value == 1);
      }
      
      void TestExample::testWithBenchmark() {
          _value++;
          qDebug() << QString::number(_value);
          QVERIFY(_value == 2);
      
          QBENCHMARK {
              _benchmarkValue++;
          }
      }
      
      QTEST_APPLESS_MAIN(TestExample)
      

       

      Expected behavior : both tests passes, and each test is called only once, the code inside the QBENCHMARK brackets is called multiple times.

      Observed behavior : testWithoutBenchmark passes, testWithBenchmark fails because it's called multiple times. Remove the QVERIFY of testWithBenchmark to see how many times the test slot is called though the qDebug output.

       

      Same issue occur with QBENCHMARK_ONCE, resulting in even more unexpected result as the code inside QBNECHMARK_ONCE is executed several times when it should run only one time.

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            macadder Jason McDonald
            Reporter:
            fpe fpe
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes