You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为STM32项目创建最优可定制的Ceedling单元测试配置

STM32 + Ceedling 单元测试配置最佳实践

用户需求

我正在基于STM32F407G-DISC1开发STM32项目,想要搭建Ceedling进行单元测试。需要保留STM32CubeMX或STM32CubeIDE使用的标准目录结构,同时构建一套健壮且可定制的Ceedling配置。我已经自己写了project.yml文件,但不确定是否完成了所有必要步骤。我的目标是找到一套能轻松适配不同STM32项目的优质Ceedling配置,求该场景下的最佳实践或示例。

现有project.yml配置

:project:
  :use_exceptions: FALSE
  :use_test_preprocessor: :all
  :use_auxiliary_dependencies: TRUE
  :build_root: build
  :release_build: TRUE
  :test_file_prefix: test_
  :which_ceedling: gem
  :ceedling_version: 0.31.1
  :default_tasks:
    - test:all

#:test_build:
# :use_assembly: TRUE

:release_build:
  :output: stmBase
  :use_assembly: TRUE
  :artifacts:
    - stmBase.map

# :output: MyApp.out
# :use_assembly: FALSE

:environment:

:extension:
  :executable: .elf

:paths:
  :test:
    - +:test/**
    - -:test/support
  :source:
    - Core/Inc/**
    - Core/Src/**
  :include:
    - Core/Inc/
    - Drivers/CMSIS/Device/ST/STM32F4xx/Include/
    - Drivers/CMSIS/Include/
    - Drivers/STM32F4xx_HAL_Driver/Inc/
  :toolchain_include:
    - USB_HOST/App
    - USB_HOST/Target
    - Core/Inc
    - build/vendor/unity/src
    - Drivers/STM32F4xx_HAL_Driver/Inc
    - Drivers/STM32F4xx_HAL_Driver/Inc/Legacy
    - Middlewares/ST/STM32_USB_Host_Library/Core/Inc
    - Middlewares/ST/STM32_USB_Host_Library/Class/CDC/Inc
    - Drivers/CMSIS/Device/ST/STM32F4xx/Include
    - Drivers/CMSIS/Include
  :support:
    - test/support

  :libraries: []

# :exclude_files:
#   - Core\Src\main.c

:defines:
  :common: &common_defines
    - STM32F407xx 
    - USE_HAL_DRIVER 
  :test:
    - *common_defines
    - TEST
  :test_preprocess:
    - *common_defines
    - TEST

: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
  # TODO: herausfinden, ob das richtig ist.
  :includes:
    - <stdbool.h>
    - <stdint.h>
  :treat_externs: :include


:gcov:
  :reports:
    - HtmlDetailed
  :gcovr:
    :html_medium_threshold: 75
    :html_high_threshold: 90

:tools:
  :test_compiler:
    :executable: arm-none-eabi-gcc
    :arguments:
      - ${1}
      - -mcpu=cortex-m4
      - -std=gnu11
      - -g3
      - -DDEBUG
      - -DUSE_HAL_DRIVER
      - -DSTM32F407xx
      - -c
      - -I"$": COLLECTION_PATHS_TOOLCHAIN_INCLUDE
      - -O0
      - -ffunction-sections
      - -fdata-sections
      - -Wall
      - -fstack-usage
      - -fcyclomatic-complexity
      - -MMD
      - -MP
      - -MF"${2}.d"
      - -MT"${2}.o"
      - --specs=nano.specs
      - -mfpu=fpv4-sp-d16
      - -mfloat-abi=hard
      - -mthumb
      - -o ${2}

  :test_linker:
    :executable: arm-none-eabi-gcc
    :arguments:

      - ${1}
      - -DTARGET
      - -Isrc/ 
      - -I"$": COLLECTION_PATHS_RELEASE_TOOLCHAIN_INCLUDE 
      - -mcpu=cortex-m4 
      - -mthumb 
      - -mfpu=fpv4-sp-d16 
      - -mfloat-abi=hard 
      - -Wl,-Map="${2}.map" 
      - -g 
      - -T"STM32F407VGTX_FLASH.ld" 
      - --specs=nosys.specs 
      - -Wl,--gc-sections 
      - -static 
      - --specs=nano.specs 
      - -Wl,--start-group -lc -lm -Wl,--end-group 
      - -o ${2}.elf 


:libraries:
  :placement: :end

  :flag: "-l${1}"

  :path_flag: "-L ${1}"

  :system: [] 

  :test: []

  :release: []

:plugins:
  :load_paths: []
  :enabled: []

配置优化与最佳实践

1. 目录与文件过滤

  • 启用main.c排除:主函数依赖硬件初始化逻辑,单元测试无需编译,取消注释并修正路径格式:
    :exclude_files:
      - Core/Src/main.c
    
  • 确保所有路径使用Unix风格斜杠,避免跨平台兼容问题。

2. 编译与链接参数精简

  • 测试编译器:移除重复定义的-DUSE_HAL_DRIVER和-DSTM32F407xx,复用:defines中的公共宏,减少冗余配置。
  • 测试链接器:
    • 移除无效的-Isrc/路径(Cube项目无此目录)
    • 将COLLECTION_PATHS_RELEASE_TOOLCHAIN_INCLUDE改为COLLECTION_PATHS_TOOLCHAIN_INCLUDE,确保测试链接时能找到所有头文件
    • 移除-T"STM32F407VGTX_FLASH.ld"链接脚本,单元测试无需模拟硬件内存布局,使用默认脚本可加快编译速度

3. CMock配置强化

  • 补充64位类型映射:适配更多场景的mock需求
    :treat_as:
      uint8: HEX8
      uint16: HEX16
      uint32: UINT32
      uint64: UINT64
      int8: INT8
      int64: INT64
      bool: UINT8
    
  • 修改外部变量处理策略:将:treat_externs: :include改为:exclude,避免自动生成不必要的外部变量mock,仅手动指定需要mock的变量。

4. 代码覆盖率增强

  • 添加总览报告类型,优化报告可读性:
    :gcov:
      :reports:
        - HtmlDetailed
        - HtmlSummary
      :gcovr:
        :html_medium_threshold: 75
        :html_high_threshold: 90
        :html_title: "STM32 项目测试覆盖率报告"
    

5. 可移植性与扩展性

  • 抽象芯片型号宏:通过环境变量或默认值适配不同STM32型号
    :defines:
      :common: &common_defines
        - ${CHIP_TYPE:-STM32F407xx}
        - USE_HAL_DRIVER
    
  • 启用module_generator插件,快速生成测试文件模板:
    :plugins:
      :enabled:
        - module_generator
    

优化后完整配置示例

:project:
  :use_exceptions: FALSE
  :use_test_preprocessor: :all
  :use_auxiliary_dependencies: TRUE
  :build_root: build
  :release_build: TRUE
  :test_file_prefix: test_
  :which_ceedling: gem
  :ceedling_version: 0.31.1
  :default_tasks:
    - test:all

:release_build:
  :output: stmBase
  :use_assembly: TRUE
  :artifacts:
    - stmBase.map

:extension:
  :executable: .elf

:paths:
  :test:
    - +:test/**
    - -:test/support
  :source:
    - Core/Inc/**
    - Core/Src/**
  :include:
    - Core/Inc/
    - Drivers/CMSIS/Device/ST/STM32F4xx/Include/
    - Drivers/CMSIS/Include/
    - Drivers/STM32F4xx_HAL_Driver/Inc/
  :toolchain_include:
    - USB_HOST/App
    - USB_HOST/Target
    - Core/Inc
    - build/vendor/unity/src
    - Drivers/STM32F4xx_HAL_Driver/Inc
    - Drivers/STM32F4xx_HAL_Driver/Inc/Legacy
    - Middlewares/ST/STM32_USB_Host_Library/Core/Inc
    - Middlewares/ST/STM32_USB_Host_Library/Class/CDC/Inc
    - Drivers/CMSIS/Device/ST/STM32F4xx/Include
    - Drivers/CMSIS/Include
  :support:
    - test/support
  :libraries: []

:exclude_files:
  - Core/Src/main.c

:defines:
  :common: &common_defines
    - ${CHIP_TYPE:-STM32F407xx}
    - USE_HAL_DRIVER
  :test:
    - *common_defines
    - TEST
  :test_preprocess:
    - *common_defines
    - TEST

:cmock:
  :mock_prefix: mock_
  :when_no_prototypes: :warn
  :enforce_strict_ordering: TRUE
  :plugins:
    - :ignore
    - :callback
  :treat_as:
    uint8: HEX8
    uint16: HEX16
    uint32: UINT32
    uint64: UINT64
    int8: INT8
    int64: INT64
    bool: UINT8
  :includes:
    - <stdbool.h>
    - <stdint.h>
  :treat_externs: :exclude

:gcov:
  :reports:
    - HtmlDetailed
    - HtmlSummary
  :gcovr:
    :html_medium_threshold: 75
    :html_high_threshold: 90
    :html_title: "STM32 项目测试覆盖率报告"

:tools:
  :test_compiler:
    :executable: arm-none-eabi-gcc
    :arguments:
      - ${1}
      - -mcpu=cortex-m4
      - -std=gnu11
      - -g3
      - -DDEBUG
      - -c
      - -I"$": COLLECTION_PATHS_TOOLCHAIN_INCLUDE
      - -O0
      - -ffunction-sections
      - -fdata-sections
      - -Wall
      - -fstack-usage
      - -fcyclomatic-complexity
      - -MMD
      - -MP
      - -MF"${2}.d"
      - -MT"${2}.o"
      - --specs=nano.specs
      - -mfpu=fpv4-sp-d16
      - -mfloat-abi=hard
      - -mthumb
      - -o ${2}

  :test_linker:
    :executable: arm-none-eabi-gcc
    :arguments:
      - ${1}
      - -DTARGET
      - -I"$": COLLECTION_PATHS_TOOLCHAIN_INCLUDE
      - -mcpu=cortex-m4
      - -mthumb
      - -mfpu=fpv4-sp-d16
      - -mfloat-abi=hard
      - -Wl,-Map="${2}.map"
      - -g
      - --specs=nosys.specs
      - -Wl,--gc-sections
      - -static
      - --specs=nano.specs
      - -Wl,--start-group -lc -lm -Wl,--end-group
      - -o ${2}.elf

:libraries:
  :placement: :end
  :flag: "-l${1}"
  :path_flag: "-L ${1}"
  :system: []
  :test: []
  :release: []

:plugins:
  :load_paths: []
  :enabled:
    - module_generator

内容的提问来源于stack exchange,提问作者HEIM_MAG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 13:12:02