1. CppUnit是xUnit系列中的c++实现版本,它是从JUnit移植过来的,第一个移植版本由Michael Feathers完成,安装cppunit,你可以在此下载cppunit的新版本,新版本是CppUnit release 1.12.0,安装方法,现解压,然后到文件夹下找到INSTALL-WIN32.txt(windows平台)
2.打开examples下的examples.dsw,编译链接即可完成。
3.分析所要测试的类class
class Money
{
public:
Money( double amount, std::string currency )
: m_amount( amount )
, m_currency( currency )
{
}
double getAmount() const
{
return m_amount;
}
std::string getCurrency() const
{
return m_currency;
}
bool operator ==( const Money &other ) const
{
return m_amount == other.m_amount &&
m_currency == other.m_currency;
}
bool operator !=( const Money &other ) const
{
return !(*this == other);
}
Money &operator +=( const Money &other )
{
if ( m_currency != other.m_currency )
throw IncompatibleMoneyError();
m_amount += other.m_amount;
return *this;
}
private:
double m_amount;
std::string m_currency;
};
4. 所要测试的有哪些接口呢?
我们分析一下这个类的公开的属性和方法。这些都是我们要测试的接口。
构造函数
Money( double amount, std::string currency )
接口函数有
double getAmount() const
std::string getCurrency() const
bool operator ==( const Money &other ) const
bool operator !=( const Money &other ) const
Money &operator +=( const Money &other )