[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] cmocka: Allow to pass initial data to test cases


Just a ping to let you know about pending path.

On 05/21/2015 05:33 PM, Pawel Szewczyk wrote:

Sometimes multiple test cases share the same test function, running it
on different data. To pass this data to test functions we must define
setup function for each data set. It's not very convienient when there
are many states to test.

This commit introduce more elegant way to pass data to tests. The
initial_state field of CMUnitTest structure can be defined by user.
It will be either passed to setup function, which makes any preparation
needed, or it will be passed directly to test function when setup func is
NULL.

Signed-off-by: Pawel Szewczyk <p.szewczyk@xxxxxxxxxxx>
---
  include/cmocka.h | 15 +++++++++++----
  src/cmocka.c     |  5 +++++
  2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/include/cmocka.h b/include/cmocka.h
index 797fbe2..10a606b 100644
--- a/include/cmocka.h
+++ b/include/cmocka.h
@@ -1489,19 +1489,25 @@ static inline void _unit_test_dummy(void **state) {
/** Initializes a CMUnitTest structure. */
-#define cmocka_unit_test(f) { #f, f, NULL, NULL }
+#define cmocka_unit_test(f) { #f, f, NULL, NULL, NULL }
/** Initializes a CMUnitTest structure with a setup function. */
-#define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL }
+#define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL, NULL }
/** Initializes a CMUnitTest structure with a teardown function. */
-#define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown }
+#define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown, NULL }
/**
   * Initialize an array of CMUnitTest structures with a setup function for a test
   * and a teardown function. Either setup or teardown can be NULL.
   */
-#define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup, teardown }
+#define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup, teardown, NULL }
+
+/**
+ * Initialize a CMUnitTest structure with setup, teardown and initial state (passed
+ * to setup function). Any of these values can be NULL.
+ */
+#define cmocka_unit_test_inital_state(f, setup, teardown, state) { #f, f, setup, teardown, state }
#define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
  #define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / sizeof(tests)[0])
@@ -1894,6 +1900,7 @@ struct CMUnitTest {
      CMUnitTestFunction test_func;
      CMFixtureFunction setup_func;
      CMFixtureFunction teardown_func;
+    void *initial_state;
  };
/* Location within some source code. */
diff --git a/src/cmocka.c b/src/cmocka.c
index fc83b57..871a375 100644
--- a/src/cmocka.c
+++ b/src/cmocka.c
@@ -2543,6 +2543,11 @@ int _cmocka_run_group_tests(const char *group_name,
              if (group_state != NULL) {
                  cm_tests[i].state = group_state;
              }
+
+            if (cm_tests[i].test->initial_state != NULL) {
+                cm_tests[i].state = cm_tests[i].test->initial_state;
+            }
+
              rc = cmocka_run_one_tests(cmtest);
              total_executed++;
              total_runtime += cmtest->runtime;



Follow-Ups:
Re: [PATCH] cmocka: Allow to pass initial data to test casesJakub Hrozek <jakub.hrozek@xxxxxxxxx>