During the requirement testing, you may come across the following requirement.

When the door of the car, which was open, closes, the interior light stays on for 5 seconds and then turns off.

When there is a timer function for this, this timer function should be repeatedly called at set intervals to check whether the interior light turns off after 5 seconds.

There are situations where you need to test functions that are called periodically. After CT 2023.12, a scenario testing feature has been introduced to support these types of tests.

Conditions for Scenario Testing

To convert a normal test into a scenario test, all of the following conditions must be met.

  • C Project
  • The test target functions must only have functions with void return type and no parameters

Convert to Scenario Test

In [Test Editor] > [Test Info Tab] > [Test target function], select [Convert to Scenario test] to convert to a scenario test.

Scenario tests maintain the test case context. That is, it runs the next test case while maintaining the state of the previous test case.

Scenario Test

Two variables are added to assist with scenario testing.

  • Duration
    • You can enter values in [Test Case Tab].
    • It determines the number of iterations of the for loop for each test case.
  • Total Duration
    • It shows the cumulative number of repetitions of the for loop in the test case.

Among the test target fucntions, two settings are added to the periodic function:

  • Start time
    • This is the time at which the function calls start.
    • If the start time is 40, the function won’t be called when the total duration is between 0 and 39.
  • Cycle time
    • This is the time interval for repeating function calls.
    • If the period time is 10, the function will be called when the total duration is 10, 20, 30, and so on.

Below is an illustration to help understanding the scenario testing concept.

The cycle unit can be set in ms, μs, or ns. This value is only displayed in the test report and test editor, not affecting the actual execution time. Therefore, even if the cycle unit is set to ms and the total duration is 5000, it won’t be executed for an actual 5 seconds. Scenario testing simulates this time for running tests.

Test Code of Scenario Test

Based on the settings in [Test Info Tab], the test code is generated as follows:
The initial function is a function that is called only once before iterating through a for loop, and it’s initially invoked in the first test case. After that, it isn’t called in any other test cases.

/* Declaration (parameter/return/target object) variables */
unsigned int CS_TC_SPENT_TIME = 0;  // Stores the Duration input value.
static unsigned int CS_TOTAL_SPENT_TIME = 0; // Stores the number of repetitions of test cases. The value of this variable is stored as the output of Total Duration.

/* Input */
CS_TC_SPENT_TIME = CS_INT_INPUT(unsigned int, "CS_TC_SPENT_TIME");

/* Call initial function */
if (CS_TOTAL_SPENT_TIME == 0) {
    // Position where initial function is added
}

// Where periodic functions are repeated. Loops for the Duration input value.
for (int CS_CYCLE_INDEX = 0; CS_CYCLE_INDEX < CS_TC_SPENT_TIME; CS_CYCLE_INDEX++) {
    if ((CS_TOTAL_SPENT_TIME >= 0) && ((CS_TOTAL_SPENT_TIME - 0) % 10 == 0)) {

        /* TASK_1ms() */
        TASK_1ms();

    }
    CS_TOTAL_SPENT_TIME++;
}

/* Output */
CS_INT_OUTPUT(CS_TOTAL_SPENT_TIME, "CS_TOTAL_SPENT_TIME");

Examples

You can fulfill various requirements by using scenario testing.

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

Thanks for your feedback.