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

QTest frameworks needs more fine-grained init and cleanup methods

    XMLWordPrintable

    Details

    • Type: Suggestion
    • Status: Reported
    • Priority: P3: Somewhat important
    • Resolution: Unresolved
    • Affects Version/s: Some future release
    • Fix Version/s: None
    • Component/s: Testing: qtestlib
    • Labels:
      None

      Description

      When creating unit tests, you sometimes want to try a series of methods calls on an object in a specific state. For instance, if you write a parser for dates that takes a specific locale, you may want to try a series of text inputs on the same parser object.

      It would be nice if we were to get two more 'special' slots that we might implement, next to the MyTest_data() slot. I would suggest to make it possible to add a MyTest_init() slot and a MyTest_cleanup() slot. They would be called before the first invokation of the test (either direct or with data from MyTest_data()) respectively after the last invokation. This makes it possible to keep the tests themselves clean, and to not needlessly re-create objects to test thus making the tests faster to run.

      For now, I created a workaround by implementing the init() slot like this:

      static QByteArray lastTestFunction;

      if (!lastTestFunction.isEmpty() && strcmp(lastTestFunction.data(), QTest::currentTestFunction()) == 0)
      return;

      if (!lastTestFunction.isEmpty()) {
      //we're going to run a new test, so run the test cleanup function of the previous test if it exists...
      QByteArray cleanupMethod(lastTestFunction);
      cleanupMethod += "_cleanup";
      int index = metaObject()->indexOfMethod(cleanupMethod + "()");
      if (index != -1)

      { metaObject()->invokeMethod(this, cleanupMethod.data(), Qt::DirectConnection); }

      }

      lastTestFunction = QTest::currentTestFunction();

      //we're going to run a new test, so run the test init function if it exists...
      QByteArray initMethod(QTest::currentTestFunction());
      initMethod += "_init";
      int index = metaObject()->indexOfMethod(initMethod + "()");
      if (index != -1)
      metaObject()->invokeMethod(this, initMethod.data(), Qt::DirectConnection);

      This allows me to create (invokable) methods MyTest_init() and MyTest_cleanup(), as long as they are not private slots. Having this build-in would be much nicer.

        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:
            andre André Somers
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:

                Gerrit Reviews

                There are no open Gerrit changes