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

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

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


当前回答

我不知道有什么平台可以测试Arduino代码。

但是,有一个Fritzing平台,你可以用它来建模硬件,然后导出PCB图等。

检查的价值。

其他回答

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

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

GitHub库

为此我搭建了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本身

在没有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();
}

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

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

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

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