单元测试嵌入式 C++使用 Visual Studio 2010 编写代码

发布于 2024-10-26 19:28:09 字数 215 浏览 2 评论 0原文

我有要重构/添加的遗留代码。该代码是用 C++ 编写的,针对使用 Greenhills 编译器的嵌入式设备。我听说Visual Studio 2010有更好的测试框架并且需要更少的工作来编写测试用例。是否可以使用VS2010对嵌入代码进行单元测试?有关如何执行此操作或逐步过程的示例将不胜感激。

如果没有,CppUnit/CxxTest如何与VS2010集成?我找不到关于如何实现这一目标的明确文档。

I have legacy code that I am going to refactor/add. The code is written in C++ and is targeted for embedded device using Greenhills compiler. I heard that Visual Studio 2010 has better testing framework and requires less work to write test cases. Is it possible to use VS2010 to unit test the embedded code? An Example on how to do it or stepwise procedure will be greatly appreciated.

If not, how CppUnit/CxxTest can be integrated with VS2010? I couldn't find clear documentation on how to achieve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清君侧 2024-11-02 19:28:09

您可以在 Visual Studio 中编译嵌入式软件。为平台特定功能以及 Green Hills 细节(如果有)创建存根。还要注意任何特定的 Green Hills 宏。您需要找到或创建等效项。

我在 Visual Studio 2008 中使用 CppUnit 和 wxWidgets。我没有尝试单独将 CppUnit 与 Visual Studio 2010 集成。

请记住,您希望将验证代码与嵌入代码分开。嵌入式代码应作为库和头文件导入到您的验证项目中。这使测试保持诚实(尽管我意识到您可能无法将 Green Hills 库与 Visual Studio 库链接起来)。否则,使用 VS 构建嵌入式代码,但在构建步骤中作为单独的库。

如果可能的话,围绕软件需求设计您的测试。之后,设计一些代码来执行公共功能。请记住,开发人员不应该编写代码来满足测试,测试人员也不应该编写代码来满足开发人员的功能。 “如果存在的话,测试一下。”或“如果使用,测试它”。例如,平台可能有 DMA 控制器但不使用它。

编辑 #1 -- 示例

给定“嵌入”或实现中的以下类:

class Title
{
  public:
                                    Title();
                                    Title(const Title& rc);
    virtual                         ~Title();
    Title&                          operator= (const Title& rt);
    const std::string&              get_table_name(void) const;
    const std::string&              get_title_text(void) const;
    void                            set_table_name(const std::string&);
    void                            set_title_text(const std::string& new_text);
};

CppUnit 测试类将如下所示:

#include "cppunit/extensions/HelperMacros.h"

class Test_Ing_Title
    : public CPPUNIT_NS::TestFixture
{
    //---------------------------------------------------------------------
    //  Friends
    //---------------------------------------------------------------------
    CPPUNIT_TEST_SUITE(Test_Ing_Title);
    CPPUNIT_TEST(ing_name_field_testing);
    CPPUNIT_TEST(copying);
    CPPUNIT_TEST(table_name_testing);
    CPPUNIT_TEST(test_id_field);
    CPPUNIT_TEST(title_testing);
    CPPUNIT_TEST(visitor_test);
    CPPUNIT_TEST_SUITE_END();

    //---------------------------------------------------------------------
    //  Public types
    //---------------------------------------------------------------------
  public:

    //---------------------------------------------------------------------
    //  Public Constructors and Destructors
    //---------------------------------------------------------------------
  public:
    //! Constructor - Default
                                Test_Ing_Title();

    //! Copy Constructor
                                Test_Ing_Title(const Test_Ing_Title& n_obj);

    //! Destructor
    virtual                     ~Test_Ing_Title();

    //---------------------------------------------------------------------
    //  Public Overloaded Operators
    //---------------------------------------------------------------------
  public:

    //---------------------------------------------------------------------
    //  Public Methods
    //---------------------------------------------------------------------
  public:
    //! Test cloning of the title record.
    void                        cloning(void);

    //! Test copying
    void                        copying(void);

    //! Test the name field getters and setters
    void                        ing_name_field_testing(void);

    //! Test the table name getters & setters
    void                        table_name_testing(void);

    void                        tearDown(void);

    //! Test the ID field getters and setters.
    void                        test_id_field(void);

    //! Test the title getters and setters
    void                        title_testing(void);

    //! Test visitors to the title.
    void                        visitor_test(void);
};

示例测试方法:

#include <stdafx.h>     // This line is required for precompiled headers on MSVC++.
#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>
#include <string>

