首页 > 解决方案 > 在具有 FreeRTOS 依赖性的项目中进行单元测试

问题描述

我正在尝试在CMock 的帮助下(了解更多关于 CMock的信息)在 Unity 中实现单元测试(了解更多关于 Unity的信息)。为此,我使用了 Ceedling 工具,它将所有 Unity 组件组合到一个框架中(了解有关 Ceedling的更多信息)。

在我将 FreeRTOS 添加到我的项目/测试之前,一切都按预期工作。添加后,Ceedling 难以为 FreeRTOS 组件(如 queue.c 或 list.c)生成模拟。它给出以下错误:

Generating include list for queue.h...
build/temp/_queue.h:32:6: error: #error "include FreeRTOS.h" must appear in source files before "include queue.h"
   32 |     #error "include FreeRTOS.h" must appear in source files before "include queue.h"
      |      ^~~~~
In file included from build/temp/_queue.h:40:
../build/_deps/rtos.freertos-src/include/task.h:32:6: error: #error "include FreeRTOS.h must appear in source files before include task.h"
   32 |     #error "include FreeRTOS.h must appear in source files before include task.h"
      |      ^~~~~
In file included from ../build/_deps/rtos.freertos-src/include/task.h:35,
                 from build/temp/_queue.h:40:
../build/_deps/rtos.freertos-src/include/list.h:60:6: error: #error "FreeRTOS.h must be included before list.h"
   60 |     #error "FreeRTOS.h must be included before list.h"
      |      ^~~~~

对我来说,它看起来像编译 queue.c 文件以找出应该模拟的函数列表。没关系,但我不知道在检查 queue.c 或 list.c 文件时强制它添加 FreeRTOS.h 包含。

我修改了我的project.yml文件,因此添加了 FreeRTOS 的路径,并将 FreeRTOS.h 标头添加到包括:

:paths:
  :test:
    - +:test/**
    - -:test/support
    - ../
    - ../build/_deps/rtos.freertos-src/portable/GCC/ARM_CM4F
    - ../build/_deps/rtos.freertos-src/include
    - ../debug/
  :source:
    - src/**
    - ../build/_deps/rtos.freertos-src/portable/GCC/ARM_CM4F/**
    - ../build/_deps/rtos.freertos-src/include/**
    - ../build/_deps/rtos.freertos-src/**
  :support:
    - test/support
  :libraries: []

... skipped lines ...
... skipped lines ...
... skipped lines ...

:cmock:
  :mock_prefix: mock_
  :when_no_prototypes: :warn
  :enforce_strict_ordering: TRUE
  :plugins:
    - :ignore
    - :callback
  :treat_as:
    uint8:    HEX8
    uint16:   HEX16
    uint32:   UINT32
    int8:     INT8
    bool:     UINT8
  :includes_h_pre_orig_header:
    - FreeRTOS.h
  :includes_c_pre_header:
    - FreeRTOS.h
  :includes:
    - ../debug/assert.h
    - FreeRTOS.h

即使添加了includes_h_pre_orig_headerincludes_c_pre_header包含它仍然像FreeRTOS.h一样失败。没有在queue.hlist.h头文件之前添加。

我发现了类似的问题,解决方法是在测试脚本中添加对 SetUp 和 Teardown 方法的 Init 和 Verify 调用,并为与 FreeRTOS 相关的调用添加 Expect。我没有运气就试过了。

我的测试脚本很简单:

#include "unity.h"
#include "cmock.h"

#include "FreeRTOS.h"
#include "FreeRTOSConfig.h"
#include "mock_queue.h"

#include "test.h"

void setUp(void) {
    mock_queue_Init();
    mock_list_Init();
    mock_task_Init();

}

void tearDown(void) {
    mock_queue_Verify();
    mock_list_Verify();
    mock_task_Verify();
}

void test_Rtos(void)
{
    xQueueCreate_Expect(8, 16);

    TestRtos();
}

源文件也很简单,只是为了检查 Ceedling 是否可以与 FreeRTOS 一起使用而创建的:

#include "test.h"

#include "FreeRTOSConfig.h"
#include "FreeRTOS.h"
#include "queue.h"

void TestRtos(void)
{
    QueueHandle_t someQueue = xQueueCreate(8, 16);
    uxQueueMessagesWaiting(someQueue);
}

我还查看了来自 Amazon 的一些示例,但他们的project.yml甚至没有提及与 FreeRTOS 相关的一些路径或头文件。我在这里错过了什么?

标签: unit-testingfreertosunity-test-frameworkcmockceedling

解决方案


我有同样的问题。我通过在文件 queue.h (例如)一开始就包含文件 FreeRTOS.h 来解决它。我认为这不是最好的解决方案,但它确实有效。

谢谢。


推荐阅读