gpt4 book ai didi

c++ - CppUtest 在测试之间共享资源, undefined reference

转载 作者:行者123 更新时间:2023-11-28 05:56:08 24 4
gpt4 key购买 nike

我认为这是一个简单的问题...我希望变量 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'

有什么想法吗?

最佳答案

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 .

关于c++ - CppUtest 在测试之间共享资源, undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34125626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com