Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] How to get the list of test cases in a test class- Unit Testing.

[Solved] How to get the list of test cases in a test class- Unit Testing.

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sam
    wrote on last edited by
    #1

    Inorder to perform unit testing for an application we declare many test cases/functions under the private slots. Is there any way to get the list of test cases/functions for a unit test class based on the class name ?

    eg :-

    @class TestDateEdit : public QObject
    {
    Q_OBJECT
    private slots:
    void testChanges();
    void testValidator_data();
    void testValidator();

    };@

    So from the above class how to get the test cases/functions that will run like testChanges() and testValidator().

    1 Reply Last reply
    0
    • G Offline
      G Offline
      gayu
      wrote on last edited by
      #2

      By using like this u can get the test cases/functions, i think

      @
      void TestDateEdit::testChanges()
      @
      Whether it will work.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam
        wrote on last edited by
        #3

        [quote author="gayu" date="1353399523"]By using like this u can get the test cases/functions, i think

        @
        void TestDateEdit::testChanges()
        @

        Whether it will work.[/quote]

        No you didnt get my question , the above answer that u suggested is for the implementation/definition of the testFunction , I need to get the list/stringlist of the valid test cases/function for a test class based on the className.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DeVeL-2010
          wrote on last edited by
          #4

          Because TestDateEdit is subclass of QObject you can use metaObject(). There you can use methodCount() and method() for more informations.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sam
            wrote on last edited by
            #5

            Something like

            @const QMetaObject* metaObject = obj->metaObject();
            QStringList methods;
            for(int i = metaObject->methodOffset(); i < metaObject->methodCount(); ++i)
            methods << QString::fromLatin1(metaObject->method(i).signature());@

            from the documentation of QMetaObject::methodCount() ?

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DeVeL-2010
              wrote on last edited by
              #6

              Hello Sam,

              this should work.
              You can restrict this to the private slots with
              @access() == Private && methodType() == Slot@

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sam
                wrote on last edited by
                #7

                Thanks for the reply,

                How can I create an instance of a class from a QString ?

                I looked at the documentation of QMetaType and got

                @int id = QMetaType::type(testClass);
                if (id == 0)
                {
                void *myClassPtr = QMetaType::construct(id);

                }@

                But how to create an instance , for eg i only have a QString "TestDate" ?

                Edit : This issue is solved

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sam
                  wrote on last edited by
                  #8

                  [quote author="DeVeL-2010" date="1353406809"]Hello Sam,

                  this should work.
                  You can restrict this to the private slots with
                  @access() == Private && methodType() == Slot@
                  [/quote]

                  Thanks a lot it works perfect :)

                  This is solved.

                  @
                  int i=0;
                  const QMetaObject *metaObject = object->metaObject();
                  QStringList methods;

                  for (int i= metaObject->methodOffset(); i < metaObject->methodCount(); i++)
                  {
                  if((metaObject->method(i).access() == QMetaMethod::Private) && (metaObject->method(i).methodType() == QMetaMethod::Slot))
                  methods << QString::fromLatin1(metaObject->method(i).signature());
                  }

                  /* To restrict the valid functions for display */
                  foreach (QString str, methods) {

                  if(str.endsWith("_data()"))
                  methods.removeAt(i);
                  i++;
                  }@

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Sam
                    wrote on last edited by
                    #9

                    Hi,
                    I have another question. Is there any way to skip any particular test case/function while running the test using QTest::qExec(). The scenario is that i have listed the test functions in a treeView with checkbox as a QStandardItem, So foreach testClass the associated testFunctions are listed.

                    Now the requirement is to test only those cases/functions that are checked and the other's should be skipped.

                    I have tried

                    @testCmd<<" " << QString("%1").arg(childItem->text())<<"-xml" <<"-o" <<QString("UnitTest_Results/%1_log.xml").arg(className);

                    QTest::qExec(plugin->testList().at(pRow),testCmd);@

                    this generates an xml output but the xml.file is overwritten for each test function and at the end displays the result of the last checked item.

                    Thanks.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved