我有要重构/添加的遗留代码。该代码是用C++编写的,目标是使用Greenhills编译器的嵌入式设备。我听说Visual Studio2010有更好的测试框架,编写测试用例需要的工作更少。可以使用VS2010对嵌入式代码进行单元测试吗?关于如何做的例子或逐步程序将非常感谢。
如果没有,CppUnit/CxxTest如何与VS2010集成?我找不到关于如何实现这一点的明确文档。
发布于 2011-03-26 02:27:29
您可以在Visual Studio中编译嵌入式软件。为特定于平台的功能和Green Hills的特定功能创建存根。还要注意任何特定的Green Hills宏。您需要查找或创建等效项。
我在Visual Studio2008和wxWidgets中使用CppUnit。我没有尝试将CppUnit与Visual Studio2010单独集成。
请记住,您希望将验证代码与嵌入式代码分开。嵌入的代码应该作为库和头文件导入到验证项目中。这使测试保持诚实(尽管我知道您可能无法将Green Hills库与Visual Studio库链接起来)。否则,使用VS构建嵌入式代码,但在构建步骤中将其作为一个单独的库。
如果可能,围绕软件需求设计测试。之后,设计一些代码来执行公共函数。请记住,开发人员不应该编写代码来满足测试,测试人员也不应该编写代码来满足开发人员的功能。如果存在,请对其进行测试。或者“如果它被使用了,测试它”。例如,平台可能有一个DMA控制器,但不使用它。
编辑#1 --示例
在"embedded“或实现中给出以下类:
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。
https://stackoverflow.com/questions/5434769
复制相似问题