放弃新增的代码,直接采用现有结构。在头文件宏定义中添加__FILE__结果符合预期。不能在unity.c文件中这样做。否则所有的测试汇总信息都定位到了unity。

This commit is contained in:
建峰 2024-08-29 00:54:26 +08:00
parent f87dc997eb
commit 18a14d6d03
5 changed files with 13 additions and 77 deletions

View File

@ -1,16 +1,12 @@
# 问题和解决方案 # 问题和解决方案
## 1. 多文件打印测试文件路径不合理的问题 ## 1. 添加配置文件
新增宏定义可放于unity_config.h中用来让测试文件路径显示为当前测试文件而不是主test.c文件。 在unity_internals.h文件中添加了宏定义方便使用unity_config.h文件。
方便针对多文件测试,快速定位错误。
```c ```c
#define RUN_TEST_WITH_CURRENT_FILE 1 #define UNITY_INCLUDE_CONFIG_H
``` ```
另外,修改了两个函数,新增文件路径参数。 ## 2. 多文件打印测试文件路径不合理的问题
```c 修改`RUN_TEST`宏,在测试文件路径前添加`__FILE__`。
void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum, const char* FileName); 借助`UnitySetTestFile`函数,打印的路径信息对多文件测试也有效。
void UnityConcludeTest(const char *FileName);
```

View File

@ -543,7 +543,6 @@ static void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)
} }
/*-----------------------------------------------*/ /*-----------------------------------------------*/
#ifndef RUN_TEST_WITH_CURRENT_FILE
void UnityConcludeTest(void) void UnityConcludeTest(void)
{ {
if (Unity.CurrentTestIgnored) if (Unity.CurrentTestIgnored)
@ -566,31 +565,6 @@ void UnityConcludeTest(void)
UNITY_PRINT_EOL(); UNITY_PRINT_EOL();
UNITY_FLUSH_CALL(); UNITY_FLUSH_CALL();
} }
#else
void UnityConcludeTest(const char *FileName)
{
if (Unity.CurrentTestIgnored)
{
Unity.TestIgnores++;
}
else if (!Unity.CurrentTestFailed)
{
UnityTestResultsBegin(FileName, Unity.CurrentTestLineNumber);
UnityPrint(UnityStrPass);
}
else
{
Unity.TestFailures++;
}
Unity.CurrentTestFailed = 0;
Unity.CurrentTestIgnored = 0;
UNITY_PRINT_EXEC_TIME();
UNITY_PRINT_EOL();
UNITY_FLUSH_CALL();
}
#endif
/*-----------------------------------------------*/ /*-----------------------------------------------*/
static void UnityAddMsgIfSpecified(const char* msg) static void UnityAddMsgIfSpecified(const char* msg)
@ -2216,7 +2190,6 @@ 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 */ /* If we have not defined our own test runner, then include our default test runner to make life easier */
#ifndef UNITY_SKIP_DEFAULT_RUNNER #ifndef UNITY_SKIP_DEFAULT_RUNNER
#ifndef RUN_TEST_WITH_CURRENT_FILE
void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum) void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum)
{ {
Unity.CurrentTestName = FuncName; Unity.CurrentTestName = FuncName;
@ -2236,27 +2209,6 @@ void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int
UNITY_EXEC_TIME_STOP(); UNITY_EXEC_TIME_STOP();
UnityConcludeTest(); UnityConcludeTest();
} }
#else
void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum, const char* FileName)
{
Unity.CurrentTestName = FuncName;
Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum;
Unity.NumberOfTests++;
UNITY_CLR_DETAILS();
UNITY_EXEC_TIME_START();
if (TEST_PROTECT())
{
setUp();
Func();
}
if (TEST_PROTECT())
{
tearDown();
}
UNITY_EXEC_TIME_STOP();
UnityConcludeTest(FileName);
}
#endif
#endif #endif
/*-----------------------------------------------*/ /*-----------------------------------------------*/

View File

@ -5,6 +5,4 @@
#define UNITY_OUTPUT_COLOR 1 #define UNITY_OUTPUT_COLOR 1
#define UNITY_USE_FLUSH_STDOUT 1 #define UNITY_USE_FLUSH_STDOUT 1
#define RUN_TEST_WITH_CURRENT_FILE 1
#endif #endif

View File

@ -8,7 +8,7 @@
#ifndef UNITY_INTERNALS_H #ifndef UNITY_INTERNALS_H
#define UNITY_INTERNALS_H #define UNITY_INTERNALS_H
#define UNITY_INCLUDE_CONFIG_H 1 #define UNITY_INCLUDE_CONFIG_H
#ifdef UNITY_INCLUDE_CONFIG_H #ifdef UNITY_INCLUDE_CONFIG_H
#include "unity_config.h" #include "unity_config.h"
@ -546,19 +546,11 @@ extern struct UNITY_STORAGE_T Unity;
void UnityBegin(const char* filename); void UnityBegin(const char* filename);
int UnityEnd(void); int UnityEnd(void);
void UnitySetTestFile(const char* filename); void UnitySetTestFile(const char* filename);
#ifndef RUN_TEST_WITH_CURRENT_FILE
void UnityConcludeTest(void); void UnityConcludeTest(void);
#else
void UnityConcludeTest(const char *FileName);
#endif
#ifndef RUN_TEST #ifndef RUN_TEST
#ifndef RUN_TEST_WITH_CURRENT_FILE
void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum); void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum);
#else #else
void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum, const char* FileName);
#endif
#else
#define UNITY_SKIP_DEFAULT_RUNNER #define UNITY_SKIP_DEFAULT_RUNNER
#endif #endif
@ -825,13 +817,9 @@ extern const char UnityStrErrShorthand[];
#ifndef RUN_TEST #ifndef RUN_TEST
#ifdef UNITY_SUPPORT_VARIADIC_MACROS #ifdef UNITY_SUPPORT_VARIADIC_MACROS
#define RUN_TEST(...) RUN_TEST_AT_LINE(__VA_ARGS__, __LINE__, throwaway) #define RUN_TEST(...) RUN_TEST_AT_LINE(__VA_ARGS__, __LINE__, throwaway)
#ifndef RUN_TEST_WITH_CURRENT_FILE #define RUN_TEST_AT_LINE(func, line, ...) UnitySetTestFile(__FILE__);UnityDefaultTestRun(func, #func, line)
#define RUN_TEST_AT_LINE(func, line, ...) UnityDefaultTestRun(func, #func, line) #endif
#else #endif
#define RUN_TEST_AT_LINE(func, line, ...) UnityDefaultTestRun(func, #func, line, __FILE__)
#endif // RUN_TEST_WITH_CURRENT_FILE
#endif // UNITY_SUPPORT_VARIADIC_MACROS
#endif // RUN_TEST
/* Enable default macros for masking param tests test cases */ /* Enable default macros for masking param tests test cases */
#ifdef UNITY_SUPPORT_TEST_CASES #ifdef UNITY_SUPPORT_TEST_CASES

View File

@ -14,9 +14,11 @@ static void test_queue_destory(void)
{ {
queue_t queue = NULL; queue_t queue = NULL;
queue = queue_new(); queue = queue_new();
TEST_ASSERT_TRUE(queue_init(queue, sizeof(int)));
TEST_ASSERT_NOT_NULL(queue); TEST_ASSERT_NOT_NULL(queue);
TEST_ASSERT_TRUE(queue_init(queue, sizeof(int)));
TEST_ASSERT_NULL(queue);
queue_free(&queue); queue_free(&queue);
TEST_ASSERT_NULL(queue); TEST_ASSERT_NULL(queue);
} }