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

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

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


当前回答

如果你想在MCU之外(在桌面上)进行单元测试,请检查libcheck: https://libcheck.github.io/check/

我用它测试了几次我自己的嵌入式代码。这是一个非常健壮的框架。

其他回答

使用带有Arduino库的Proteus VSM来调试代码或测试它。

这是在将代码安装到板上之前的最佳实践,但要确保计时,因为模拟不会在板上运行时实时运行。

试试Autodesk电路模拟器。它允许用许多其他硬件组件测试Arduino代码和电路。

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

如果您有兴趣运行INO草图并检查串行输出,我在我的Arduino NMEA校验和项目中有一个工作实现。

下面的脚本获取该文件,并使用Arduino CLI将其编译为HEX文件,然后加载到SimAVR中,SimAVR对其进行计算并打印串行输出。由于所有Arduino程序都永远运行,没有真正杀死自己的选项(exit(0)不起作用),我让草图运行几秒钟,然后将捕获的输出与预期的输出进行区分。

下载并提取Arduino CLI(在本例中,版本为0.5.0 -撰写本文时的最新版本):

curl -L https://github.com/arduino/arduino-cli/releases/download/0.5.0/arduino-cli_0.5.0_Linux_64bit.tar.gz -o arduino-cli.tar.gz
tar -xvzf arduino-cli.tar.gz

现在你可以更新索引并安装相应的核心:

./arduino-cli core update-index
./arduino-cli core install arduino:avr

假设你的草图名为nmea-checksum。ino,获取ELF和HEX,运行:

./arduino-cli compile -b arduino:avr:uno nmea-checksum.ino

接下来,SimAVR运行HEX(或ELF) -我从源代码构建,因为最新版本不适合我:

sudo apt-get update
sudo apt-get install -y build-essential libelf-dev avr-libc gcc-avr freeglut3-dev libncurses5-dev pkg-config
git clone https://github.com/buserror/simavr.git
cd simavr
make

成功的编译将为您提供simavr/run_avr,您可以使用它来运行草图。就像我说的,暂停它,否则它永远不会终止:

cd simavr
timeout 10 ./run_avr -m atmega168 -f 16000000 ../../nmea-checksum.ino.arduino.avr.uno.elf &> nmea-checksum.ino.clog || true

生成的文件将有ANSI颜色代码控制字符包装串行输出,以摆脱这些:

cat nmea-checksum.ino.clog | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" > nmea-checksum.ino.log
cat nmea-checksum.ino.log

现在你所需要做的就是将这个文件与一个已知的好文件进行比较:

diff nmea-checksum.ino.log ../../nmea-checksum.ino.test

如果没有差异,diff将以代码0退出,否则脚本将失败。

保持特定于硬件的代码与其他代码分离或抽象,这样您就可以在任何拥有良好工具和最熟悉的平台上测试和调试更大的“休息”。

Basically, try to build as much of the final code from as many known-to-work building blocks as possible. The remaining hardware-specific work will then be much easier and faster. You may finish it by using existing emulators and/or emulating devices on your own. And then, of course, you'll need to test the real thing somehow. Depending on circumstances, that may or may not be very well automatable (i.e. who or what will press buttons and provide other inputs? who or what will observe and interpret various indicators and outputs?).