KUnit
基本介绍
KUnit Suite Memory
每一个kUnit都有自己的上下文,所以一个崩溃了,不会影响到其他的suit。
开关
目前可以有下面几种方法控制case测试。
menuconfig控制
使用.kunitconfig控制编译的测试文件
使用宏选择性开启某些case
使用debugfs
kunit_get_current_test
总开关
CONFIG_KUNIT=y
CONFIG_KUNIT_DEFAULT_ENABLED=y
CONFIG_KUNIT: 将KUNIT框架编入内核。
CONFIG_KUNIT_DEFAULT_ENABLED:开启测试,如果是n会关闭测试。
测试文件开关
使用menuconfig
通过在Kconfig中配置测试文件的构建选项来控制是否加入测试。
例如,有Kunit测试文件 kunit-example-test.c , 可以通过在Kconfig中加入 CONFIG_KUNIT_EXAMPLE_TEST选项可以是y/m/n,并且在Makefile中添加:
如果设置成m就会编译出.ko的驱动文件。在挂载的时候进行测试。
使用.kunitconfig
.kunitconfig是 a minconfig(通过运行生成的 .config ),用于运行一组特定的测试。如果想运行一组特定的测试,可以在.kunitconfig中提供Kconfig选项,以及所依赖的其他配置选项(架构配置、启动/禁用默写代码块配置、依赖项)。其根本也是使用Kconfig用来配置构建选项,具体可以见这里.
使用debugfs
可以通过配置 CONFIG_KUNIT_DEBUGFS=y ,开启Kunit提供的debugfs的功能。Kunit会在 /sys/kernel/debug/kunit/
cat results
就可以输出测试结果
echo anything > run
可以再次运行测试并输出测试结果
测试函数开关
使用宏来选择性开启case
可以在代码段中通过宏条件编译的方式选择性开启case。或者如果不想将测试的函数或者变量导出给测试文件使用,也可以使用下面的方法
kunit_get_current_test()
当需要用测试文件外部调用仅测试代码时,可以使用 kunit_get_current_test 。该函数可以通过task_struct中kunit_test来分析出当前的kunit上下文,并且如果Kunit没有启用,该函数返回null,也可以安全调用,并且对性能的影响忽略不计。所以可以通过这个特性,在待测函数中植入kunit测试,通过启用Kunit测试当做开关。例如:
使用kunit提供的一些状态函数
kunit_skip:跳过当前test
一些测试模板
最简测试模板
driver需要fake device
可以使用 kunit_device_register 来创建一个fake device,当测试结束时会自动清理。同时在init中使用test→priv 传递device到各个cases中。
从输出log信息中处理得到测试用例信息
Just set CONFIG_KUNIT=y
CONFIG_KUNIT_DEBUGFS=y
CONFIG_PINCTRL_MSM_TEST=y
, then build the kernel image and flash it to device. During kernel boot process, will receive some information similar to the following.
[ 1.591001] KTAP version 1
[ 1.593870] 1..3
[ 1.597682] KTAP version 1
[ 1.600959] # Subtest: device-dts-test
[ 1.605408] # module: pinctrl_msm_test
[ 1.605428] 1..1
[ 1.622358] ok 1 get_correct_dts_test
[ 1.622390] ok 1 device-dts-test
[ 1.630229] KTAP version 1
[ 1.633413] # Subtest: pinmux_set_test
[ 1.637678] # module: pinctrl_msm_test
[ 1.637682] 1..2
[ 1.648330] ok 1 request_one_pin_test
[ 1.679433] ok 2 double_request_fail_test
[ 1.679528] # pinmux_set_test: pass:2 fail:0 skip:0 total:2
[ 1.684103] # Totals: pass:2 fail:0 skip:0 total:2
[ 1.689900] ok 2 pinmux_set_test
[ 1.698443] KTAP version 1
[ 1.701654] # Subtest: gpio_set_test
[ 1.705777] # module: pinctrl_msm_test
[ 1.705781] 1..1
[ 1.716401] ok 1 gpio_set_value_test
[ 1.716597] ok 3 gpio_set_test
Then, paste these messages into a file and use the KUnit tool to parse them.
./tools/testing/kunit/kunit.py parse <message file>
We can obtain the output as follows.
[21:30:57] ============================================================
[21:30:57] ============== device-dts-test (1 subtest) ===============
[21:30:57] [PASSED] get_correct_dts_test
[21:30:57] ================ [PASSED] device-dts-test ================
[21:30:57] =============== pinmux_set_test (2 subtests) ===============
[21:30:57] [PASSED] request_one_pin_test
[21:30:57] [PASSED] double_request_fail_test
[21:30:57] ================= [PASSED] pinmux_set_test =================
[21:30:57] ================ gpio_set_test (1 subtest) =================
[21:30:57] [PASSED] gpio_set_value_test
[21:30:57] ================== [PASSED] gpio_set_test ==================
[21:30:57] ============================================================
[21:30:57] Testing complete. Ran 4 tests: passed: 4
Due to CONFIG_KUNIT_DEBUGFS=y, we can also find two files, /sys/kernel/debug/kunit/<suite>/results
and /sys/kernel/debug/kunit/<suite>/run
. Running cat /sys/kernel/debug/kunit/<suite>/results
will display the output.
__EOF__

本文链接:https://www.cnblogs.com/alanli07/p/18399622.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程