Skip to content
Permalink
Browse files
added links to CI examples
  • Loading branch information
Mark Tyers committed Mar 17, 2018
1 parent a70d34e commit 9b89d29bc384f98cfd9a6993dd5504368f512be9
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
@@ -25,6 +25,13 @@ You will need to create a `.gitlab-ci.yml` file in the root directory of your pr

You will also need to configure GitLab to only allow merging if all tests have passed. In this way you can bypass the requirement for the code to be reviewed by the Scrum Master which should speed up the process of integrating code into the Master branch.

### 1.1 Examples

To help you configure your GitLab CI pipeline there are a couple of sample repositories you should be studying which cover most of the platforms you are developing for:

1. [ci-arduino](https://gitlab.com/covcom/ci-arduino) shows how to build a CI pipeline using the [PlatformIO](https://platformio.org) tools.
2. [ci-nodejs](https://gitlab.com/covcom/continuous-integration-example) shows how to build a CI and CD pipeline for a NodeJS API.

### 1.1 Continuous Integration and Arduino Code

The challenge for carrying out unit and integration tests for Arduino code is that it has to run on a server rather than on a physical microcontroller. There are links to useful web resources in the `exercises/08_ci/arduino/` directory.
@@ -15,6 +15,11 @@ Once installed you will need to restart VS Code. After restarting you will see t
6. Test
7. Run task
8. Serial monitor
9. New terminal

## 1 Creating a PlatformIO Account

In order to complete this tutorial you will need to log in with a [**PlatformIO** account](https://community.platformio.org). Yu will be sent an activation email.

Locate the **New Project** button on the **PIO Home** screen and click it to launch the wizard.

@@ -25,7 +30,7 @@ You should give your project a title (Blink). For the board use the dropdown lis
If you open the explorer tab in VSCode (the first icon down the left of the editor) you can see that PlatformIO has created a series of directories and files:

```
.
project_dir
├── lib
│ └── readme.txt
├── platformio.ini
@@ -58,3 +63,98 @@ void loop() {
Plug in your ESP8266, click on the **Build** button. Once the code is compiled and linked click on the **Upload** button to upload the hex file to the microcontroller. This should result in the onboard LED flashing on and off.

## 2 Unit Testing

Note that this feature requires a [paid account](https://platformio.org/pricing). The cheapest option is the **Basic Non-Commercial** licence. Your first month is free.

Now we will write some unit tests for our program. Start by creating a new directory called `test/` and create a file in it called `test_main.cpp`. Your project directories and files should look soemthing like the following:

```
project_dir
├── lib
│ └── readme.txt
├── platformio.ini
├── src
│ └── main.cpp
└── test
└── test_main.cpp
```

Edit the `main.cpp` file and add the two lines as shown.

```cpp
#include "Arduino.h"
#ifndef UNIT_TEST // IMPORTANT LINE!
#define LED_BUILTIN 2
void setup() {
// initialize LED digital pin as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
#endif // IMPORTANT LINE!
```

Now add the following code to the new test file.

```cpp
#include <Arduino.h>
#include <unity.h>
#ifdef UNIT_TEST
void test_led_builtin_pin_number(void) {
TEST_ASSERT_EQUAL(LED_BUILTIN, 2);
}
void test_led_state_high(void) {
digitalWrite(LED_BUILTIN, HIGH);
TEST_ASSERT_EQUAL(digitalRead(LED_BUILTIN), HIGH);
}
void test_led_state_low(void) {
digitalWrite(LED_BUILTIN, LOW);
TEST_ASSERT_EQUAL(digitalRead(LED_BUILTIN), LOW);
}
void setup() {
// NOTE!!! Wait for >2 secs
// if board doesn't support software reset via Serial.DTR/RTS
delay(2000);
UNITY_BEGIN(); // IMPORTANT LINE!
RUN_TEST(test_led_builtin_pin_number);
pinMode(LED_BUILTIN, OUTPUT);
}
uint8_t i = 0;
uint8_t max_blinks = 5;
void loop() {
if (i < max_blinks)
{
RUN_TEST(test_led_state_high);
delay(500);
RUN_TEST(test_led_state_low);
delay(500);
i++;
}
else if (i == max_blinks) {
UNITY_END(); // stop unit testing
}
}
#endif
```

To run the tests click on the **Test** button, you will need to log in with your PlatforIO account.

0 comments on commit 9b89d29

Please sign in to comment.