Add init_data() function for default data driven testing
authorCharles Yin <yinyunqiao@gmail.com>
Fri, 30 Mar 2012 12:59:20 +0000 (22:59 +1000)
committerQt by Nokia <qt-info@nokia.com>
Tue, 3 Apr 2012 05:50:17 +0000 (07:50 +0200)
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>
src/imports/testlib/TestCase.qml
src/imports/testlib/testcase.qdoc

index 1f9de5e..694c01a 100644 (file)
@@ -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()
index f928cda..29f7a2d 100644 (file)
@@ -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"
     \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 },
         }
 
         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)
         }
     }