[問題] 自己手寫測其他function的code

看板C_and_CPP作者 (改)時間12年前 (2012/03/11 23:51), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/1
有聽說過每個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
Exploring the C++ Unit Testing Framework Jungle
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
文章代碼(AID): #1FNCdmWv (C_and_CPP)