Example greentea tests with Blinky library

Dependencies:   Blinky

Committer:
sarahmarshy
Date:
Mon Sep 19 22:12:16 2016 +0000
Revision:
0:0d5d376157a0
Child:
1:b0034f8b2c42
greentea tests with example Blinky library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sarahmarshy 0:0d5d376157a0 1 #include "Blinky.h"
sarahmarshy 0:0d5d376157a0 2 #include "mbed.h"
sarahmarshy 0:0d5d376157a0 3 #include "greentea-client/test_env.h"
sarahmarshy 0:0d5d376157a0 4 #include "unity.h"
sarahmarshy 0:0d5d376157a0 5 #include "utest.h"
sarahmarshy 0:0d5d376157a0 6 #include "rtos.h"
sarahmarshy 0:0d5d376157a0 7
sarahmarshy 0:0d5d376157a0 8 using namespace utest::v1;
sarahmarshy 0:0d5d376157a0 9
sarahmarshy 0:0d5d376157a0 10 //All tests will blink the LED 5 times
sarahmarshy 0:0d5d376157a0 11 const int expected_blinks = 5;
sarahmarshy 0:0d5d376157a0 12 //To be set by interrupt handler
sarahmarshy 0:0d5d376157a0 13 int measured_blinks = 0;
sarahmarshy 0:0d5d376157a0 14
sarahmarshy 0:0d5d376157a0 15 // A test that returns successfully is considered successful
sarahmarshy 0:0d5d376157a0 16 void test_success() {
sarahmarshy 0:0d5d376157a0 17 TEST_ASSERT(true);
sarahmarshy 0:0d5d376157a0 18 }
sarahmarshy 0:0d5d376157a0 19
sarahmarshy 0:0d5d376157a0 20 // Tests that assert are considered failing
sarahmarshy 0:0d5d376157a0 21 void test_failure() {
sarahmarshy 0:0d5d376157a0 22 TEST_ASSERT(false);
sarahmarshy 0:0d5d376157a0 23 }
sarahmarshy 0:0d5d376157a0 24
sarahmarshy 0:0d5d376157a0 25 //To be called by interrupt, increases the number of measured rising edges
sarahmarshy 0:0d5d376157a0 26 void count_blinks(){
sarahmarshy 0:0d5d376157a0 27 TEST_ASSERT_MESSAGE(++measured_blinks<=expected_blinks, "******Too many blinks!******");
sarahmarshy 0:0d5d376157a0 28 }
sarahmarshy 0:0d5d376157a0 29
sarahmarshy 0:0d5d376157a0 30 //Generic blink test
sarahmarshy 0:0d5d376157a0 31 //interval => the the amount of time between blinks
sarahmarshy 0:0d5d376157a0 32 //led => PinName of LED to blink
sarahmarshy 0:0d5d376157a0 33 bool test_blinks(int interval, PinName led){
sarahmarshy 0:0d5d376157a0 34 //reset the measured blinks to 0 for each case
sarahmarshy 0:0d5d376157a0 35 measured_blinks = 0;
sarahmarshy 0:0d5d376157a0 36 //Set an appropriate wait time to measure exactly expected_blinks
sarahmarshy 0:0d5d376157a0 37 //with the given interval
sarahmarshy 0:0d5d376157a0 38 float wait_time = 2*interval*expected_blinks;
sarahmarshy 0:0d5d376157a0 39 //Create an interrupt that calls count_blinks on rising edge of LED pin
sarahmarshy 0:0d5d376157a0 40 InterruptIn blink_counter(led);
sarahmarshy 0:0d5d376157a0 41 //Create Blinky
sarahmarshy 0:0d5d376157a0 42 Blinky blinker(led, interval);
sarahmarshy 0:0d5d376157a0 43 //Attach interrupt
sarahmarshy 0:0d5d376157a0 44 blink_counter.rise(&count_blinks);
sarahmarshy 0:0d5d376157a0 45 //start blinky
sarahmarshy 0:0d5d376157a0 46 blinker.start();
sarahmarshy 0:0d5d376157a0 47 //Wait the appropriate amount of time for expected_blinks
sarahmarshy 0:0d5d376157a0 48 Thread::wait(wait_time);
sarahmarshy 0:0d5d376157a0 49 blinker.stop();
sarahmarshy 0:0d5d376157a0 50 test_success();
sarahmarshy 0:0d5d376157a0 51 }
sarahmarshy 0:0d5d376157a0 52
sarahmarshy 0:0d5d376157a0 53 void blink_500(){
sarahmarshy 0:0d5d376157a0 54 //Blinky with a 500 ms interval between blinks
sarahmarshy 0:0d5d376157a0 55 test_blinks(500, LED1);
sarahmarshy 0:0d5d376157a0 56 }
sarahmarshy 0:0d5d376157a0 57 void blink_1000(){
sarahmarshy 0:0d5d376157a0 58 //Blinky with a 1000 ms interval between blinks
sarahmarshy 0:0d5d376157a0 59 test_blinks(1000, LED1);
sarahmarshy 0:0d5d376157a0 60 }
sarahmarshy 0:0d5d376157a0 61
sarahmarshy 0:0d5d376157a0 62 utest::v1::status_t test_setup(const size_t number_of_cases) {
sarahmarshy 0:0d5d376157a0 63 // Setup Greentea using a reasonable timeout in seconds
sarahmarshy 0:0d5d376157a0 64 GREENTEA_SETUP(40, "default_auto");
sarahmarshy 0:0d5d376157a0 65 return verbose_test_setup_handler(number_of_cases);
sarahmarshy 0:0d5d376157a0 66 }
sarahmarshy 0:0d5d376157a0 67
sarahmarshy 0:0d5d376157a0 68 // Test cases
sarahmarshy 0:0d5d376157a0 69 Case cases[] = {
sarahmarshy 0:0d5d376157a0 70 Case("Testing 500 ms interval", blink_500),
sarahmarshy 0:0d5d376157a0 71 Case("Testing 1000 ms interval", blink_1000),
sarahmarshy 0:0d5d376157a0 72 };
sarahmarshy 0:0d5d376157a0 73
sarahmarshy 0:0d5d376157a0 74 Specification specification(test_setup, cases);
sarahmarshy 0:0d5d376157a0 75
sarahmarshy 0:0d5d376157a0 76 // Entry point into the tests
sarahmarshy 0:0d5d376157a0 77 int main() {
sarahmarshy 0:0d5d376157a0 78 return !Harness::run(specification);
sarahmarshy 0:0d5d376157a0 79 }