Mite
Mite

Reputation: 57

CppUtest shared resources between tests, undefined reference

This is a simple question I believe... I want the variable virtualLeds to be shared between all the tests in the test group LedDriverTests, this is the code:

#include "CppUTest/TestHarness.h"
#include <stdint.h>

extern "C"
{
    #include "led_driver.h"
}

TEST_GROUP(LedDriverTests)
{
    static uint16_t virtualLeds;

    void setup()
    {
    }

    void teardown()
    {
    }

    void expect(void)
    {
    }

    void given(void)
    {
    }
};

TEST(LedDriverTests, AllOffAtInitialization)
{
    // Set all the bits to 1, the LEDs are turned ON by hardware.
    virtualLeds = 0xFFFF;
    led_driver_create(&virtualLeds);
    LONGS_EQUAL(0x0000, virtualLeds);
}

TEST(LedDriverTests, TurnOnLedOne)
{
    led_driver_turn_on(1);
    LONGS_EQUAL(0x0001, virtualLeds);
}

TEST(LedDriverTests, TurnOffLedOne)
{
    led_driver_turn_on(1);
    led_driver_turn_off(1);
    LONGS_EQUAL(0x0000, virtualLeds);
}

When I try to compile I get the following error:

undefined reference to `TEST_GROUP_CppUTestGroupLedDriverTests::virtualLeds'

Any ideas?

Upvotes: 0

Views: 1561

Answers (2)

ollo
ollo

Reputation: 25370

You have to add a definition for the static member:

TEST_GROUP(LedDriverTests)
{
    static uint16_t virtualLeds;

    // ...
};

// Definition of static data
uint16_t TEST_GROUP_CppUTestGroupLedDriverTests::virtualLeds;

// ...

Upvotes: 1

jayant
jayant

Reputation: 2389

TEST_GROUP macro creates a struct and then you create a static member in that struct. See more about static members in struct. In short that's why you're unable to access it in this way virtualLeds = 0xFFFF;

Looking at your code. I think what you want to do is:

TEST_GROUP(LedDriverTests)
{
    uint16_t virtualLeds;

    void setup()
    {
        // Set all the bits to 1, the LEDs are turned ON by hardware.
        virtualLeds = 0xFFFF;
    }

};

TEST(LedDriverTests, AllOffAtInitialization)
{
    led_driver_create(&virtualLeds);
    LONGS_EQUAL(0x0000, virtualLeds);
}

TEST(LedDriverTests, TurnOnLedOne)
{
    led_driver_turn_on(1);
    LONGS_EQUAL(0x0001, virtualLeds);
}

TEST(LedDriverTests, TurnOffLedOne)
{
    led_driver_turn_on(1);
    led_driver_turn_off(1);
    LONGS_EQUAL(0x0000, virtualLeds);
}

setup() is called before running each TEST

Remember that unit tests should pass or fail independently of other tests.

Upvotes: 1

Related Questions