sketch_unittest.cc File

  • This file contains two templates.

    • Template 1 - When using general components This template is used in situations where the code should execute regardless of the conditions.

      • Example 1) Is the input/output of pin 13 configured correctly?

      • Example 2) Is the serial communication value set to 9600 correctly?

    • Template 2 - When receiving input values from sensors This template is used in situations where the execution result varies depending on the conditions.

      • Example 1) Is the LED properly turned on when the measurement value of the motion sensor is HIGH?

      • Example 2) Is the sound of the piezo buzzer properly played when the measurement value of the ultrasonic sensor is 17cm?

ℹ️ You can copy and paste the templates as needed to set multiple grading criteria. However, you should appropriately modify the numbers in the test suite (quiz1) to quiz1, quiz2, etc.

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

Template 1 - When using general components

  1. ① Section - Write the code that you want to check for execution, excluding the semicolon (;). ② Section - Write the minimum number of times the code should be executed.

EXPECT_CALL(*arduinoMock, _(1)_.Times(AtLeast(_(2)_));

--------------------

// Example 1: Has the code that sets pin 13 as output been executed at least once?
EXPECT_CALL(*arduinoMock, pinMode(13, OUTPUT)).Times(AtLeast(1));

// Example 2: Has the code that pauses the execution, regardless of time, been executed at least twice?
EXPECT_CALL(*arduinoMock, delay(_)).Times(AtLeast(2));

⚠️ If you need to check the serial communication-related code, write only the necessary code excluding the Serial. part.

// Example 1: Has the code that sets the serial communication value to 9600 been executed at least once?
EXPECT_CALL(*arduinoMock, begin(9600)).Times(AtLeast(1));

// Example 2: Has the code that outputs the string "LED ON" through serial communication (including line breaks) been executed at least once?
EXPECT_CALL(*arduinoMock, println("LED ON")).Times(AtLeast(1));
  1. Set setup(); or loop(); depending on the area where grading should be performed.

    1. If you want to check the code in the setup() function. → Keep only setup(); and delete loop();

    2. If you want to check the code in the loop() function. → Keep only loop(); and delete setup();

⚠️ You cannot check the code in both the setup() and loop() functions simultaneously. You need to divide them into separate test cases, as shown in the example below.

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

Template 2 - When receiving input values from sensors

  1. ① Section - Write the code that the sensor uses to measure values, excluding the semicolon (;). ② Section - Write the assumed value that the sensor measures.

EXPECT_CALL(*arduinoMock, _(1)_.WillOnce(Return(_(2)_));

--------------------

// Example 1: If the HIGH (1) value is detected from pin 2...
EXPECT_CALL(*arduinoMock, digitalRead(2)).WillOnce(Return(1));

// Example 2: If the ECHO (ultrasonic sensor) detects the value 1000 (approximately 17cm)...
EXPECT_CALL(*arduinoMock, pulseIn(ECHO, HIGH)).WillOnce(Return(1000));
  1. ① Section - Write the code that you want to check for execution, excluding the semicolon (;). ② Section - Write the minimum number of times the code should be executed.

EXPECT_CALL(*arduinoMock, _(1)_.Times(AtLeast(_(2)_));

--------------------

// Example 1: ...played a 262Hz (C4) sound for 200ms through the buzzer at least once?
EXPECT_CALL(*arduinoMock, tone(9, 262, 200)).Times(AtLeast(1));

// Example 2: ...flashed the RGB LED regardless of the color at least twice?
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 check the serial communication-related code, write only the necessary code excluding the Serial. part.

// Example 1: Has the code that sets the serial communication value to 9600 been executed at least once?
EXPECT_CALL(*arduinoMock, begin(9600)).Times(AtLeast(1));

// Example 2: Has the code that outputs the string "LED ON" through serial communication (including line breaks) been executed at least once?
EXPECT_CALL(*arduinoMock, println("LED ON")).Times(AtLeast(1));

Last updated