From 5b090cd9bc642582d214addba154ed022f6e537e Mon Sep 17 00:00:00 2001 From: Charles Yin Date: Fri, 30 Mar 2012 22:59:20 +1000 Subject: [PATCH] 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 --- src/imports/testlib/TestCase.qml | 7 ++++++- 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 1f9de5e..694c01a 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 f928cda..29f7a2d 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) } } -- 2.7.4