Skip to main content

sketch_unittest.cc file

  • This file contains 2 templates.
    • Template 1 - When using general components
      This template is used in situations where the operation is executed regardless of conditions.
      • Example 1) Did you set the input/output of pin 13 correctly?
      • Example 2) Did you set the serial communication value to 9600 correctly?
    • Template 2 - When receiving values from a sensor
      This template is used in situations where the execution results vary based on conditions.
      • Example 1) When the human detection sensor's measurement value is HIGH, did you turn on the LED correctly?
      • Example 2) When the ultrasonic sensor's measurement value is 17cm, did you play the sound on the piezo buzzer correctly?

ℹ️ You can copy the templates as needed to set multiple grading criteria. However, the number part of the test suite (quiz1) should be appropriately modified to quiz1, quiz2, etc.

// Example: If there are 3 grading criteria
TEST(quiz1, case1) {
...
}
TEST(quiz2, case1) {
...
}
TEST(quiz3, case1) {
...
}

Template 1 - When using general components

Template 1 Configuration

Template 1 Configuration

  1. ① Area - Write the code you want to check for execution without the semicolon (;).
    ② Area - Write the minimum number of times that the code should be executed.
EXPECT_CALL(*arduinoMock, _(1)_.Times(AtLeast(_(2)_));

  • Example 1: Did you set pin 13 as output at least once?
EXPECT_CALL(*arduinoMock, pinMode(13, OUTPUT)).Times(AtLeast(1));
  • Example 2: Regardless of time, did you pause the execution at least twice?
EXPECT_CALL(*arduinoMock, delay(_)).Times(AtLeast(2));

⚠️ If you need to verify serial communication-related code, write everything except the Serial. part at the beginning of the code.

// Example 1: Did you set the serial communication value to 9600 at least once?
EXPECT_CALL(*arduinoMock, begin(9600)).Times(AtLeast(1));

// Example 2: Did you print the string "LED ON" (including line feed) via serial communication at least once?
EXPECT_CALL(*arduinoMock, println("LED ON")).Times(AtLeast(1));
  1. Based on the area of the function you want to grade, set either setup(); or loop();.
    1. If you want to check the code in the setup() function?
      → Leave only setup(); and delete loop();
    2. If you want to check the code in the loop() function?
      → Leave only loop(); and delete setup();

⚠️ You cannot check both the setup() and loop() functions simultaneously. You should create separate test cases to test setup(); and loop(); as shown in the example below.

// Example: If you want to grade both setup() function and loop() function
TEST(quiz1, case1) {
...
setup();
...
}
TEST(quiz2, case1) {
...
loop();
...
}

Template 2 - When receiving values from a sensor

Template 2 Configuration

Template 2 Configuration

  1. ① Area - Write the code that measures the values using the sensor without the semicolon (;).
    ② Area - Write the value that the sensor is expected to measure.
EXPECT_CALL(*arduinoMock, _(1)_.WillOnce(Return(_(2)_));

  • Example 1: If it detects HIGH (1) value on pin 2...
EXPECT_CALL(*arduinoMock, digitalRead(2)).WillOnce(Return(1));
  • Example 2: If it detects 1000 (approximately 17cm) value on ECHO (ultrasonic sensor)...
EXPECT_CALL(*arduinoMock, pulseIn(ECHO, HIGH)).WillOnce(Return(1000));
  1. ① Area - Write the code you want to check for execution without the semicolon (;).
    ② Area - Write the minimum number of times that the code should be executed.
EXPECT_CALL(*arduinoMock, _(1)_.Times(AtLeast(_(2)_));

  • Example 1: ...Did you play a sound of 262Hz (C4) for 200ms on the buzzer at least once?
EXPECT_CALL(*arduinoMock, tone(9, 262, 200)).Times(AtLeast(1));
  • Example 2: ...Did you blink the RGB LED at least twice regardless of color?
EXPECT_CALL(*arduinoMock, analogWrite(RED, _)).Times(AtLeast(2));
EXPECT_CALL(*arduinoMock, analogWrite(GREEN, _)).Times(AtLeast(2));
EXPECT_CALL(*arduinoMock, analogWrite(BLUE, _)).Times(AtLeast(2));

⚠️ If you need to verify serial communication-related code, write everything except the Serial. part at the beginning of the code.

// Example 1: Did you set the serial communication value to 9600 at least once?
EXPECT_CALL(*arduinoMock, begin(9600)).Times(AtLeast(1));

// Example 2: Did you print the string "LED ON" (including line feed) via serial communication at least once?
EXPECT_CALL(*arduinoMock, println("LED ON")).Times(AtLeast(1));