diff options
author | Charles Yin <yinyunqiao@gmail.com> | 2012-03-30 22:59:20 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-04-03 07:50:17 +0200 |
commit | 5b090cd9bc642582d214addba154ed022f6e537e (patch) | |
tree | e4474c9bc23cf2b90ca3b2d7702b55ad2b9d6192 /src/imports/testlib | |
parent | 817c0741d1d3e1dc1c505652ba764fe96b584aac (diff) |
Add init_data() function for default data driven testing
If all or some test functions share the same test data, the test data
can be put into init_data() function, this can reduce unnecessary code
duplications.
Change-Id: I118dd9c3c1d0f2f238aab6e25e5d8af80b3d3049
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
Diffstat (limited to 'src/imports/testlib')
-rw-r--r-- | src/imports/testlib/TestCase.qml | 7 | ||||
-rw-r--r-- | src/imports/testlib/testcase.qdoc | 25 |
2 files changed, 26 insertions, 6 deletions
diff --git a/src/imports/testlib/TestCase.qml b/src/imports/testlib/TestCase.qml index 1f9de5e998..694c01a710 100644 --- a/src/imports/testlib/TestCase.qml +++ b/src/imports/testlib/TestCase.qml @@ -475,6 +475,7 @@ Item { function cleanupTestCase() {} function init() {} function cleanup() {} + function init_data() {} function qtest_runInternal(prop, arg) { try { @@ -607,6 +608,10 @@ Item { functionsToRun.splice(index, 1) } qtest_results.functionName = prop + + if (!testCase.hasOwnProperty(datafunc)) + datafunc = "init_data"; + if (datafunc in testCase) { if (qtest_runInternal(datafunc)) { var table = qtest_testCaseResult @@ -624,7 +629,7 @@ Item { qtest_runFunction(prop, row) qtest_results.dataTag = "" } - if (!haveData) + if (!haveData && datafunc != "init_data") qtest_results.warn("no data supplied for " + prop + "() by " + datafunc + "()" , util.callerFile(), util.callerLine()); qtest_results.clearTestTable() diff --git a/src/imports/testlib/testcase.qdoc b/src/imports/testlib/testcase.qdoc index f928cdaa44..29f7a2d287 100644 --- a/src/imports/testlib/testcase.qdoc +++ b/src/imports/testlib/testcase.qdoc @@ -51,8 +51,8 @@ element: \code - import QtQuick 1.0 - import QtQuickTest 1.0 + import QtQuick 2.0 + import QtTest 1.0 TestCase { name: "MathTests" @@ -97,15 +97,24 @@ \section1 Data-driven tests Table data can be provided to a test using a function name that ends - with "_data": + with "_data". Alternatively, the \c init_data() function can be used + to provide default test data for all test functions in a TestCase element: + \code - import QtQuick 1.0 - import QtQuickTest 1.0 + import QtQuick 2.0 + import QtTest 1.0 TestCase { name: "DataTests" + function init_data() { + return [ + {tag:"init_data_1", a:1, b:2, answer: 3}, + {tag:"init_data_2", a:2, b:4, answer: 6} + ]; + } + function test_table_data() { return [ {tag: "2 + 2 = 4", a: 2, b: 2, answer: 4 }, @@ -114,6 +123,12 @@ } function test_table(data) { + //data comes from test_table_data + compare(data.a + data.b, data.answer) + } + + function test__default_table(data) { + //data comes from init_data compare(data.a + data.b, data.answer) } } |