Main Content

Incorrect use of test setup/teardown API

Test setup invoked in test suite configuration, suite setup invoked in test configuration, and so on

Since R2023b

Description

This defect occurs when you invoke setup and teardown macros from the Polyspace® Test™ xUnit API in the incorrect configuration. For instance:

  • You invoke suite-level macros such as PST_SUITE_SETUP or PST_SUITE_TEST_SETUP in the test configuration macro PST_TEST_CONFIG (instead of the suite configuration macro PST_SUITE_CONFIG).

  • You invoke test-level macros such as PST_SETUP in the suite configuration macro PST_SUITE_CONFIG (instead of the test configuration macro PST_TEST_CONFIG).

Similar considerations apply to teardown macros PST_SUITE_TEARDOWN, PST_SUITE_TEST_TEARDOWN and PST_TEARDOWN.

This checker is enabled if you specify the value pstunit for the option Libraries used (-library) or find defects in a test file from the Polyspace Platform (Polyspace Test) user interface. For more information, see Check for Bugs and Run-Time Errors in C/C++ Tests and Functions Under Test (Polyspace Test).

Risk

When you run tests, setup and teardown macros invoked in the incorrect configuration get ignored. As a result, your tests are not set up correctly.

Fix

Invoke test setup and teardown macros in the correct configuration.

Setup and Teardown MacroConfiguration
  • PST_SUITE_SETUP

  • PST_SUITE_TEARDOWN

  • PST_SUITE_TEST_SETUP

  • PST_SUITE_TEST_TEARDOWN

PST_SUITE_CONFIG
  • PST_SETUP

  • PST_TEARDOWN

PST_TEST_CONFIG

Examples

expand all

In this example, the suite-level macros PST_SUITE_TEST_SETUP and PST_SUITE_TEST_TEARDOWN are incorrectly invoked in the test configuration PST_TEST_CONFIG. These macros specify setup and teardown functions that must be run before and after every test in a suite and therefore must be part of the suite configuration.

  • Header with type definitions (named example.h):

    #include <limits.h>
    extern unsigned gSum;
    
    typedef enum {
        NOT_SATURATED = 0,
        SATURATED = 1
    } SUM_STATUS;
    
    SUM_STATUS globalSum(unsigned x);
  • Source:

    #include "example.h"
    
    unsigned gSum;
    
    SUM_STATUS globalSum(unsigned x) {
        if (x > UINT_MAX - gSum) {
            gSum = UINT_MAX;
            return SATURATED;
        }
        gSum += x;
        return NOT_SATURATED;
    }
  • Tests:

    #include "pstunit.h"
    #include "example.h"
    
    //Test utilities
    void setgSum (void) {
        gSum = 0;
    }
    
    void resetgSum (void) {
        gSum = 0;
    }
    
    
    PST_SUITE_CONFIG(gSumTests) {}
    
    PST_TEST_CONFIG(gSumTests, TestCaseOne){
        PST_SUITE_TEST_SETUP(setgSum);
        PST_SUITE_TEST_TEARDOWN(resetgSum);
    }
    PST_TEST_BODY(gSumTests, TestCaseOne) {
        unsigned test_data = UINT_MAX/2 + 1;
        unsigned expected_gSum = UINT_MAX/2 + 1;
        int  i;
        
         for(i = 1; i<= 1; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in nonsaturating sum.");
          
    }
    
    PST_REGFCN(myRegFcn) {
        PST_ADD_TEST(gSumTests, TestCaseOne);
    }
    
    #ifndef PSTEST_BUILD
    int main(int argc, char *argv[]) {
        PST_REGFCN_CALL(myRegFcn);
        return PST_MAIN(argc, argv);
    }
    #endif
    
Correction — Place Suite-test Setup and Teardown in Suite Configuration

Place invocations to macros PST_SUITE_TEST_SETUP and PST_SUITE_TEST_TEARDOWN in a suite configuration PST_SUITE_CONFIG. The corrected test file is shown below.

  • Header with type definitions (named example.h):

    #include <limits.h>
    extern unsigned gSum;
    
    typedef enum {
        NOT_SATURATED = 0,
        SATURATED = 1
    } SUM_STATUS;
    
    SUM_STATUS globalSum(unsigned x);
  • Source:

    #include "example.h"
    
    unsigned gSum;
    
    SUM_STATUS globalSum(unsigned x) {
        if (x > UINT_MAX - gSum) {
            gSum = UINT_MAX;
            return SATURATED;
        }
        gSum += x;
        return NOT_SATURATED;
    }
  • Tests:

    #include "pstunit.h"
    #include "example.h"
    
    //Test utilities
    void setgSum (void) {
        gSum = 0;
    }
    
    void resetgSum (void) {
        gSum = 0;
    }
    
    
    PST_SUITE_CONFIG(gSumTests) {
        PST_SUITE_TEST_SETUP(setgSum);
        PST_SUITE_TEST_TEARDOWN(resetgSum);
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseOne){}
    PST_TEST_BODY(gSumTests, TestCaseOne) {
        unsigned test_data = UINT_MAX/2 + 1;
        unsigned expected_gSum = UINT_MAX/2 + 1;
        int  i;
        
         for(i = 1; i<= 1; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in nonsaturating sum.");
          
    }
    
    PST_REGFCN(myRegFcn) {
        PST_ADD_TEST(gSumTests, TestCaseOne);
    }
    
    #ifndef PSTEST_BUILD
    int main(int argc, char *argv[]) {
        PST_REGFCN_CALL(myRegFcn);
        return PST_MAIN(argc, argv);
    }
    #endif
    

Result Information

Group: Libraries Misuse
Language: C | C++
Default: Off
Command-Line Syntax: PSTUNIT_MISUSE_SETUP_TEARDOWN
Impact: Medium

Version History

Introduced in R2023b