Home

OFF-SOFT.net

OFF-SOFT.net

This site is support & information site of WEB,and Software. This site might help you that create software or Web Site…perhaps?[:]

Qt (10) How to test with QTestLib.

Published on| June 27th, 2009 | No Comment.
Description:
In Qt, CppUnit like QTestLib.

In this article, QTestLib Let's create a simple test program.
(This article is part of a tutorial site articles. Qt is the target for the novice programmers. )

Articles: http://doc.trolltech.com/4.5.1/qtestlib-manual.html

Sample source code you used here:

Make the class a simple test target
For this, I have a class of its own target for a simple test.

[TestPlus.h]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class CTestPlus
{
public :
	CTestPlus();
	virtual ~CTestPlus();
 
	//	calc method
	int plus(int val);
	int minus(int val);
 
	//	value method
	int value();
	void value(int val);
 
private :
	int m_value[3];
};

[TestPlus.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "TestPlus.h"
 
CTestPlus::CTestPlus()
{
	for(int ni=0;ni<3;ni++)
		m_value[ni]=0;
}
 
CTestPlus::~CTestPlus()
{
}
 
int CTestPlus::plus(int val)
{
	m_value[0]=m_value[2];
	m_value[1]=val;
	m_value[2]=m_value[0]+m_value[1];
	return value();
}
 
int CTestPlus::minus(int val)
{
	m_value[0]=m_value[2];
	m_value[1]=val;
	m_value[2]=m_value[0]-m_value[1];
	return value();
}
 
int CTestPlus::value()
{
	return m_value[2];
}
void CTestPlus::value(int val)
{
	m_value[2]=val;
}

This class is very simple.
Want to test the method, the following two methods.
  • int plus (int val);
    - Specify the value of adding value to the current val.
  • int minus (int val);
    - Specify the value of the current reduces the value val.

Values on the stack so you can see how to calculate what has in the array.


So, I have the following program.

[sample.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "TestPlus.h"
#include <QtTest/QtTest>
 
 
class TestCTestPlus: public QObject
{
	Q_OBJECT
private slots:
	void testMinus_data();
 
	void testPlus();
	void testMinus();
};
 
void TestCTestPlus::testPlus()
{
	CTestPlus sobj;
	QBENCHMARK {
		sobj.value(3);
		sobj.plus(4);
	}
	QCOMPARE(sobj.value(), int(7));
}
 
void TestCTestPlus::testMinus_data()
{
	QTest::addColumn<int>("value");
	QTest::addColumn<int>("minus");
	QTest::addColumn<int>("result");
 
	QTest::newRow("3-4")   << 3   << 4   << -1;
	QTest::newRow("-1+5")  << -1  << -5  << 4;
	QTest::newRow("-1+4")  << -1  << -4  << 5;	//	error
	QTest::newRow("4-3")   << 4   << 3   << 1;
}
 
void TestCTestPlus::testMinus()
{
 
	QFETCH(int, value);
	QFETCH(int, minus);
	QFETCH(int, result);
 
	CTestPlus sobj;
	sobj.value(value);
 
	QCOMPARE(sobj.minus(minus), result);
}
 
QTEST_MAIN(TestCTestPlus)
#include "sample.moc"

Make a simple explanation.

The first, as each of the following cases are finally determined.
Line 5, the test class, be sure, QObject inherit.
Line 50, class test, be sure, QTEST_MAIN use a macro to create the main program.
Line 51, the test class is always the last to include the moc own.

QTEST_MAIN about, you can create your own. However, it was easier to follow the ways of the above are described as such.

Test methods, private slots defined.
The name is any good, you have a good descriptive name.
15 from the line, plus we are testing the method.
Line 18 is a set of benchmark test cases. QBENCHMARK () you can test the capacity specified in the macro range. Test the default behavior is the processing time. In addition to processing time, by specifying the parameters of the benchmark, CPU CLOCK can also print out information.

Line 22 is compared.
QCOMPARE in the macro, the same test if the first argument and second argument will be OK.
The first argument and second argument, the comparison operator (==) are available, you can set.
For example, CTestPlus to
bool operator == (const CTestPlus & _Right) const;
If you overload the method, such as, QCOMPARE macro, CTestPlus You can also check the two objects.


Line 25, test method (testMinus) method of the data set.
Test method name _data () is a reserved name for the data set as a method for testing the method.
Data set, you must do the following.
  • QTest:: addColumn <T> Use the template to define the type and name of the parameter used in the test method.
  • QTest:: newRow (name) is used to define patterns of data and parameters for testing.

37 from the line, remove the first set DESUTOPARAMETA, the test method.
QFETCH macro assignment, the type parameter data is set, remove it by name.
44 from the line, and we are running a test using the extracted parameters.

Each test pattern has been set before, so this method is called, and need to be aware of where the number of test patterns.


As long as you do not do something special in a very unique, very easy.

Let's execute Test Program.

So, immediately, let's compile and run this program.

When compiled, the command parameters as follows: the capture or QTHELP,. Pro needs to be appended to.
> qmake "QT+=testlib"

If you want to add .pro file, you should add a following line.

QT += testlib


When you run this program, the results appear below.
Properly test pattern line 33, "FAIL (failed)" is.

>testnew.exe
********* Start testing of TestCTestPlus *********
Config: Using QTest library 4.5.1, Qt 4.5.1
PASS   : TestCTestPlus::initTestCase()
RESULT : TestCTestPlus::testPlus():
     0.000044 msec per iteration (total: 47, iterations: 1048576)
PASS   : TestCTestPlus::testPlus()
FAIL!  :  style="color: #444;">TestCTestPlus::testMinus(-1+4) Compared values are not the same
   Actual (sobj.minus(minus)): 3
   Expected (result): 5
.\sample.cpp(47) : failure location
PASS   : TestCTestPlus::cleanupTestCase()
Totals: 3 passed, 1 failed, 0 skipped
********* Finished testing of TestCTestPlus *********
 
>testnew.exe testPlus
********* Start testing of TestCTestPlus *********
Config: Using QTest library 4.5.1, Qt 4.5.1
PASS   : TestCTestPlus::initTestCase()
RESULT : TestCTestPlus::testPlus():
     0.000044 msec per iteration (total: 47, iterations: 1048576)
PASS   : TestCTestPlus::testPlus()
PASS   : TestCTestPlus::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of TestCTestPlus *********
 
>testnew.exe testMinus:-1+4
********* Start testing of TestCTestPlus *********
Config: Using QTest library 4.5.1, Qt 4.5.1
PASS   : TestCTestPlus::initTestCase()
FAIL!  :  style="color: #444;">TestCTestPlus::testMinus(-1+4) Compared values are not the same
   Actual (sobj.minus(minus)): 3
   Expected (result): 5
.\sample.cpp(47) : failure location
PASS   : TestCTestPlus::cleanupTestCase()
Totals: 2 passed, 1 failed, 0 skipped
********* Finished testing of TestCTestPlus *********
 
>testnew.exe testMinus:-1+5
********* Start testing of TestCTestPlus *********
Config: Using QTest library 4.5.1, Qt 4.5.1
PASS   : TestCTestPlus::initTestCase()
PASS   : TestCTestPlus::testMinus()
PASS   : TestCTestPlus::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of TestCTestPlus *********
 
>testnew.exe testPlus -tickcounter
********* Start testing of TestCTestPlus *********
Config: Using QTest library 4.5.1, Qt 4.5.1
PASS   : TestCTestPlus::initTestCase()
RESULT : TestCTestPlus::testPlus():
     664 ticks per iteration (total: 664, iterations: 1)
PASS   : TestCTestPlus::testPlus()
PASS   : TestCTestPlus::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of TestCTestPlus *********

It is very simple, QTestLib main feature, here, (GUI is not part of the test) are included.
The input pattern is changed, the program will run this test, we can specify the parameters, I had a test pattern as the above example.
(Details of the parameters, see above)


Program, you can specify the following parameters automatically.
testname [options] [testfunctions [: testdata] ] ...

[options] Will refer to later.

[testfunctions [: testdata] ]
Specify if you want to run some test methods. Format below.
Method name: The name ※ If you do not enter the test pattern test pattern names are all variations of the test method specified.

  • -help
    outputs the possible command line arguments and give some useful help.
  • -functions
    outputs all test functions available in the test.
  • -o filename
    write output to the specified file, rather than to standard output
  • -silent
    silent output, only shows warnings, failures and minimal status messages
  • -v1
    verbose output; outputs information on entering and exiting test functions.
  • -v2
    extended verbose output; also outputs each QCOMPARE () and QVERIFY ()
  • -vs
    outputs every signal that gets emitted
  • -xml
    outputs XML formatted results instead of plain text
  • -lightxml
    outputs results as a stream of XML tags
  • -eventdelay ms
    if no delay is specified for keyboard or mouse simulation (QTest:: keyClick (), QTest:: mouseClick () etc.), the value from this parameter (in milliseconds) is substituted.
  • -keydelay ms
    like-eventdelay, but only influences keyboard simulation and not mouse simulation.
  • -mousedelay ms
    like-eventdelay, but only influences mouse simulation and not keyboard simulation.
  • -keyevent-verbose
    output more verbose output for keyboard simulation
  • -maxwarnings numberBR sets the maximum number of warnings to output. 0 for unlimited, defaults to 2000.

In benchmark tests, you can specify additional parameters as follows.
  • Walltime (default) All platforms
  • CPU tick counter-tickcounter Windows, Mac OS X, Linux, many UNIX-like systems.
  • Valgrind / Callgrind-callgrind Linux (if installed)
  • Event Counter-eventcounter All platforms

An example of a simple screen test
The following is an example of the screen test in the tutorial.
Dare not explain what the tutorial.

[sample.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <QtGui>
#include <QtTest/QtTest>
 
class TestKeyType: public QObject
{
	Q_OBJECT
 
private slots:
	void testKeyInput();
 
};
 
void TestKeyType::testKeyInput()
{
	QLineEdit lineEdit;
	QTest::keyClicks(&lineEdit, "hello world");
	QCOMPARE(lineEdit.text(), QString("hello world"));
 
	QTest::keyClicks(&lineEdit, "hello worlD2");
	QCOMPARE(lineEdit.text(), QString("hello world2"));
}
 
QTEST_MAIN(TestKeyType)
#include "sample.moc"


[Results]
********* Start testing of TestKeyType *********
Config: Using QTest library 4.5.1, Qt 4.5.1
PASS   : TestKeyType::initTestCase()
FAIL!  :  style="color: #444;">TestKeyType::testKeyInput() Compared values are not the same
   Actual (lineEdit.text()): hello worldhello worlD2
   Expected (QString("hello world2")): hello world2
.\sample.cpp(20) : failure location
PASS   : TestKeyType::cleanupTestCase()
Totals: 2 passed, 1 failed, 0 skipped
********* Finished testing of TestKeyType *********

Above, keyClicks uses, the following can be used also.

  • QTest:: keyClicks ()
  • QTest:: keyClick ()
  • QTest:: keyPress ()
  • QTest:: keyRelease ()
  • QTest:: mouseClick ()
  • QTest:: mouseDClick ()
  • QTest:: mouseMove ()
  • QTest:: mousePress ()
  • QTest:: mouseRelease ()

[sample.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <QtGui>
#include <QtTest/QtTest>
 
class TestKeyType: public QObject
{
	Q_OBJECT
 
private slots:
	void testKeyInput_data();
	void testKeyInput();
};
 
void TestKeyType::testKeyInput_data()
{
	QTest::addColumn<QTestEventList>("events");
	QTest::addColumn<QString>("expected");
 
	QTestEventList list1;
	list1.addKeyClick('a');
	QTest::newRow("char") << list1 << "a";
 
	QTestEventList list2;
	list2.addKeyClick('a');
	list2.addKeyClick(Qt::Key_Backspace);
	list2.addKeyClick(Qt::Key_Backspace);
	list2.addKeyClick(Qt::Key_Backspace);
	list2.addKeyClick(Qt::Key_Backspace);
	QTest::newRow("there and back again") << list2 << "";
 
	QTestEventList list3;
	list3.addKeyClick('a');
	list3.addKeyClick('b');
	list3.addKeyClick('c');
	list3.addKeyClick('d');
	QTest::newRow("char") << list3 << "a";
 
}
 
void TestKeyType::testKeyInput()
{
	QFETCH(QTestEventList, events);
	QFETCH(QString, expected);
 
	QLineEdit lineEdit;
 
	events.simulate(&lineEdit);
 
	QCOMPARE(lineEdit.text(), expected);
}
 
 
QTEST_MAIN(TestKeyType)
#include "sample.moc"


[Results]
********* Start testing of TestKeyType *********
Config: Using QTest library 4.5.1, Qt 4.5.1
PASS   : TestKeyType::initTestCase()
FAIL!  :  style="color: #444;">TestKeyType::testKeyInput(char) Compared values are not the same
   Actual (lineEdit.text()): abcd
   Expected (expected): a
.\sample.cpp(48) : failure location
PASS   : TestKeyType::cleanupTestCase()
Totals: 2 passed, 1 failed, 0 skipped
********* Finished testing of TestKeyType *********


At first, I tested on matters of a street. CppUnit than I can describe it.
Also, some screens, you can automate the testing.
And test the algorithm, and test capabilities, I would be extremely useful.
However, automated testing of the screen, it is that it is a personal impression.

And any feedback, so thank you for comment.

Comments

Leave a Reply







  • はてなブックマークへ追加する
  • Facebookでシェアする
  • twitter でつぶやく
  • Google Plusでシェアする
  • Pocketでシェアする
ページトップへ