如何使用 Visual Studio CppUnitTestFramework 初始化测试变量 [英] How to initialize test variables using Visual Studio CppUnitTestFramework

查看:39
本文介绍了如何使用 Visual Studio CppUnitTestFramework 初始化测试变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个机器人控制器类 Controller,其中我为 4 个可控电机中的每一个使用了结构 Axis.

对于每个测试,我都想重置所有内容,因此我在类中创建了一个指针,在每个测试方法之前,该指针更改为一个新的 Controller.初始化在 TEST_METHOD_INITIALIZE 中工作正常,但是一旦调用任何 TEST_METHOD 程序似乎重置轴指针.

感谢您的帮助!

经过进一步分析,我有理论,初始化的Axis对象Axis init_mx在方法完成后被删除.

Edit2:我认为这是一个稍微复杂的问题,如下所示:

<代码> }...测试方法(id_3_next_mode){mx->位置=5;控制器->getAxisPositionMx();//指针中的轴被重置,因此没有指向//提供了来自 TEST_METHOD_INITIALIZE 的结构}}};}

Controller.h(摘录):

私有:结构轴 *mx, *my, *mz, *mg;

Controller.cpp(摘录)

Controller::Controller(Axis *mx_in, Axis *my_in, Axis *mz_in, Axis *mg_in){mx = mx_in;我的 = my_in;mz = mz_in;毫克 = mg_in;}

解决方案

您发现了最初的错误,但我认为值得指出测试模块、类、方法和特殊初始化/清理方法的意图的一些细节工作.

需要注意的最重要的一点是,每次执行测试方法时都会实例化一个类的新实例,因此每个测试都会有一组新的类变量.这意味着您的示例测试类可能如下所示:

TEST_CLASS(UnitTestController){民众:结构轴 mx, my, mz, mg;控制器控制器;//定义一个构造函数来初始化类变量单元测试控制器():mx(50)、my(50)、mz(50)、mg(50)、控制器(mx,my,mz,mg){}测试方法(id_3_next_mode){mx.position = 5;控制器.getAxisPositionMx();}};

如果你想让一个变量在测试之间保持不变,你可以把它放在测试类之外.

命名空间 UnitTest {SomeClass moduleVar1;TEST_CLASS(UnitTestClass){民众:TEST_METHOD(TestMethod1){断言::IsTrue(moduleVar1.SomeMethod());}TEST_METHOD(TestMethod2){断言::IsFalse(moduleVar1.SomeOtherMethod());}};}

该框架还提供了以下 Initialize 和 Cleanup 函数,可用于操作测试环境和设备:

  • TEST_METHOD_INITIALIZE - 在运行每个测试方法之前调用一次.
  • TEST_METHOD_CLEANUP - 在运行每个测试方法后调用一次.
  • TEST_CLASS_INITIALIZE - 在运行属于此类的任何测试之前,每个模块调用一次.
  • TEST_CLASS_CLEANUP - 在运行属于此类的所有测试后,每个模块调用一次.
  • TEST_MODULE_INITIALIZE - 在运行任何测试或类初始化器之前,每个模块调用一次.
  • TEST_MODULE_CLEANUP - 在运行所有测试和类清理后每个模块调用一次.

I'm writing a roboter controller class Controller in which I'm using a struct Axis for each of the 4 controllable motors.

For each test I want to reset everything, so I created a pointer in the class which is changed to a new Controller before each test method. The initialisation works fine in TEST_METHOD_INITIALIZE, but once any TEST_METHOD is called the program seems reset the Axis pointers.

Thanks for your help!

Edit: After further analysis I have the theory, that the initialised Axis objects Axis init_mx are deleted after the method is finished.

Edit2: I think this a slightly more complex problem like this: Pointer to local variable in C++ Nevertheless, I didn't find a way to reset the Axis variables at for every method without actually resetting each variable in it.

using namespace Microsoft::VisualStudio::CppUnitTestFramework;
...
namespace UnitTest
{

    TEST_CLASS(UnitTestController)
    {
    public:
        Controller* controller;
        struct Axis *mx, *my, *mz, *mg;

        TEST_METHOD_INITIALIZE(methodName)
        {
            Axis init_mx(50), init_my(50), init_mz(50), init_mg(5);         
            mx = &init_mx;
            my = &init_my;
            mz = &init_mz;
            mg = &init_mg;
            Controller init_controller(mx, my, mz, mg);
            controller = &init_controller;

        }
        ...
        TEST_METHOD(id_3_next_mode)
        {
            mx->position = 5; 
            controller->getAxisPositionMx();              
            //Axis in pointers got reset and therefore have no pointers to the 
            //provided structs from TEST_METHOD_INITIALIZE

        }

        }

    };
}

Controller.h(excerpt):

private:
struct Axis *mx, *my, *mz, *mg;

Controller.cpp (excerpt)

Controller::Controller(Axis *mx_in, Axis *my_in, Axis *mz_in, Axis *mg_in)
{
    mx = mx_in;
    my = my_in;
    mz = mz_in;
    mg = mg_in;
}

解决方案

You found your original bug, but I think it's worth pointing out some details of how test modules, classes, methods, and the special initialize/cleanup methods are intended to work.

The most important thing to note is that a new instance of the class is instantiated every time a test method is executed, so each test will have a fresh set of class variables. This means that your example test class could look like:

TEST_CLASS(UnitTestController)
{
public:
    struct Axis mx, my, mz, mg;
    Controller controller;

    // Define a constructor to initialize the class variables
    UnitTestController() : 
        mx(50), my(50), mz(50), mg(50),
        controller(mx, my, mz, mg)
    {
    }

    TEST_METHOD(id_3_next_mode)
    {
        mx.position = 5; 
        controller.getAxisPositionMx();              
    }
};

If you wanted to have a variable persist between tests, you could put it outside the test class.

namespace UnitTest {

    SomeClass moduleVar1;

    TEST_CLASS(UnitTestClass)
    {
    public:

        TEST_METHOD(TestMethod1)
        {
           Assert::IsTrue(moduleVar1.SomeMethod());
        }

        TEST_METHOD(TestMethod2)
        {
           Assert::IsFalse(moduleVar1.SomeOtherMethod());
        }
    };
} 

The framework also provides the following Initialize and Cleanup functions which can be used to manipulate the test environment and fixtures:

  • TEST_METHOD_INITIALIZE - Called once before running each test method.
  • TEST_METHOD_CLEANUP - Called once after running each test method.
  • TEST_CLASS_INITIALIZE - Called once per module before running any tests belonging to this class.
  • TEST_CLASS_CLEANUP - Called once per module after running all tests belonging to this class.
  • TEST_MODULE_INITIALIZE - Called once per module before running any tests or class initalizers.
  • TEST_MODULE_CLEANUP - Called once per module after running all tests and class cleanups.

这篇关于如何使用 Visual Studio CppUnitTestFramework 初始化测试变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