我认为这是一个简单的问题...我希望变量virtualLeds
在测试组LedDriverTests
中的所有测试之间共享,这是代码:
#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);
}
当我尝试编译时,我收到以下错误:
undefined reference to `TEST_GROUP_CppUTestGroupLedDriverTests::virtualLeds'
有什么想法吗?
答案 0 :(得分:1)
TEST_GROUP
宏创建一个struct,然后在该结构中创建一个静态成员。详细了解static members in struct。简而言之,这就是您无法以这种方式访问它的原因virtualLeds = 0xFFFF;
查看你的代码。我想你想做的是:
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()
之前调用 TEST
请记住unit tests should pass or fail independently of other tests。
答案 1 :(得分:0)
您必须为静态成员添加定义:
TEST_GROUP(LedDriverTests)
{
static uint16_t virtualLeds;
// ...
};
// Definition of static data
uint16_t TEST_GROUP_CppUTestGroupLedDriverTests::virtualLeds;
// ...