使用Visual Studio 2010对嵌入式C++代码进行单元测试

Rob*_*Rob 3 c++ embedded visual-studio-2010

我有遗留代码,我将重构/添加.代码是用C++编写的,目标是使用Greenhills编译器的嵌入式设备.我听说Visual Studio 2010有更好的测试框架,编写测试用例所需的工作量更少.是否可以使用VS2010对嵌入式代码进行单元测试?将非常感谢有关如何执行此操作或逐步过程的示例.

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

Tho*_*ews 5

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

我在Visual Studio 2008中使用带有wxWidgets的CppUnit.我没有尝试单独将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);
};
Run Code Online (Sandbox Code Playgroud)

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);
};
Run Code Online (Sandbox Code Playgroud)

一个示例测试方法:

#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;
}
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,测试类创建要测试的类的实例,然后练习方法.关键点在于测试类与系统隔离或以另一种方式隔离,测试类不会影响被测系统.将测试代码放在一个单独的位置将有助于强调这一概念.将"嵌入"代码视为只读.对极端情况进行测试,坚持不懈,减少产品退货数量将是您的回报(并增加公司的利润).

HTH.