Divide the test cases to check when running a scenario test to verify the value of a specific variable during the test run. For instance, in a test where the cycle unit is ms, if we want to check the changes in a specific variable at 2 and 4 seconds, we design the test cases as follows:

Test Case (TC) Duration Total Duration
1 1999 1999
2 1 2000
3 1999 3999
4 1 4000

Check the value change before/after 2 seconds through TC3 and TC4 and check the value change before/after 4 seconds through TC3 and TC4.

By dividing the test cases in this way, we can check desired values at specific time. We will explain it in detail with a simple example.

Source Code and Requirement

The source code and requirement to be used as an example are as follows.

#include <stdio.h>
#include <stdbool.h>

typedef enum {
    CLOSED, OPEN
} OpenCloseState;

typedef enum {
    OFF, ON
} OnOffState;

OpenCloseState doorState;
OpenCloseState doorSensor;
OnOffState ignitionState;
OnOffState lightState;

void initial() {
	doorState = OPEN;
	doorSensor = OPEN;
	ignitionState = OFF;
	lightState = OFF;
}

void lightOn() { if (lightState != ON)     lightState = ON; }

void lightOff() { if (lightState != OFF)     lightState = OFF; }

void setDoorSensor(OpenCloseState sensor) {
	doorSensor = sensor;
}

void tick() {
    static int timer = 0;

    if (doorState == OPEN && doorSensor == CLOSED) {
        timer = 500;
        lightOn();
    } else if (ignitionState == ON){
        timer = 0;
        lightOff();
    }

    if (timer > 0)     timer--;

    if (timer == 0)     lightOff();

    doorState = doorSensor;
}
Requirement: When the door of the car, which was open, closes, the interior light stays on for 5 seconds and then turns off.

Test Design

We will design a test that meets these requirements.

h3 Design of Periodic Function and Cycle Unit

Using The indoor light turns off after being on for 5 seconds and timer = 500;, it can be understood that the time unit of the timer is ms, and the function “@tick()@” is called every 10ms. Therefore, set “@tick()@” as the periodic function, the cycle unit as ms, the start time of “@tick()@” as 0, and the cycle time as 10.

h3 Design of Initial Function

The initial() function initializes each sensor and state. Both doorState and doorSensor are initially set to OPEN. To satisfy the requirement When the door of the car, which was open, closes, set the parameter value in the setDoorSensor() function to CLOSED.

h3 Design of Test Cases and Scenario Variable (Time)

To verify the requirement, check the output value of lightState. The interior light should remain on for 5 seconds and then turn off. This means that lightState remains ON until the 499th call of tick(), and lightState changes to OFF on the 500th call. To check this, set the time to 4990 for the first test case and 10 for the second test case. Select lightState in the global variable, check [Output], run the test, and verify the value of lightState.

Write Test

Based on the above design, writing tests will look like the following:

  • [Test Info Tab]
  • [Test Case Tab]

Checking Test Results

Run the test and verify the results in [Test Case Tab].

During 0ms to 4990ms, the lightState is ON(1), and when it reaches 5000ms, the lightState changes to OFF(0). By using scenario testing, it has been confirmed that the source code meets the requirements.

Need more help with this?
Don’t hesitate to contact us here.

Thanks for your feedback.