1
完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
嗨,我正在编写单元测试,现在我需要模拟一个硬件调用的函数。代替原来的“Foo”函数,我想调用一个不同的函数“bar”,因此,在我的测试函数中,如果函数“FO”被调用,我可以检查一个标志。我不想仅仅为了测试目的而修改我的生产代码。所以,使用弱属性是毫无疑问的。我遇到链接器选项“--FrIP Fo”及其编译器选项“Wl,-Calp= FoO”。但这不起作用。我的模拟函数从不被调用。我所做的:在生产代码(FoBa.c)中:在我的测试代码(Test-FooBar .c)中:然后我重写了FooBar .c的构建选项,并将“-Wl,-Calp= FoO”添加到XC32 GCC选项的“附加选项”中。有人对此有过经验吗?我做错什么了?
以上来自于百度翻译 以下为原文 Hi, I am writing Unit-Tests and now I need to mock a function that does a hardware-call. Instead of the original "foo" function, I want to call a different function "bar" so, in my test function, I can check a flag if the function "foo" would have been called. I do not want to modify my productive code solely for testing purposes! So, using the weak attribute is out of question. I came across the linker option "--wrap foo" and its compiler option "-Wl,--wrap=foo". But this does not work. My mock function is never called. What I did: In the productive code (foobar.c): void foo(int bar) { return 1; } In my test code (test_foobar.c): void __wrap_foo(int bar) { return -1; } Then I have overridden the build options for foobar.c and added "-Wl,--wrap=foo" to the "Additional options" of xc32-gcc options. Has someone experience with this? What am I doing wrong? |
|
相关推荐
11个回答
|
|
为什么要先链接原始的FoE()而不是模拟呢?
以上来自于百度翻译 以下为原文 Why are you linking the original foo() instead of the mock in the first place? |
|
|
|
你的意思是只包含包含FoW()的C文件?这在某些情况下是可能的,是的。在其他情况下,我需要来自同一文件的一些函数。拆分代码是可能的,但是如果可能的话,我想避免它。或者有一种方法可以从一个.c文件中排除特定的函数(带有编译器/链接标志)吗?
以上来自于百度翻译 以下为原文 You mean just excluding the c-file containing foo()? Thats possible in some cases, yes. In other cases, I need some functions from the same file. Splitting the code could be possible, but I want to avoid it if possible. Or is there a way to exclude specific functions from a .c file (with a compiler/linker flag)? |
|
|
|
将特定于平台的代码分割成单独的编译单元是无论如何都要做的明智之举。至少有一些模拟框架允许你在每一个单元测试可执行的基础上选择真实的和模拟的实现,也考虑你想要测试的,以及在设备上而不是在桌面上运行测试是多么的重要。对于绝大多数来说,它根本不重要,在PC上运行更有效,更容易在CI环境中实现。
以上来自于百度翻译 以下为原文 Splitting the platform-specific code into separate compilation units is the sane thing to do anyways. At least some mocking frameworks allow you to pick between the real and mock implementations on a per unit test executable basis. Also consider what you want to test, and how critical it is to run the tests on the device rather than on the desktop. For the vast majority it should not matter at all, and running on a PC is vastly more efficient and easier to implement in a CI environment. |
|
|
|
我同意你的看法。我说,我不能在桌面上测试我的程序,因为我使用FreeRTOS。微芯片模拟器不支持FreeRTOS。使用一个不同的编译器编译x86的所有东西是毫无疑问的,因为在不同的PultFrm上成功的测试对目标PaltFrm没有任何说明。但是回到最初的问题:我遇到了CMOCKA——但是CMOCKA确实做了对我不起作用的事情——它使用“-Wl,-Calp…”
以上来自于百度翻译 以下为原文 I agree with you. That said, I cannot test my program (completely) on the desktop, since I use FreeRTOS. And the microchip simulator does not support FreeRTOS. Using a different compiler to compile everything for x86 is out of question, since a successful test on a different plattform says nothing about the target plattform. But back to the original question: I came across cmocka - but cmocka does exactly what is not working for me - it uses "-Wl,--wrap ..." |
|
|
|
由于它是链接器选项,所以我认为您应该去Project属性并将此选项添加到XC32 LD & GT;另外的驱动程序选项。
以上来自于百度翻译 以下为原文 Since it is a linker option, I'd say you should go to Project Properties and add this option to xc32-ld -> Additional driver options. |
|
|
|
一点也不!如果你的逻辑在嵌入式平台上被破坏了,它也会在桌面上被破坏。几乎所有你可以进行单元测试的,都可以在目标之外完美地测试。
以上来自于百度翻译 以下为原文 Not at all! If your logic is broken on the embedded platform, it will be broken on the desktop as well. Almost everything you can unit test, can be perfectly well tested outside the target. |
|
|
|
你能“定义”Sy-SmithHWYFuxUnITy测试“,然后在硬件交互周围有一些IF I/I IFDEF测试吗?我以前做过合理的成功,但我也看到在某些情况下,这会变成不可读取的代码。
以上来自于百度翻译 以下为原文 Can you "#define __SIM_HW_FOR_UNIT_TEST" and then have some #if/#ifdef tests around the hardware interaction? I've done that before with reasonable success, but I've also seen that blow up into unreadable code in some instances. |
|
|
|
这就是为什么我不想这样做的原因。(通常)我不希望在我的生产代码中只包含一行代码,仅仅是为了测试。如果使用定义,改变了你的生产代码的工作方式,你就可以保证在发布版本时有人会设置错误的定义。我希望我能那么容易。至少它不起作用,这对于单个函数的单元测试是正确的。逻辑不应受到影响。但是,当您进行组件测试时,时间和优先级是重要的,您不能信任在桌面上执行的测试。但是,在第一步中,您可以在桌面上进行测试并稍后在平台上验证。STS。现在仍然是可能的,谢谢大家!但是如果有人发现为什么包装的东西不起作用,请写一篇文章。
以上来自于百度翻译 以下为原文 Thats why I do not want to do that. (Usually) I do not want even one line of code within my productive code that is solely for testing. Also If you use defines, that change the way your productive code works, you are guaranteed that someone will set the wrong define when building a release build. Nope, I wish I would be that easy. At least it did not work. Thats is true for Unit-Testing of single functions. The logic should not be affected. But as soon as you do component testing, where timings and priorities are important, you cannot trust a test that was executed on desktop. But true, in a first step, you can test on desktop and validate on the platform later. I think I will try to not-compile the code in question when building the tests. Currently thats still possible. Thanks to all! But if someone comes across why the --wrap thing does not work, please write a post. |
|
|
|
也许这是有用的HTTP//www. McCHIP.COM/FUMMS/FUNDSPE/101674
以上来自于百度翻译 以下为原文 Maybe this is of use http://www.microchip.com/forums/FindPost/1016744 |
|
|
|
不,我希望我能那么容易。至少它不起作用…这对我来说是用MPLABX模拟(和吉姆的演示代码一样):
以上来自于百度翻译 以下为原文 Nope, I wish I would be that easy. At least it did not work. ... This works for me using MPLABX simulation (same as Jim's demo code): #include #include void foo(void); // Real foo() is defined in a different .c file void __wrap_foo(void) { printf("__wrap_foo is calledn"); } int main(void) { foo(); // Prints "__wrap_foo is called" while (1) { } return (0); } void _on_bootstrap(void) { U1MODEbits.UARTEN = 1; U1STAbits.UTXEN = 1; } void _mon_putc(char c) { while (U1STAbits.UTXBF); U1TXREG = c; while (U1STAbits.TRMT != 1); } |
|
|
|
我很高兴地告诉你,我错了,你是对的。既然你们两个都确认它应该像所描述的那样工作,我就再试一次。现在它起作用了……我不知道我第一次做错了什么。我以前确实尝试过,我可以发誓我做的和现在一样…我只在C文件中使用了-WARP选项,使用了所讨论的函数。那不管用!“包裹”选项必须在项目的链接器选项中定义(至少这是可行的)。可能是我只尝试了编译器GLULALY的-WL选项,并且只使用了C文件选项中的链接器的-Repe选项…非常感谢!谢谢你和我呆在一起:
以上来自于百度翻译 以下为原文 I am happy to inform you that I was wrong and you were right. Since you both confirmed that it should work as described, I tried it again. And now it works... I have no idea what I did wrong the first time. I definitively tried it before, and I could swear that I did it exactly as now... I experimented with using the --warp option only on the c-file the function in question was used. That does not work! The --wrap option has to be defined in the linker options of the project (at least thats what works). Could be that I only tried the -Wl option of the compiler globaly and used the --wrap option of the linker only in the c-file options... Thanks a lot! And thanks for staying with me :) |
|
|
|
只有小组成员才能发言,加入小组>>
5238 浏览 9 评论
2028 浏览 8 评论
1950 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3204 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2253 浏览 5 评论
774浏览 1评论
664浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
592浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
674浏览 0评论
574浏览 0评论
小黑屋| 手机版| Archiver| 德赢Vwin官网 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-22 16:59 , Processed in 1.499380 second(s), Total 96, Slave 80 queries .
Powered by 德赢Vwin官网 网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
德赢Vwin官网 观察
版权所有 © 湖南华秋数字科技有限公司
德赢Vwin官网 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号