我希望能够单元测试我的Arduino代码。理想情况下,我可以运行任何测试,而无需将代码上传到Arduino。哪些工具或库可以帮助我做到这一点?
目前正在开发的Arduino模拟器可能很有用,但似乎还没有准备好使用。
Atmel的AVR Studio包含一个芯片模拟器,可能很有用,但我不知道如何将它与Arduino IDE结合使用。
我希望能够单元测试我的Arduino代码。理想情况下,我可以运行任何测试,而无需将代码上传到Arduino。哪些工具或库可以帮助我做到这一点?
目前正在开发的Arduino模拟器可能很有用,但似乎还没有准备好使用。
Atmel的AVR Studio包含一个芯片模拟器,可能很有用,但我不知道如何将它与Arduino IDE结合使用。
当前回答
通过抽象出硬件访问并在测试中模拟它,我在单元测试PIC代码方面取得了相当大的成功。
例如,我用抽象PORTA
#define SetPortA(v) {PORTA = v;}
然后SetPortA可以很容易地模拟,而不需要在PIC版本中添加开销代码。
一旦硬件抽象被测试了一段时间,我很快发现代码通常会从测试平台到PIC,并且第一次就能工作。
更新:
对于单元代码,我使用#include seam,对于测试平台,在c++文件中使用#include单元代码,对于目标代码使用C文件。
作为一个例子,我想复用四个7段显示器,一个端口驱动段和第二个选择显示。显示代码通过SetSegmentData(char)和SetDisplay(char)与显示进行接口。我可以在我的c++测试平台中模拟这些,并检查我是否得到了我期望的数据。对于目标,我使用#define,这样就可以直接赋值,而不需要调用函数
#define SetSegmentData(x) {PORTA = x;}
其他回答
看起来乳香就能完美地完成这项工作。
Emulino是Greg Hewgill为Arduino平台开发的模拟器。(源)
GitHub库
有一个叫ncore的项目,它为Arduino提供了原生内核。并允许您为Arduino代码编写测试。
来自项目描述
本机核心允许您编译和运行Arduino草图 PC,一般无需修改。的原生版本 标准的Arduino功能,以及一个命令行解释器 草图的输入通常来自硬件 本身。
同样在“我需要使用它什么”部分
如果要构建测试,则需要从 http://cxxtest.tigris.org。NCORE已经用cxxtest 3.10.1进行了测试。
试试Autodesk电路模拟器。它允许用许多其他硬件组件测试Arduino代码和电路。
We are using Arduino boards for data acquisition in a large scientific experiment. Subsequently, we have to support several Arduino boards with different implementations. I wrote Python utilities to dynamically load Arduino hex images during unit testing. The code found on the link below supports Windows and Mac OS X via a configuration file. To find out where your hex images are placed by the Arduino IDE, hit the shift key before you hit the build (play) button. Hit the shift key while hitting upload to find out where your avrdude (command line upload utility) is located on your system / version of Arduino. Alternatively, you can look at the included configuration files and use your install location (currently on Arduino 0020).
http://github.com/toddstavish/Python-Arduino-Unit-Testing
如果你想在MCU之外(在桌面上)进行单元测试,请检查libcheck: https://libcheck.github.io/check/
我用它测试了几次我自己的嵌入式代码。这是一个非常健壮的框架。