Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use arduino_ci to run unit tests as part of git pull requests (not just tests of compilation) #91

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ianfixes
Copy link

@ianfixes ianfixes commented Mar 5, 2018

Context: adafruit/ci-arduino#16

I've created an alternative CI system to the install.sh method referenced in the above repository. This was a personal project and I make no claim that it is better for your purposes than what you have now. However, since the development was heavily inspired by install.sh and the proof of concept was built against this library, it seems appropriate to submit my work for your consideration.

Scope of change

Only the CI-related scripts have been changed, and of that functionality, no compilation steps have been removed*. In fact, I've added some example unit tests (which are enabled by my new ruby gem) that increase the amount of testing.

No changes to the library itself have been made.

(* except for boards whose compilation failed due to a lack of SoftwareSerial availability -- I edited the config to avoid compiling against those. If that was in error, that might indicate a bug in my system.)

Please run any tests or examples that can exercise your modified code

I'm glad you asked. This pull request enables unit tests to happen as part of Travis CI. The results will be visible there as TAP-13-formatted output. You can see my own battery of tests (for the library itself and all its components) in this Travis CI job.

I've added a test of getIMEI as a demonstration, see https://github.com/adafruit/Adafruit_FONA/compare/master...ianfixes:2018-03-05_arduino_ci_unit_tests?expand=1#diff-38242b01e4859016b646adf632a7cbacR83

This is accomplished in two pieces. First, we need to create a mock of the serial device that the FONA code will be talking to. That's as simple as extending an existing class called DeviceUsingBytes from the arduino_ci cpp library:

class FakeFona : public DeviceUsingBytes {
  public:
    String mLast;

    FakeFona() : DeviceUsingBytes() {
      mLast = "";
      addResponseCRLF("AT", "OK");
      addResponseCRLF("ATE0", "OK");
      addResponseCRLF("AT+CVHU=0", "OK");
      addResponseCRLF("ATI", "OK");
      addResponseCRLF("AT+CPMS=\"SM\",\"SM\",\"SM\"", "OK");
      addResponseCRLF("AT+GSN", "1234567890abcde\r\nOK");
    }

    virtual ~FakeFona() {}

    virtual void onMatchInput(String output) {
      mLast = output;
      state->digitalPin[FONA_TX].fromAscii(output, bigEndian);
    }
};

Note the definitions of requests and responses (i.e. AT will prompt a response of OK), and the onMatchInput function which sets the digital pin state to a sequence of ASCII bits. The outcome of this will be that the SoftwareSerial library listening on FONA_TX will receive what looks like a response from a real device.

The actual unit test looks like this (extra annotations added here):

// retrieve the IMEI from a serial device faked by the FakeFona class
unittest(fona_imei) {
  // Initialize the mocked pins and mocked hardware serial
  GodmodeState* state = GODMODE();
  state->reset();

  // set up FONA on serial port as usual (this was taken from an example)
  Adafruit_FONA_debug fona = Adafruit_FONA_debug(FONA_RST);
  SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
  SoftwareSerial *fonaSerial = &fonaSS;
  fonaSerial->begin(115200);

  // set up our fake modem on the other end.
  // note that the digitalPin we attach to is an ObservableDataStream
  FakeFona fakeFona;
  fakeFona.attach(&state->digitalPin[FONA_RX]);

  // test that the initialization of the fona modem succeeds.
  // this is more a test of our FakeFona class than anything else
  assertTrue(fona.begin(*fonaSerial));

  // allocate storage and execute the getIMEI command to fill it 
  char imei[16] = {0}; // MUST use a 16 character buffer for IMEI!
  uint8_t imeiLen = fona.getIMEI(imei);

  // finally, check that the library picked up the value we specified.
  assertEqual("1234567890abcde", imei);
}

I don't expect this to be merged anytime soon, but I welcome any comments/criticisms/suggestions on the arduino_ci library and methodology itself. Thanks for inspiring me to write it!

@ianfixes ianfixes force-pushed the 2018-03-05_arduino_ci_unit_tests branch 8 times, most recently from 3460b15 to 1aa9e5d Compare March 7, 2018 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant