Yocto中ptest配置失败排查及命名规则咨询
关于Yocto中FooImpl单元测试配置的问题及错误排查
问题背景
我是Yocto新手,正在为从FooImpl拉取代码的recipe配置单元测试,但遇到错误无法定位根因。
相关文件内容
recipe文件(foo-impl-git.bb)
SUMMARY = "FooImpl" DESCRIPTION = "FooImpl Recipe" LICENSE = "CLOSED" DEPENDS = "foo-interface googletest" SRC_URI = "xxx/FooImpl;protocol=ssh;branch=mainline; \ file://run-ptest \ " SRCREV = "yyyyy" S = "${WORKDIR}/git" inherit cmake ptest do_install_ptest() { mkdir -p ${D}${PTEST_PATH}/tests cp -rf ${B}/test/foo-impl-unittest ${D}${PTEST_PATH}/tests } IMAGE_INSTALL:append = "foo-impl-ptest"
错误日志
[buildfarm-ssh] NOTE: recipe foo-impl-git-r0: task do_install_ptest_base: Started [buildfarm-ssh] ERROR: foo-impl-git-r0 do_install_ptest_base: ExecutionError('zzz/tmp/work/core2-64-poky-linux/foo-impl/git-r0/temp/run.do_install_ptest_base.23883', 1, None, None) [buildfarm-ssh] ERROR: Logfile of failure stored in: zzz/tmp/work/core2-64-poky-linux/foo-impl/git-r0/temp/log.do_install_ptest_base.23883 [buildfarm-ssh] Log data follows: | DEBUG: Executing shell function do_install_ptest_base | grep: Makefile: No such file or directory | cp: cannot stat 'zzz/tmp/work/core2-64-poky-linux/foo/git-r0/foo-impl-git//test/foo-impl-unittest': No such file or directory
run-ptest文件
#!/bin/sh tests/foo-impl-unittest if [ $? -eq 0 ]; then echo "PASS: $i" else echo "FAIL: $i" fi
FooImpl目录结构
- src - test - FooTest.cpp - CmakeLists.txt - CMakeLists.txt - Foo.cpp - CMakeLists.txt - .gitignore
疑问
- do_install_ptest中的
foo-impl-unittest是否可自定义?若不可,正确的命名规则是什么? IMAGE_INSTALL:append = "foo-impl-ptest"中的foo-impl-ptest是否可自定义?若不可,正确的命名规则是什么?- run-ptest文件中的
tests/foo-impl-unittest是否可自定义?若不可,正确的命名规则是什么?
解答及错误排查建议
疑问解答
- do_install_ptest中的
foo-impl-unittest:完全可自定义,名称必须与CMakeLists.txt中生成的测试可执行文件名一致。比如CMake中用add_executable(foo-impl-unittest FooTest.cpp),这个名称就正确;若CMake里命名不同,此处需对应修改。 foo-impl-ptest:不可随意自定义,规则为<recipe核心名称>-ptest。更稳妥的写法是用变量${PN}-ptest,Yocto中PN对应recipe的核心名称(比如你的recipe是foo-impl-git.bb,PN一般为foo-impl-git),直接写IMAGE_INSTALL:append = "${PN}-ptest"可以避免硬编码错误。- run-ptest中的
tests/foo-impl-unittest:可自定义,路径和名称需与do_install_ptest中安装到${D}${PTEST_PATH}下的测试可执行文件完全匹配即可。
错误排查建议
- 确认测试可执行文件的生成位置:
- 先检查build目录(
${B})下是否存在目标文件,可通过find ${B} -name "foo-impl-unittest"命令查找。从错误日志看,你写的${B}/test/foo-impl-unittest路径不存在,可能是CMake将测试文件生成到了${B}/src/test/目录,或者文件名与CMake定义不一致。 - 查看FooImpl的CMake配置,特别是
src/test/CMakeLists.txt,确认add_executable的目标名称,以及CMAKE_RUNTIME_OUTPUT_DIRECTORY是否指定了输出路径。
- 先检查build目录(
- 修正do_install_ptest的路径:
- 找到测试可执行文件的实际路径后,把
do_install_ptest中的${B}/test/foo-impl-unittest替换为正确路径。
- 找到测试可执行文件的实际路径后,把
- 解决Makefile找不到的问题:
- 错误中的
grep: Makefile: No such file or directory是因为ptest默认的do_install_ptest_base任务会尝试查找Makefile,但cmake生成的Makefile可能不在默认查找路径。可以在recipe里添加空实现do_install_ptest_base() {},跳过默认行为,因为你已经自定义了do_install_ptest。
- 错误中的
- 修复run-ptest的脚本问题:
- 脚本中的
$i变量未定义,会导致输出异常,可改成echo "PASS: foo-impl-unittest"直接指定测试名称。
- 脚本中的
内容的提问来源于stack exchange,提问作者bourne
相关产品推荐
相关产品推荐

