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



  • 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().



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

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



  • [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.



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



  • 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() ?



  • Hello Sam,

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



  • 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



  • [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++;
    }@



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.