【问题标题】:How to create a test run/plan for a C++ Program? [closed]如何为 C++ 程序创建测试运行/计划? [关闭]
【发布时间】:2015-10-30 09:09:35
【问题描述】:

您好,我有一个名为 Date 的 c++ 类。代码如下。如何为班级创建测试运行?我对 C++ 非常陌生。我进行了很多搜索,但找不到任何有效/有用的网站。请问有人可以帮忙吗?提前致谢。

date.h

#ifndef H_date
#define H_date 
#include <iostream> 
using namespace std; 
class Date{
    friend ostream &operator << (ostream & os, const Date &);
    friend istream &operator >> (istream & is, Date &);

public:
    Date();
    Date(int day, int month, int year);
    void setday(int day);
    void setmonth(int month);
    void setyear(int year);
    void setDate(int day, int month, int year);
    int getday() const;
    int getmonth() const;
    int getyear() const;
    void print() const;
protected:
    int day;
    int month;
    int year; 
};
#endif

date.cpp

#include <iostream>
#include "date.h"
using namespace std;
Date::Date()
{
    day = 0;
    month = 0;
    year = 0;
}
Date::Date(int newDay, int newMonth, int newYear)
{
    day = newDay;
    month = newMonth;
    year = newYear;
}
void Date::setday(int newDay)
{
    day =newDay;
}
void Date::setmonth(int newMonth)
{
    month = newMonth;
}
void Date::setyear(int newYear)
{
    year = newYear;
}

void Date::setDate(int newDay, int newMonth, int newYear)
{
    day =newDay;
    month = newMonth;
    year = newYear;
}
int Date::getday()const
{
    return day;

}
int Date::getmonth()const
{
    return month;

}
int Date::getyear()const
{
    return year;

}

void Date::print()const
{
    cout << day << ":" << month<< ":" << year <<endl;
}
ostream& operator<< (ostream& osObject, const Date& date1)
{
    osObject << date1.day
        << ":" <<date1.month
        << ":" << date1.year;
    return osObject;

}
istream& operator>> (istream& isObject, Date& date1)
{
    isObject>>date1.day>>date1.month>>date1.year;
    return isObject;

}

【问题讨论】:

  • 如果您使用特定值调用setdaygetday 是否返回正确的值?测试一个类真的很简单:使用 setter 函数来设置一个特定的值(在你的例子中是数据)并检查 getter 函数是否返回正确的值。对于输入/输出函数,使用std::istringstreamstd::ostringstream
  • “创建测试运行”是指“单元测试”?
  • 你无法想象我多么想免费为其他人编写测试规范。
  • Yes Nayana.. 单元测试

标签: c++ visual-studio visual-studio-2010 oop


【解决方案1】:

对于单元测试,您通常会使用单元测试框架,例如CppUnit

  • 为每个类创建一系列测试;
  • 对于每个要测试的方法,您都开发了一个专门的测试;
  • 每个测试都使用函数或宏来验证一些断言,例如CPPUNIT_ASSERT、CPPUNIT_FAIL、CPPUNIT_ASSERT_THROW;
  • 将测试人员类friend 设置为测试类通常很有用。

这是一个测试构造函数的示例:

void DateUnitTest::ConstructorTests
{
    // Test default-ctor
    { 
        Date date;
        CPPUNIT_ASSERT ( date.day   == 0 );
        CPPUNIT_ASSERT ( date.month == 0 );
        CPPUNIT_ASSERT ( date.year  == 0 );
    }

    // Test ctor with args
    { 
        Date date(31,12,2015);
        CPPUNIT_ASSERT ( date.day   == 31 );
        CPPUNIT_ASSERT ( date.month == 12 );
        CPPUNIT_ASSERT ( date.year  == 2015 );
    }
}

【讨论】:

  • 谢谢丹尼尔。如何运行这个?我已经有一个主程序。我需要为此创建一个不同的项目吗?
  • @Blacky 是的,这是一个单独的项目/一个专用的单元测试程序。这样,您的构建链非常简单:(1)构建单元测试程序(2)运行单元测试程序(3)检查没有错误。如果这 3 个步骤都可以,并且您的测试计划足够详尽,那么您就知道在单元级别上一切正常。您可能需要添加一些进一步的测试(集成、性能和/或压力),但这是第一步 :-)
  • 非常感谢丹尼尔 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2012-04-04
  • 2018-07-06
  • 1970-01-01
  • 2019-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多