void
Test_Ing_Title ::
title_testing(void)
{
    static const char           expected_title_text[] = "Ground Beef";
    const std::string           expected_title(expected_title_text);
    std::string             actual_title;
    Ingredient::Records::Title  ing_title;
    ing_title.set_title_text(expected_title);
    actual_title = ing_title.get_title_text();
    CPPUNIT_ASSERT(actual_title == expected_title);
    return;
}

在上面的示例中,测试类创建要测试的类的实例,然后进行练习的方法。关键点是测试类与系统隔离,或者换句话说,测试类不影响被测系统。将测试代码放在单独的位置将有助于强调这个概念。将“嵌入”代码视为只读。测试到极限,坚守阵地,减少的产品退货数量将是你的奖励(并增加公司的利润)。

HTH。

You can compile embedded software in Visual Studio. Create stubs for platform specific functionality and also for Green Hills specifics, if any. Also beware of any specific Green Hills macros. You'll need to find or create equivalents.

I am using CppUnit in Visual Studio 2008 with wxWidgets. I have not tried to integrate CppUnit with Visual Studio 2010 separately.

Keep in mind you want to keep the validation code separate from the embedded code. The embedded code should be imported into your validation project as a library and header files. This keeps the testing honest (although I realize that you may not be able to link a Green Hills library with a Visual Studio library). Otherwise build the embedded code using VS, but as a separate library in the build step.

Design your testing around the Software Requirements, if possible. After that, design some code to exercise the public functions. Remember, the developer's should not write code to satisfy the testing, nor should the testing folks write code to satisfy the developer's functions. "If it exists, test it." or "If it is used, test it". For example, the platform may have a DMA controller but not use it.

Edit #1 -- Example

Given the following class in the "embedded" or implementation:

class Title
{
  public:
                                    Title();
                                    Title(const Title& rc);
    virtual                         ~Title();
    Title&                          operator= (const Title& rt);
    const std::string&              get_table_name(void) const;
    const std::string&              get_title_text(void) const;
    void                            set_table_name(const std::string&);
    void                            set_title_text(const std::string& new_text);
};

The CppUnit testing class would look like:

#include "cppunit/extensions/HelperMacros.h"

class Test_Ing_Title
    : public CPPUNIT_NS::TestFixture
{
    //---------------------------------------------------------------------
    //  Friends
    //---------------------------------------------------------------------
    CPPUNIT_TEST_SUITE(Test_Ing_Title);
    CPPUNIT_TEST(ing_name_field_testing);
    CPPUNIT_TEST(copying);
    CPPUNIT_TEST(table_name_testing);
    CPPUNIT_TEST(test_id_field);
    CPPUNIT_TEST(title_testing);
    CPPUNIT_TEST(visitor_test);
    CPPUNIT_TEST_SUITE_END();

    //---------------------------------------------------------------------
    //  Public types
    //---------------------------------------------------------------------
  public:

    //---------------------------------------------------------------------
    //  Public Constructors and Destructors
    //---------------------------------------------------------------------
  public:
    //! Constructor - Default
                                Test_Ing_Title();

    //! Copy Constructor
                                Test_Ing_Title(const Test_Ing_Title& n_obj);

    //! Destructor
    virtual                     ~Test_Ing_Title();

    //---------------------------------------------------------------------
    //  Public Overloaded Operators
    //---------------------------------------------------------------------
  public:

    //---------------------------------------------------------------------
    //  Public Methods
    //---------------------------------------------------------------------
  public:
    //! Test cloning of the title record.
    void                        cloning(void);

    //! Test copying
    void                        copying(void);

    //! Test the name field getters and setters
    void                        ing_name_field_testing(void);

    //! Test the table name getters & setters
    void                        table_name_testing(void);

    void                        tearDown(void);

    //! Test the ID field getters and setters.
    void                        test_id_field(void);

    //! Test the title getters and setters
    void                        title_testing(void);

    //! Test visitors to the title.
    void                        visitor_test(void);
};

An example testing method:

#include <stdafx.h>     // This line is required for precompiled headers on MSVC++.
#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>
#include <string>

void
Test_Ing_Title ::
title_testing(void)
{
    static const char           expected_title_text[] = "Ground Beef";
    const std::string           expected_title(expected_title_text);
    std::string             actual_title;
    Ingredient::Records::Title  ing_title;
    ing_title.set_title_text(expected_title);
    actual_title = ing_title.get_title_text();
    CPPUNIT_ASSERT(actual_title == expected_title);
    return;
}

In the above example, the test class creates an instance of the class to test, then exercises the methods. The key point is that the test class is isolated from the system or put another way, the test class does not influence the system under test. Placing the test code in a separate location will help emphasize this concept. Treat the "embedded" code as read-only. Test to the extremes, hold your ground, the reduced number of product returns will be your reward (and adds to the profit of the company).

HTH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文