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

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

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


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

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

检查的价值。


通过抽象出硬件访问并在测试中模拟它,我在单元测试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;}

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

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

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

GitHub库


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

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

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


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


James W. Grenning写了一本很棒的书,这本书是关于嵌入式C代码的单元测试的。


你可以在我的项目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

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

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?).


你可以使用仿真——你可以在图表上拖放微控制器,然后在Eclipse中运行你的代码。网站上的文档会告诉你如何设置。


不要在Arduino设备或模拟器上运行单元测试

案例针对微控制器设备/模拟器/基于sim的测试

关于单元测试的含义有很多讨论,但我不是 我想在这里讨论一下。这篇文章不是 告诉你不要对你的终极目标进行任何实际测试 硬件。我想说的是关于优化你的 通过消除目标硬件来实现开发反馈周期 你最平常最频繁的测试被测单元是假定的 要比整个项目小得多。

单元测试的目的是测试你自己代码的质量。单元测试通常不应该测试超出您控制范围的因素的功能。

可以这样想:即使你要测试Arduino库、微控制器硬件或模拟器的功能,这些测试结果也绝对不可能告诉你自己工作的质量。因此,编写不在目标设备(或模拟器)上运行的单元测试更有价值,也更有效。

在目标硬件上频繁测试的周期非常缓慢:

调整代码 编译并上传到Arduino设备 观察行为并猜测您的代码是否正在执行您所期望的操作 重复

如果你希望通过串口获得诊断信息,但你的项目本身需要使用Arduino唯一的硬件串口,那么步骤3就特别麻烦了。如果您认为SoftwareSerial库可能会有所帮助,那么您应该知道这样做可能会破坏任何需要精确计时的功能,例如同时生成其他信号。我遇到过这样的问题。

同样,如果你要用模拟器测试你的草图,你的时间紧迫的例程在你上传到实际的Arduino之前运行得很好,那么你唯一要学到的教训就是模拟器有缺陷——即使知道这一点,也不能说明你自己的工作质量。

如果在设备或模拟器上进行测试很愚蠢,我该怎么做?

你可能正在用电脑做你的Arduino项目。那台计算机比微控制器快几个数量级。编写要在计算机上构建和运行的测试。

请记住,应该假设Arduino库和微控制器的行为是正确的或至少始终不正确的。

When your tests produce output contrary to your expectations, then you likely have a flaw in your code that was tested. If your test output matches your expectations, but the program does not behave correctly when you upload it to the Arduino, then you know that your tests were based on incorrect assumptions and you likely have a flawed test. In either case, you will have been given real insights on what your next code changes should be. The quality of your feedback is improved from "something is broken" to "this specific code is broken".

如何在PC上构建和运行测试

您需要做的第一件事是确定您的测试目标。考虑一下您想要测试自己代码的哪些部分,然后确保以这样一种方式构建您的程序,即您可以隔离用于测试的离散部分。

如果要测试的部件调用任何Arduino功能,则需要在测试程序中提供模型替换。这比看上去要简单得多。您的模型实际上不需要做任何事情,只需要为您的测试提供可预测的输入和输出。

您打算测试的任何自己的代码都需要存在于.pde草图之外的源文件中。不要担心,即使使用草图之外的一些源代码,您的草图仍然可以编译。当您真正开始着手时,在草图文件中应该只定义程序的正常入口点。

剩下的就是编写实际的测试,然后使用您最喜欢的c++编译器编译它!这也许可以用一个真实的例子来最好地说明。

一个实际的工作示例

我在这里发现的一个喜欢的项目有一些在PC上运行的简单测试。对于这个答案提交,我将回顾一下我如何模拟Arduino库的一些函数,以及我为测试这些模型而编写的测试。这与我之前所说的不要测试其他人的代码并不矛盾,因为我是编写模型的人。我想非常确定我的模型是正确的。

mock_arduino.cpp的源代码,其中包含复制Arduino库提供的一些支持功能的代码:

#include <sys/timeb.h>
#include "mock_arduino.h"

timeb t_start;
unsigned long millis() {
  timeb t_now;
  ftime(&t_now);
  return (t_now.time  - t_start.time) * 1000 + (t_now.millitm - t_start.millitm);
}

void delay( unsigned long ms ) {
  unsigned long start = millis();
  while(millis() - start < ms){}
}

void initialize_mock_arduino() {
  ftime(&t_start);
}

当我的代码将二进制数据写入硬件串行设备时,我使用下面的模拟生成可读的输出。

fake_serial.h

#include <iostream>

class FakeSerial {
public:
  void begin(unsigned long);
  void end();
  size_t write(const unsigned char*, size_t);
};

extern FakeSerial Serial;

fake_serial.cpp

#include <cstring>
#include <iostream>
#include <iomanip>

#include "fake_serial.h"

void FakeSerial::begin(unsigned long speed) {
  return;
}

void FakeSerial::end() {
  return;
}

size_t FakeSerial::write( const unsigned char buf[], size_t size ) {
  using namespace std;
  ios_base::fmtflags oldFlags = cout.flags();
  streamsize oldPrec = cout.precision();
  char oldFill = cout.fill();

  cout << "Serial::write: ";
  cout << internal << setfill('0');

  for( unsigned int i = 0; i < size; i++ ){
    cout << setw(2) << hex << (unsigned int)buf[i] << " ";
  }
  cout << endl;

  cout.flags(oldFlags);
  cout.precision(oldPrec);
  cout.fill(oldFill);

  return size;
}

FakeSerial Serial;

最后是实际的测试程序:

#include "mock_arduino.h"

using namespace std;

void millis_test() {
  unsigned long start = millis();
  cout << "millis() test start: " << start << endl;
  while( millis() - start < 10000 ) {
    cout << millis() << endl;
    sleep(1);
  }
  unsigned long end = millis();
  cout << "End of test - duration: " << end - start << "ms" << endl;
}

void delay_test() {
  unsigned long start = millis();
  cout << "delay() test start: " << start << endl;
  while( millis() - start < 10000 ) {
    cout << millis() << endl;
    delay(250);
  }
  unsigned long end = millis();
  cout << "End of test - duration: " << end - start << "ms" << endl;
}

void run_tests() {
  millis_test();
  delay_test();
}

int main(int argc, char **argv){
  initialize_mock_arduino();
  run_tests();
}

这篇文章已经足够长了,所以请参考我在GitHub上的项目来查看更多的测试用例。我将正在进行的工作保存在master以外的分支中,因此也要检查这些分支以获得额外的测试。

我选择编写自己的轻量级测试例程,但也有更健壮的单元测试框架,如CppUnit。


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

来自项目描述

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

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

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


该程序允许自动运行几个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.


我在编写Arduino代码时使用Searduino。Searduino是Arduino模拟器和开发环境(Makefiles, C代码…),可以使用您最喜欢的编辑器在C/ c++中轻松破解。您可以导入Arduino草图并在模拟器中运行它们。

Searduino 0.8截图:http://searduino.files.wordpress.com/2014/01/jearduino-0-8.png

Searduino 0.9将发布,并在最后的测试完成后立即录制视频....一两天后。

在模拟器上测试并不是真正的测试,但它确实帮助我找到了许多愚蠢的/逻辑错误(忘记执行pinMode(xx, OUTPUT)等)。

顺便说一句:我是Searduino开发人员之一。


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


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

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


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

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


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

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

-Nandha_Frost


如果您有兴趣运行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退出,否则脚本将失败。