Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for parameters with RUN_TEST #571

Open
n-marion opened this issue Jul 23, 2021 · 1 comment
Open

Support for parameters with RUN_TEST #571

n-marion opened this issue Jul 23, 2021 · 1 comment

Comments

@n-marion
Copy link

RUN_TEST currently supports passing in only a test name. It'd be very valuable to pass in different parameters with RUN_TEST to prevent the need to create extra functions and tests.

Current Implementation:

run_test(bool test) {
  do something...
}

true_test() {
  run_test(true)
}

false_test() {
  run_test(false)
}

main() {
  RUN_TEST(true_test)
  RUN_TEST(false_test)
}

Possible support for:

run_test(bool test) {
  do something...
}

main () {
  RUN_TEST(run_test(true))
  RUN_TEST(run_test(false))
}

This is a very, very basic example, but I have a need to setup large amounts of fields and such multiple times for tests that could be just slightly different. It'd be great to be able to create a single test and pass a parameter to drive it without the unnecessary extra functions. Additionally when the output is generated, maybe adding the parameter to the name could help to separate it.. such as: run_test-true and run_test-false.

@pstarAllgon
Copy link

I have implemented something like this where RUN_TEST can take a void*. Below follows a redacted diff (stripped my projects path etc.) of the files which I have modified.

--- a/unity.c
+++ b/unity.c
@@ -2180,6 +2180,25 @@ void UnityMessage(const char* msg, const UNITY_LINE_TYPE line)
 /*-----------------------------------------------*/
 /* If we have not defined our own test runner, then include our default test runner to make life easier */
 #ifndef UNITY_SKIP_DEFAULT_RUNNER
+void UnityDefaultTestRunArgs(UnityTestFunctionArgs Func, void* args, const char* FuncName, const int FuncLineNum)
+{
+    Unity.CurrentTestName = FuncName;
+    Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum;
+    Unity.NumberOfTests++;
+    UNITY_CLR_DETAILS();
+    UNITY_EXEC_TIME_START();
+    if (TEST_PROTECT())
+    {
+        setUp();
+        Func(args);
+    }
+    if (TEST_PROTECT())
+    {
+        tearDown();
+    }
+    UNITY_EXEC_TIME_STOP();
+    UnityConcludeTest();
+}
 void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
 {
     Unity.CurrentTestName = FuncName;
diff --git a/unity_internals.h b/unity_internals.h
index 47bd370..e661fc5 100644
--- a/unity_internals.h
+++ b/unity_internals.h
@@ -419,6 +419,7 @@ typedef UNITY_FLOAT_TYPE UNITY_FLOAT;
  *-------------------------------------------------------*/
 
 typedef void (*UnityTestFunction)(void);
+typedef void (*UnityTestFunctionArgs)(void*);
 
 #define UNITY_DISPLAY_RANGE_INT  (0x10)
 #define UNITY_DISPLAY_RANGE_UINT (0x20)
@@ -525,6 +526,7 @@ void UnityConcludeTest(void);
 
 #ifndef RUN_TEST
 void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum);
+void UnityDefaultTestRunArgs(UnityTestFunctionArgs Func, void* args,  const char* FuncName, const int FuncLineNum);
 #else
 #define UNITY_SKIP_DEFAULT_RUNNER
 #endif
@@ -781,6 +783,9 @@ extern const char UnityStrErrShorthand[];
 #ifdef UNITY_SUPPORT_VARIADIC_MACROS
 #define RUN_TEST(...) RUN_TEST_AT_LINE(__VA_ARGS__, __LINE__, throwaway)
 #define RUN_TEST_AT_LINE(func, line, ...) UnityDefaultTestRun(func, #func, line)
+
+#define RUN_TEST_ARGS(...) RUN_TEST_ARGS_AT_LINE(__VA_ARGS__, __LINE__, throwaway)
+#define RUN_TEST_ARGS_AT_LINE(func, args, line, ...) UnityDefaultTestRunArgs(func, args, #func, line)
 #endif
 #endif
 
@@ -800,8 +805,10 @@ extern const char UnityStrErrShorthand[];
 #ifndef RUN_TEST
 #ifdef CMOCK
 #define RUN_TEST(func, num) UnityDefaultTestRun(func, #func, num)
+#define RUN_TEST_ARGS(func, args, num) UnityDefaultTestRunArgs(func, args, #func, num)
 #else
 #define RUN_TEST(func) UnityDefaultTestRun(func, #func, __LINE__)
+#define RUN_TEST_ARGS(func, args) UnityDefaultTestRunArgs(func, args, #func, __LINE__)
 #endif
 #endif```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants