自定义 Python 脚本和 PyTest 的 Yocto Bitbake 配方

Yocto Bitbake Recipe for Custom Python Script and PyTest

我有一个自定义 Python Flask 应用程序,我使用 Yocto 和 Bitbake 为我的嵌入式目标构建它。 我还有一组测试用例,我 运行 在我的构建机器上针对使用 PyTest 的应用程序。如果测试失败,我希望构建失败。

我正在寻找 Yocto 方法 运行将这些测试作为自定义任务作为我的食谱的一部分。到目前为止 Google 搜索(异常地)是空的。

这是我目前已经实现的 - 它可以工作,但它需要系统 Python3 和各种 pip 安装。理想情况下,需求将在 Yocto 中构建,但仅适用于主机而非目标。我还不知道该怎么做。

# Run the pytest test cases against the app code after it is installed
python do_run_pytest_testsuite(){

    # Change to the testing directory
    import os
    testDir = "%s"%(d.getVar('WORKDIR', True))
    os.chdir(testDir)

    # Run pytest execute the test suite
    from subprocess import Popen, PIPE
    with open("%s/TestOutput.txt"%(testDir), "w") as testReportFile:
        with Popen(['/usr/bin/python3','-m','pytest','-v','tests/test_app.py'], stdout=PIPE, bufsize=1, universal_newlines=True) as proc:
            for line in proc.stdout:
                testReportFile.write(line)

    # Get the return code
    if not proc.returncode == 0:

        # Force Failure
        bb.fatal("Test Cases Failed! ( %s )"%(testDir))

}
addtask run_pytest_testsuite before do_install after do_configure

我如何在 Yocto 环境中完成这个自包含而不为目标板安装任何 PyTest 依赖项。

首先,我会查看 poky/meta/recipes-devtools/python 以了解有哪些 python 食谱可用(请注意您正在使用哪个版本)。

然后你可以添加对本机版本的食谱的依赖,例如DEPENDS = "python3-native python3-yaml-native"(或 运行 您的 python 应用程序所需的任何软件包)

然后添加一个任务运行您的应用程序

do_run_testsuite(){
  python3 ${WORKDIR}/test/test_app.py
}
addtask do_run_testsuite before do_install after do_configure

也许还要检查 openembedded python layer

如果你没有找到你需要的所有依赖,在你自己的层中添加一个 pip 包是相对容易的(只要看看提到的层中的食谱)。