Qt unit test

Source: Internet
Author: User
Tags qtest

 

Self-turning blog: http://blog.csdn.net/zhu_xz/archive/2009/12/25/5076157.aspx

The qtestlib framework provides an easy-to-use unit test framework. You need to add QT ++ = testlib to the project file. First read oneSimple Example:

C/C ++ code

# Include <qttest/qttest>

Class testqstring: Public qobject

{

Q_object

Private slots:

// Each private slot function is a test function automatically called.

Void testtolower ();

// The function ending with "_ DATA" provides test data to the corresponding test function.

Void testtolower_data ();

};

 

Void testqstring: testtolower ()

{

// Obtain test data

Qfetch (qstring, string );

Qfetch (qstring, result );

// If the two parameters are different, the test fails.

Qcompare (string. tolower (), result );

}

Void testqstring: testtolower_data ()

{

// Add a data column

Qtest: AddColumn <qstring> ("string ");

Qtest: AddColumn <qstring> ("result ");

// Add Test Data

Qtest: newrow ("lower") <"hello" <"hello ";

Qtest: newrow ("mixed") <"hello" <"hello ";

Qtest: newrow ("upper") <"hello" <"hello ";

}

// Used to build executable test programs

Qtest_main (testqstring)

# Include "testqstring. MOC"

 

In addition, QT provides four

Private slot:
Inittestcase (): called before the test starts.
Cleanuptestcase (): called after the test is complete
Init (): Each test function is called before execution.
Cleanup (): Each test function is called after execution.

The output result is as follows:

JScript code

* ******* Start testing of testqstring *********

Config: Using qtest library 4.5.3, QT 4.5.3

Pass: testqstring: inittestcase ()

Pass: testqstring: testtolower ()

Pass: testqstring: cleanuptestcase ()

Totals: 3 passed, 0 failed, 0 skipped

* ******* Finished testing of testqstring *********

 

ToTest GUI ComponentsYou can use functions such as qtest: keyclick () to simulate events in the local Window System by passing internal events. For example:

C/C ++ code

// The code is taken fromHttp://qt.nokia.com/doc/qtestlib-tutorial4-testgui-cpp.html

# Include <qtgui>

# Include <qttest/qttest>

Class testgui: Public qobject

{

Q_object

Private slots:

Void testgui_data ();

Void testgui ();

};

 

Void testgui: testgui_data ()

{

Qtest: AddColumn <qtesteventlist> ("events ");

Qtest: AddColumn <qstring> ("expected ");

// Add button events

Qtesteventlist list1;

List1.addkeyclick ('A ');

Qtest: newrow ("char") <list1 <"";

 

Qtesteventlist list2;

List2.addkeyclick ('A ');

List2.addkeyclick (QT: key_backspace );

Qtest: newrow ("There and Back Again") <list2 <"";

}

Void testgui: testgui ()

{

Qfetch (qtesteventlist, events );

Qfetch (qstring, expected );

Qlineedit lineedit;

// Simulate button events and compare the results

Events. Simulate (& lineedit );

Qcompare (lineedit. Text (), expected );

}

Qtest_main (testgui)

# Include "testgui. MOC"

 

Finally, let's see howTest Benchmark:

C/C ++ code

Void testqstring: testbenchmark ()

{

Qstring STR ("hello ");

// The following code is used to test Benchmark

Qbenchmark

{

Str. tolower ();

}

}

 

The output is as follows:

JScript code

Result: testqstring: testbenchmark ():

0.00062 msec per iteration (total: 41, iterations: 65536)

 

 

 

Qtestlib, a unit test framework of QT, is used to test custom classes.

Category:
Qt 673 reading
Comments (0) favorites
Report

In this local http://doc.qt.nokia.com/4.7/qtestlib-tutorial.html in Nokia's QT 4.7 official document, I 've introduced how to use the qtestlib framework for unit testing. In this example, we test the classes provided by the system. Here I will mainly write down how to perform unit tests on the classes I have defined.

 

Test method:

MACRO: qcompare, qtest, qverify, qbenchmark,

Event:
Keyaction (qtest: keyclick (), qtest: keypress (), qtest: keyrelease ()),
Mouseaction (qtest: mouseclick (), qtest: mousedclick (), qtest: mousemove (), qtest: mousepress (), qtest: mouserelease ())

Class: qsignalspy, qtoucheventsequence

 

Save test results:

First, we define a macro to define the file that stores our test results. This macro runs on the Symbian s60 platform.

# IfndefS60unittest_h _

# DefineS60unittest_h _

# DefineQtest_main_s60 (testobject )/

IntMain (IntArgc,Char* Argv [])/

•{/

Char* New_argv [3];/

• Qapplication app (argc, argv );/

•/

• Qstring STR = "C: // data //" + qfileinfo (qcoreapplication: applicationfilepath (). basename () + ". log ";/

• Qbytearray bytes = Str. toascii ();/

•/

CharArg1 [] = "-o ";/

•/

• New_argv [0] = argv [0];/

• New_argv [1] = arg1 ;/

• New_argv [2] = bytes. Data ();/

•/

• Testobject TC ;/

ReturnQtest: qexec (& TC, 3, new_argv );/

•}

# Endif/* S60unittest_h _*/

 

In the defined test class, use qtest_main_s60 (testobject) instead of qtest_main (testobject). Here testobject is the class name of our custom class.

The test results are stored in the C: // data // XXXX. log file. XXXX is the name of the test project.

 

Access protection of the tested class, Private member variables and member functions:

You can use one of the following two methods:

1. Declare the test class as a friend class of the tested letter class.

2. Add the following macro to the test Class header file:
# Define private public
# Define protected public
# Include "mywidget. H" // here mywidget. H is the name of my tested class
# UNDEF protected
# UNDEF private

 

 

Use qsignalspy

Void testobject: testeventtomouse ()
{
Qpushbutton button;
Qsignalspy buttonspy (& button, signal (clicked ()));
Qtest: mouseclick (& button, QT: leftbutton );
Qverify (buttonspy. Count () = 1 );
}

 

 

Then, put the header files and implementation files (. cpp or DLL) of the test class and the tested class in the same project directory, and add the files related to the tested class to the Pro file of the project.

Compile and run the test program to find the log file containing the test result in the C: // data // directory.

In addition, you can use the qdebug ("this is test info") Statement in the program to output relevant test information.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.