[問題] 自己手寫測其他function的code
有聽說過每個class最好都要有個測試用的method,
該method用來作Unit test,測試本class中其他的method執行是否如預期
自己亂想的手寫簡易Unit test(不用任何framework)如下:
class CTest
{
public:
CTest()
{
//原本的constructor
m_nA = 1;
}
#ifdef _DEBUG
CTest(int a)
{
//為了作Unit test而加的測試用constructor
//可以帶一些參數,用來改變field的值,觀察其他function的應對是否如預期
m_nA = a;
}
void RunTheTest()
{
//在這裡作Unit test
assert(Add(3, 5) == 8 * m_nA);
assert(Subtract(8, 6) == 2 * m_nA);
...
}
#endif
int Add(int n, int m)
{
return (n + m) * m_nA;
}
int Subtract(int n, int m)
{
return (n - m) * m_nA;
}
private:
int m_nA;
};
void main()
{
CTest myTest;
myTest.RunTheTest();
}
在不用framework的情況下,這樣作unit test是正確的嗎??
除此之外,作Unit test還有要注意甚麼呢?
THANKS
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.240.139.207
推
03/12 02:37, , 1F
03/12 02:37, 1F
→
03/12 02:37, , 2F
03/12 02:37, 2F
→
03/14 22:42, , 3F
03/14 22:42, 3F