我希望能够单元测试我的Arduino代码。理想情况下,我可以运行任何测试,而无需将代码上传到Arduino。哪些工具或库可以帮助我做到这一点?
目前正在开发的Arduino模拟器可能很有用,但似乎还没有准备好使用。
Atmel的AVR Studio包含一个芯片模拟器,可能很有用,但我不知道如何将它与Arduino IDE结合使用。
我希望能够单元测试我的Arduino代码。理想情况下,我可以运行任何测试,而无需将代码上传到Arduino。哪些工具或库可以帮助我做到这一点?
目前正在开发的Arduino模拟器可能很有用,但似乎还没有准备好使用。
Atmel的AVR Studio包含一个芯片模拟器,可能很有用,但我不知道如何将它与Arduino IDE结合使用。
当前回答
该程序允许自动运行几个Arduino单元测试。测试过程在PC上启动,但测试在实际的Arduino硬件上运行。一组单元测试通常用于测试一个Arduino库。 (这
Arduino论坛:http://arduino.cc/forum/index.php?topic=140027.0
GitHub项目页面:http://jeroendoggen.github.com/Arduino-TestSuite
Python包索引中的页面:http://pypi.python.org/pypi/arduino_testsuite
单元测试是用“Arduino单元测试库”编写的:http://code.google.com/p/arduinounit
对每组单元测试执行以下步骤:
Read the config file to find out which tests to run The script compiles and uploads an Arduino sketch that contains the unit testing code. The unit tests are run on the Arduino board. The results of the test are printed over the serial port and analyzed by the Python script. The script starts the next test, repeating the above steps for all test that are requested in the configuration file. The script prints a summary showing an overview of all the failed/passed tests in the complete testsuite.
其他回答
你可以在我的项目PySimAVR中使用Python进行单元测试。Arscons用于构建,simavr用于模拟。
例子:
from pysimavr.sim import ArduinoSim
def test_atmega88():
mcu = 'atmega88'
snippet = 'Serial.print("hello");'
output = ArduinoSim(snippet=snippet, mcu=mcu, timespan=0.01).get_serial()
assert output == 'hello'
开始测试:
$ nosetests pysimavr/examples/test_example.py
pysimavr.examples.test_example.test_atmega88 ... ok
为此我搭建了arduino_ci。虽然它仅限于测试Arduino库(而不是独立的草图),但它可以在本地或CI系统(如Travis CI或Appveyor)上运行单元测试。
考虑在Arduino库目录中有一个非常简单的库,名为DoSomething,带有do-something.cpp:
#include <Arduino.h>
#include "do-something.h"
int doSomething(void) {
return 4;
};
你可以像下面这样对它进行单元测试(使用一个名为test/ is_4 .cpp的测试文件):
#include <ArduinoUnitTests.h>
#include "../do-something.h"
unittest(library_does_something)
{
assertEqual(4, doSomething());
}
unittest_main() // this is a macro for main(). just go with it.
这是所有。如果assertEqual语法和测试结构看起来很熟悉,那是因为我采用了Matthew Murdoch的ArduinoUnit库 他在回答中提到了。
见参考。关于单元测试I/O引脚,时钟,串行端口等的更多信息。
这些单元测试是使用ruby gem中包含的脚本编译和运行的。有关如何设置的示例,请参阅README。或者从这些例子中复制一个:
一个测试Queue实现的实际示例 另一个Queue项目上的另一组测试 这是一个复杂的例子,模拟了一个通过SoftwareSerial连接控制交互设备的库,作为Adafruit FONA库的一部分 上面显示的DoSomething示例库,用于测试arduino_ci本身
James W. Grenning写了一本很棒的书,这本书是关于嵌入式C代码的单元测试的。
simavr是一个使用AVR -gcc的AVR模拟器。
它已经支持一些ATTiny和ATMega微控制器,而且根据作者的说法,很容易添加更多的微控制器。
示例中包含simduino,这是一个Arduino模拟器。它支持运行Arduino引导加载程序,并可以通过Socat(修改后的Netcat)使用avrdude进行编程。
看起来乳香就能完美地完成这项工作。
Emulino是Greg Hewgill为Arduino平台开发的模拟器。(源)
GitHub库