我希望能够单元测试我的Arduino代码。理想情况下,我可以运行任何测试,而无需将代码上传到Arduino。哪些工具或库可以帮助我做到这一点?

目前正在开发的Arduino模拟器可能很有用,但似乎还没有准备好使用。

Atmel的AVR Studio包含一个芯片模拟器,可能很有用,但我不知道如何将它与Arduino IDE结合使用。


当前回答

simavr是一个使用AVR -gcc的AVR模拟器。

它已经支持一些ATTiny和ATMega微控制器,而且根据作者的说法,很容易添加更多的微控制器。

示例中包含simduino,这是一个Arduino模拟器。它支持运行Arduino引导加载程序,并可以通过Socat(修改后的Netcat)使用avrdude进行编程。

其他回答

看起来乳香就能完美地完成这项工作。

Emulino是Greg Hewgill为Arduino平台开发的模拟器。(源)

GitHub库

在没有Arduino单元测试框架的情况下,我创建了ArduinoUnit。下面是一个简单的Arduino草图,展示了它的使用:

#include <ArduinoUnit.h>

// Create test suite
TestSuite suite;

void setup() {
    Serial.begin(9600);    
}

// Create a test called 'addition' in the test suite
test(addition) {
    assertEquals(3, 1 + 2);
}

void loop() {
    // Run test suite, printing results to the serial port
    suite.run();
}

有一个叫ncore的项目,它为Arduino提供了原生内核。并允许您为Arduino代码编写测试。

来自项目描述

本机核心允许您编译和运行Arduino草图 PC,一般无需修改。的原生版本 标准的Arduino功能,以及一个命令行解释器 草图的输入通常来自硬件 本身。

同样在“我需要使用它什么”部分

如果要构建测试,则需要从 http://cxxtest.tigris.org。NCORE已经用cxxtest 3.10.1进行了测试。

你可以在我的项目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是用C和c++编写的,甚至Arduino的库也是用C和c++编写的。因此,简单地说,只需将代码处理为C和c++,并尝试进行单元测试。这里,通过“句柄”这个词,我的意思是你改变所有的基本语法,如串行。Println到sysout, pinmode到变量,void循环到while()循环,该循环在keystock或某些迭代后中断。

我知道这是一个漫长的过程,不那么直接。根据我个人的经验,一旦你开始使用它,它就会变得更可靠。

-Nandha_Frost