4 BITs重用研究案例
一個二分查找的 BIT對象的如標記B所示, 該BIT對象分為兩部分:一般的方法(成員函數)和 BIT方法(函數)。該BIT對象中通過內建一個測試用例來說明BIT s方法。
在常態模式下,標記B中普通的方法被調用:BITsBinarySearch::BinarySearch(int Key, int DataSet[10])。
在測試模式下,內嵌的BIT成分通過調用 BITsBinarySearch::BIT1()被重用.
Class BITsBinarySearch {
///////////////////////////////////////////////////////////////////////////// //
Interface
///////////////////////////////////////////////////////////////////////////// //
Member functions
BITsBinarySearch(); // The constructor
~BITsBinarySearch(); // The destructor
int BinarySearch (int Key; int DataSet[10]); // The conventional object
void BIT1(); // The built-in-tests 1...3
///////////////////////////////////////////////////////////////////////////
// Implementation
///////////////////////////////////////////////////////////////////////////
// ================================
// Part 1: The conventional function code
// ================================
int BinarySearch (int Key, int DataSet[10])
{
// The conventional object
// Assume: DataSet is ordered
// LastElement -FirstElement >=0
// and FirstElement >=0
// Input: Key to be found in the DataSet
// Output: TestElemIndex
Private:
int bott, top, i;
int found;
found = false;
Bott = 1;
Top = ArraySize (DataSet); // The last element in DataSet
while (bott <= top) && (not found)
{
i = floor ((bott + top)/2));
if DataSet[i] == Key
Found = true;
else if DataSet[i] < Key
Bott = i +1
else Top = i +1;
}
if found == true
return i; // The index of the element
else return 0; // An indicator of not existence
}
// ==================================
// Part 2: The BITs
// ==================================
// BIT case 1
// ---------------------------------------------------------
void BIT1()
{
// BIT case 1: Array size of 1, key in array
private:
int DataSet[1] = {16};
int Key = 16;
int StdElemIndex = 1;
int TestElemIndex;
char TestResult1[5];
// Test implementation
TestElemIndex = BinarySearch (Key, DataSet);
// Test analysis
cout << “StdElemIndex1 = ” << StdElemIndex << “\n”;
cout << “TestElemIndex1 = ” << TestElemIndex << “\n”;
if TestElemIndex == StdElemIndex
TestResult1 = “OK”;
else TestResult1 = “FALSE”;
cout << “TestResult1: ” << TestResult1 << “\n”;
}
}
標記B 二分查找BIT對象
在維護中應用BIT成分的途徑如標記C所示。一個新對象-DatabaseQuery被開發出來,在BITsBinarySearch中的BIT成分能夠如同普通方法(成員函數)一樣被繼承和重用。此外,新加入的BITs也能夠被添加到如標記C所示的新對象中。
Class DatabaseQuery: public BITsBinarySearch
{
//////////////////////////////////////////////////////////////////////////////
// Part 1: The inherited conventional functions
//////////////////////////////////////////////////////////////////////////////
int DatabaseQueryBinarySearch (int Key, int DataSet[10]) :
BITsBinarySearch::BinarySearch(int Key; int DataSet[10]);
//////////////////////////////////////////////////////////////////////////////
// Part 2: The inherited BIT functions
//////////////////////////////////////////////////////////////////////////////
void BIT1() : BITsBinarySearch::BIT1();
//////////////////////////////////////////////////////////////////////////////
// Part 3: The newly developed BITs
//////////////////////////////////////////////////////////////////////////////
// BIT case 2
// ---------------------------------------------------------------
void BIT2()
{
// BIT case 2: Even array size, key 1st element in array
Private:
int DataSet [6] = {16,18,21,23,29,33};
int Key = 16;
int StdElemIndex = 1;
int TestElemIndex;
char TestResult2 [5];
// Test implementation
TestElemIndex = BinarySearch (Key, DataSet);
// Test analysis
cout << “StdElemIndex2 = ” << StdElemIndex << “\n”;
cout << “TestElemIndex2 = ” << TestElemIndex << “\n”;
if TestElemIndex == StdElemIndex
TestResult4 = “OK”;
else TestResult2 = “FALSE”;
cout << “TestResult2: ” << TestResult2 << “\n”;
}
標記C DatabaseQuery對象
新BIT對象 DatabaseQuery中,源至BITsBinarySearch 對象的BITs方法依然通過DatabaseQuery::BIT1()(等同于BITsBinarySearch::BIT1)被激活。DatabaseQuery中新的BITs方法以同樣的方式DatabaseQuery::BIT2()得到激活。
5 結論
本文提出了一種融合可復用內建測試(BITs)的面向對象框架設計方法。并分析了面向對象框架中BITs的重用途徑。BIT方法大大擴展了面向對象框架從代碼到測試的可重用性。因此高度可測試和測試可復用的面向對象框架能夠在現有的基于普通的面向對象框架上被開發出來。
BIT方法是普通面向對象框架技術的補充和延展。BIT方法在面向對象的分析、設計、編碼中得到了廣泛的應用。BIT方法能夠融合任何利用黑盒方法和/或白盒方法產生的測試用例并以面向對象框架組件方式存在。
感謝:
本文作者在本文的寫作中得到了European Software Institute和IVF Centre for Software Engineering兩個機構的支持,我們在此還要感謝他們對此提出有價值的評論。
參考文獻:
[1] Fayad, M. and Schmidt, D. [1997], Object-Oriented Application Frameworks, Communications of the ACM, Vol.40, No.10, October.
[2] Buschmann, F., R. Meunier, H. Rohnert, P. Sommerlad and M. Stal [1996], Pattern-Oriented Software Architecture - A System of Patterns, Wiley, New York, 1996.
[3] Jacobson, I. M. Christerson, P. Jonsson and G. Overgaard [1992], Object-Oriented Software Engineering, Addison-Wesley.
[4] Schmidt, D.C. [1995], Using Design Patterns to Develop Reusable Object-Oriented Communication Software, Communications of the ACM, Vol.38, No.10, pp.65-74.
[5] Binder, R.V. [1994], Design for Testability in Object-Oriented Systems, Communications of the ACM, Vol. 37, No. 9, Sept., pp. 87-101.
[6] McGregor, J.D. and T.D. Korson [1994], Integrating Object-Oriented Testing and Development Processes, Communications of the ACM, Vol. 37, No. 9, Sept., pp. 59-77.
[7] Jorgensen, P.C. and Erickson, C. [1994], Object-Oriented Integration Testing, Communications of the ACM, Vol. 37, No. 9, Sept., pp. 30-38.
[8] Poston, R.M. [1994], Automated Testing from Object Models, Communications of the ACM, Vol. 37, No. 9, Sept., pp. 48-58.
[9] Murphy, G.C., P. Townsend and P. S. Wong [1994], Experiences with Cluster and Class Testing, Communications of the ACM, Vol. 37, No. 9, Sept., pp. 39-47.
[10] Arnold, T.R. and W.A. Fuson [1994], Testing “in a Perfect World”, Communications of the ACM, Vol. 37, No. 9, Sept., pp. 78-86.
[11] Freedman, R.S. [1991], Testability of Software Components, IEEE Transactions on Software Engineering,Vol.17, No.6, June, pp.553-564.
[12] Voas, J.M. and Miller, K.M. [1995], Software Testability: The New Verification, IEEE Software, Vol.12, No.3, May, pp.17-28.
[13] Firesmith, D.G. [1994], Testing Object-Oriented Software, Proceedings of Object Expo. Europe, Sept., pp. 97 - 130.
[14] Wang, Y. [1988], Testability Theory and Its Application in the Design of Digital Switching Systems, Journal of Telecommunication Switching, Vol.17, pp.30-36.
[15] Wang, Y., Court, I., Ross, M., Staples, G. and King, G. [1997], On Testable Object-Oriented Programming, ACM Software Engineering Notes, July, Vol.22, No.4, pp.84-90.
[16] Harrold, M.J., J.D. McGregor and K.J. Fitzpatrick [1991], Incremental Testing of Object-Oriented Class Structures, Proceedings of the 14th International Conference on Software Engineering, March.
[17] Wang, Y., King, G., Court, I., Ross, M. and Staples, G. [1997], On Built-in Tests in Object-Oriented Reengineering, Proceedings of 5th ACM Symposium on FSE/6th European Conference on Software Engineering/Workshop on Object-Oriented Reengineering (FSE/ESEC/WOOR’97), pp. 3.6.1-3.6.5.
[18] Stroustrup, B. [1986], The C++ Programming Language, Addison-Wesley publishing Company.
文章來源于領測軟件測試網 http://www.kjueaiud.com/