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.
文章来源于领测软件测试网 https://www.ltesting.net/