Skip to content

Commit dd062fd

Browse files
authored
Merge pull request #6 from ifreecarve/2018-01-22_unittests
Unit testing support
2 parents 3555117 + 7289056 commit dd062fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1803
-235
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ vendor
1313
.rspec_status
1414

1515
# C++ stuff
16-
arduino_ci_built.bin
16+
*.bin

.rubocop.yml

+3
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,6 @@ Style/StringLiterals:
7878

7979
Style/TrailingCommaInLiteral:
8080
Enabled: false
81+
82+
Style/SymbolArray:
83+
Enabled: false

.travis.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ language: ruby
88
# - rbx
99
# - "2.5.0"
1010

11-
before_install: gem install bundler -v 1.15.4
11+
#before_install: gem install bundler -v 1.15.4
1212
script:
13+
- bundle install
1314
- bundle exec rubocop --version
1415
- bundle exec rubocop -D .
1516
- bundle exec rspec
16-
- bundle exec ci_system_check.rb
17+
- cd SampleProjects/DoSomething
18+
- bundle install
19+
- bundle exec arduino_ci_remote.rb

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
88
### Added
9+
- Unit testing support
10+
- Documentation for all Ruby methods
911
- `ArduinoInstallation` class for managing lib / executable paths
1012
- `DisplayManager` class for managing Xvfb instance if needed
1113
- `ArduinoCmd` captures and caches preferences
@@ -15,6 +17,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1517
- `ArduinoCmd` installs libraries
1618
- `ArduinoCmd` selects boards (compiler preference)
1719
- `ArduinoCmd` verifies sketches
20+
- `CppLibrary` manages GCC for unittests
21+
- `CIConfig` manages overridable config for all testing
1822

1923
### Changed
2024
- `DisplayManger.with_display` doesn't `disable` if the display was enabled prior to starting the block

README.md

+22-8
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,38 @@
77
[Arduino CI](https://github.com/ifreecarve/arduino_ci) is a Ruby gem for executing Continuous Integration (CI) tests on an Arduino library -- both locally and as part of a service like Travis CI.
88

99

10-
## Installation
10+
## Installation In Your GitHub Project And Using Travis CI
1111

12-
Add this line to your application's Gemfile:
12+
Add a file called `Gemfile` (no extension) to your Arduino project:
1313

1414
```ruby
15+
source 'https://rubygems.org'
1516
gem 'arduino_ci'
1617
```
1718

18-
And then execute:
19+
Next, you need this in `.travis.yml`
1920

20-
$ bundle
21+
```yaml
22+
sudo: false
23+
language: ruby
24+
script:
25+
- bundle install
26+
- bundle exec arduino_ci_remote.rb
27+
```
28+
29+
That's literally all there is to it on the repository side. You'll need to go to https://travis-ci.org/profile/ and enable testing for your Arduino project. Once that happens, you should be all set.
2130
22-
Or install it yourself as:
2331
24-
$ gem install arduino_ci
32+
## More Documentation
2533
34+
This software is in alpha. But [SampleProjects/DoSomething](SampleProjects/DoSomething) has a decent writeup and is a good bare-bones example of all the features.
2635
27-
## Usage
36+
## Known Problems
2837
29-
TODO: Write usage instructions here, based on other TODO of writing the actual gem.
38+
* The Arduino library is not fully mocked.
39+
* I don't have preprocessor defines for all the Arduino board flavors
40+
* Arduino Zero boards don't work in CI. I'm confused.
41+
* https://github.com/ifreecarve/arduino_ci/issues
3042
3143
3244
## Author
@@ -37,3 +49,5 @@ This gem was written by Ian Katz ([email protected]) in 2018. It's released
3749
## See Also
3850
3951
* [Contributing](CONTRIBUTING.md)
52+
* [Adafruit/travis-ci-arduino](https://github.com/adafruit/travis-ci-arduino) which inspired this project
53+
* [mmurdoch/arduinounit](https://github.com/mmurdoch/arduinounit) from which the unit test macros were adopted

SampleProjects/DoSomething/Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ PATH
22
remote: /Users/ikatz/Development/non-wayfair/arduino_ci
33
specs:
44
arduino_ci (0.0.1)
5+
os (~> 1.0)
56

67
GEM
78
remote: https://rubygems.org/
89
specs:
10+
os (1.0.0)
911

1012
PLATFORMS
1113
ruby

SampleProjects/DoSomething/README.md

+94-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Arduino CI and Unit Tests HOWTO [![Build Status](https://travis-ci.org/ifreecarve/arduino-ci-unit-tests.svg?branch=master)](https://travis-ci.org/ifreecarve/arduino-ci-unit-tests)
1+
# Arduino CI and Unit Tests HOWTO
22

33
This project is a template for a CI-enabled (and unit testable) Arduino project of your own.
44

@@ -7,17 +7,105 @@ This project is a template for a CI-enabled (and unit testable) Arduino project
77

88
* Travis CI
99
* Unit tests
10-
* Development workflow matches CI workflow - the `platformio ci` line at the bottom of `.travis.yml` can be run on your local terminal (just append the name of the file you want to compile).
10+
* Development workflow matches CI workflow
1111

1212
# Where The Magic Happens
1313

1414
Here is the minimal set of files that you will need to adapt to your own project:
1515

16-
* `.travis.yml` - You'll need to fill in the `env` section with files relevant to your project, and list out all the `--board`s under the `script` section.
17-
* `platformio.ini` - You'll need to add information for any architectures you plan to support.
18-
* `library.properties` - You'll need to update the `architectures` and `includes` lines as appropriate
16+
17+
### `Gemfile` Ruby gem configuration
18+
19+
```ruby
20+
source 'https://rubygems.org'
21+
gem 'arduino_ci'
22+
```
23+
24+
You'll need this to get access to the functionality.
25+
26+
> This is different from the `Gemfile` that's included in this directory! That's for purposes of testing the ruby gem that also lives in this repository. So "do as I say, not as I do", in this case.
27+
28+
29+
### `.travis.yml` Travis CI configuration
30+
31+
At a minimum, you will need the following lines in your file:
32+
33+
```yaml
34+
language: ruby
35+
script:
36+
- bundle install
37+
- bundle exec arduino_ci_remote.rb
38+
```
39+
40+
This will install the necessary ruby gem, and run it. There are no command line arguments as of this writing, because all configuration is provided by...
41+
42+
### `.arduino-ci.yaml` ArduinoCI configuration
43+
44+
This file controls all aspects of building and unit testing. The (relatively-complete) defaults can be found [in the base project](../../misc/default.yaml).
45+
46+
The most relevant sections for most projects will be as follows:
47+
48+
```yaml
49+
compile:
50+
libraries: ~
51+
platforms:
52+
- uno
53+
- due
54+
- leonardo
55+
56+
unittest:
57+
libraries: ~
58+
platforms:
59+
- uno
60+
- due
61+
- leonardo
62+
```
63+
64+
This does nothing but specify a list of what platforms should run each set of tests. The platforms themselves can also be defined and/or extended in the yaml file. For example, `esp8266` as shown here:
65+
66+
```yaml
67+
platforms:
68+
esp8266:
69+
board: esp8266:esp8266:huzzah
70+
package: esp8266:esp8266
71+
gcc:
72+
features:
73+
defines:
74+
warnings:
75+
flags:
76+
```
77+
78+
Of course, this wouldn't work by itself -- the Arduino IDE would have to know how to install the package via the board manager. So there's a section for packages too:
79+
80+
```yaml
81+
packages:
82+
esp8266:esp8266:
83+
url: http://arduino.esp8266.com/stable/package_esp8266com_index.json
84+
```
85+
86+
### Unit tests in `test/`
87+
88+
All `.cpp` files in the `test/` directory are assumed to contain unit tests. Each and every one will be compiled and executed on its own.
89+
90+
The most basic unit test file is as follows:
91+
92+
```C++
93+
#include <ArduinoUnitTests.h>
94+
#include "../do-something.h"
95+
96+
unittest(your_test_name)
97+
{
98+
assertEqual(4, doSomething());
99+
}
100+
101+
int main(int argc, char *argv[]) {
102+
return Test::run_and_report(argc, argv);
103+
}
104+
```
105+
106+
This test defines one `unittest` (a macro provided by `ArduionUnitTests.h`), called `your_test_name`, which makes some assertions on the target library. The `int main` section is boilerplate.
19107

20108

21109
# Credits
22110

23-
This Arduino example was created in January 2018 by Ian Katz <ianfixes@gmail.com>.
111+
This Arduino example was created in January 2018 by Ian Katz <ifreecarve@gmail.com>.

SampleProjects/DoSomething/do-something.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <Arduino.h>
2-
#include <do-something.h>
2+
#include "do-something.h"
33
int doSomething(void) {
44
millis(); // this line is only here to test that we're able to refer to the builtins
55
return 4;
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
#pragma once
12
#include <Arduino.h>
23
int doSomething(void);
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
name=arduino-ci-unit-tests
1+
name=DoSomething
22
version=0.1.0
33
author=Ian Katz <[email protected]>
44
maintainer=Ian Katz <[email protected]>
55
sentence=Arduino CI unit test example
66
paragraph=A skeleton library demonstrating CI and unit tests
77
category=Other
8-
url=https://github.com/ifreecarve/arduino-ci-unit-tests
8+
url=https://github.com/ifreecarve/arduino_ci/SampleProjects/DoSomething
99
architectures=avr
1010
includes=do-something.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <ArduinoUnitTests.h>
2+
#include "../do-something.h"
3+
4+
unittest(library_does_something)
5+
{
6+
assertEqual(4, doSomething());
7+
}
8+
9+
int main(int argc, char *argv[]) {
10+
return Test::run_and_report(argc, argv);
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <ArduinoUnitTests.h>
2+
3+
unittest(equality_as_vars)
4+
{
5+
int x = 3;
6+
int y = 3;
7+
int z = 3;
8+
assertEqual(x, y);
9+
assertEqual(x, z);
10+
}
11+
12+
unittest(equality_as_values)
13+
{
14+
assertEqual(1, 1);
15+
assertEqual(4, 4);
16+
}
17+
18+
unittest(nothing)
19+
{
20+
}
21+
22+
int main(int argc, char *argv[]) {
23+
return Test::run_and_report(argc, argv);
24+
}

SampleProjects/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Arduino Sample Projects
22
=======================
33

4-
This directory contains example projects that are meant to be built with this gem.
4+
This directory contains example projects that are meant to be built with this gem. Except "TestSomething". That one's just a beater for CI testing and includes some tests that are designed to fail (to test negative inputs).

SampleProjects/TestSomething/Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source 'https://rubygems.org'
2+
gem 'arduino_ci', path: '../../'
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
PATH
2+
remote: /Users/ikatz/Development/non-wayfair/arduino_ci
3+
specs:
4+
arduino_ci (0.0.1)
5+
os (~> 1.0)
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
os (1.0.0)
11+
12+
PLATFORMS
13+
ruby
14+
15+
DEPENDENCIES
16+
arduino_ci!
17+
18+
BUNDLED WITH
19+
1.16.0
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# TestSomething
2+
3+
This is a "beater" example that is referenced by tests of the Arduino CI module itself.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <test-something.h>
2+
// if it seems bare, that's because it's only meant to
3+
// demonstrate compilation -- that references work
4+
void setup() {
5+
}
6+
7+
void loop() {
8+
testSomething();
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=TestSomething
2+
version=0.1.0
3+
author=Ian Katz <[email protected]>
4+
maintainer=Ian Katz <[email protected]>
5+
sentence=Arduino CI unit test example
6+
paragraph=A skeleton library demonstrating CI and unit tests
7+
category=Other
8+
url=https://github.com/ifreecarve/arduino_ci/SampleProjects/TestSomething
9+
architectures=avr,esp8266
10+
includes=do-something.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "test-something.h"
2+
int testSomething(void) {
3+
millis(); // this line is only here to test that we're able to refer to the builtins
4+
return 4;
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
#include <Arduino.h>
3+
int testSomething(void);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <ArduinoUnitTests.h>
2+
3+
unittest(pretend_equal_things_arent)
4+
{
5+
int x = 3;
6+
int y = 3;
7+
assertNotEqual(x, y);
8+
}
9+
10+
int main(int argc, char *argv[]) {
11+
return Test::run_and_report(argc, argv);
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <ArduinoUnitTests.h>
2+
#include "../test-something.h"
3+
4+
unittest(library_tests_something)
5+
{
6+
assertEqual(4, testSomething());
7+
}
8+
9+
int main(int argc, char *argv[]) {
10+
return Test::run_and_report(argc, argv);
11+
}

0 commit comments

Comments
 (0)