Vous êtes sur la page 1sur 4

Software Testing Lab 1

White Box Testing


//
// Find the first instance in "string" of each character in "what"
// Return the locations of the first instance of each what[i] in
where[i]
//
ignoring the first "start" characters in string
// Return TRUE if any character found
// All strings are NULL-terminated
//
#include <string.h>
#include "findx.h"
int findx( char *string, int start, char *what, int where[] )
{
int
int
int
int
int

i = 0;
len = strlen(string);
any = 0;
found = 0;
j;

/* index into what[] */


/* any character found = FALSE */
/* current character found = FALSE */
/* index into string[] */

char c=what[0];
while (c != '\000') {
found = 0; /* FALSE */
j = start;
do {
if (string[j++] == c) {
found = 1; /* TRUE */
any = 1; /* TRUE */
}
} while ( (!found) && (j < len) );
if (found)
where[i] = j-1;
else
where[i] = len;
c=what[++i];
}
return any;
}

Consider the program findx.cc on the previous page. Develop the following
tests for this program
1.
2.
3.
4.
5.
6.
7.

Statement coverage
Branch coverage
D-D Path Testing
Condition Coverage Testing
Decision/Condition Coverage Testing
Multiple Condition Coverage Testing
Path Testing

Ensure that you document your tests as you develop them:

Write down the test cases


Write down the test data

CPPunit Example Just for your own reference in the future


The following example is of a simple program and CPPunit test suite to test
that program. Type in the code and run it to observe how the tests are
engineered. We will use the program findx.cc again, but it should be saved as
a .cpp file. The file findx.h contains
int findx( char *string, int start, char *what, int *where );

The syntax for a CPPunit test is


runtest <SUT> {<SUITE>}

where SUT is the nema of the software under test (including version
number) and {<SUITE>} is the (optional) name of the test suite to run: the
default being all tests. The name of this program is finderTest.cpp
#include
#include
#include
#include
#include
#include
#include

<extensions/TestFactoryRegistry.h>
<extensions/HelperMacros.h>
<TestCase.h>
<Exception.h>
<Asserter.h>
<TestAssert.h>
<ui/text/TestRunner.h>

using namespace CppUnit;


#include
#include
#include
#include

"stdafx.h"
<iostream>
<stddef.h>
<time.h>

#include <string.h>
#include "findx.h"

//

first start a test suite that defines the methods to call

class finderTest : public CppUnit::TestFixture {


CPPUNIT_TEST_SUITE( finderTest );
CPPUNIT_TEST( test1 );
CPPUNIT_TEST( test2 );
CPPUNIT_TEST_SUITE_END();
// end the test suite
private:
// put private attributes required for testing here
public:
// put your own test methods here - one per test case or test ID
is usual
// use CPPUNIT_ASSERT() to check results
// Test1 this test should be passed
void test1()
{
int rv;
int where[6];
rv = findx( "abccde", 0, "c", &where[0] );
CPPUNIT_ASSERT( rv == 1 ); // program resturns true
CPPUNIT_ASSERT( where[0] == 2 ); //char c located at index 2
}
// Test2

- this test should fail

void test2()
{
int rv;
int where[6];
rv = findx( "abccde", 0, "x", &where[0] );
CPPUNIT_ASSERT( rv == 1 );
CPPUNIT_ASSERT( where[0] == 2 );

}
};
// the following line adds this test suite to the test factory
CPPUNIT_TEST_SUITE_REGISTRATION( finderTest );
// main program for CPPunit
int main( int argc, char *argv[] )
{
bool success;
TextUi::TestRunner runner;

TestFactoryRegistry &registry = TestFactoryRegistry::getRegistry();


char *testname;
char *sut;
time_t now=time(0);
// run all the tests if no params
if (argc==1) {
sut = "finder";
testname = "finderTest";
}
// print the test info
// default to all tests if none specified
std::cout << "Test log at " << ctime(&now) << std::endl;
std::cout << "Running cppunit tests for " << sut << std::endl;
std::cout << "Test suite selected: " << testname << std::endl;
// add all the unit tests from the registry to this runner
runner.addTest( registry.makeTest() );
// and run the (selected) tests
success = runner.run( testname );
// return 0 on success, and 1 on failure (for make)
if (success)
return 0;
else
return 1;
}

Type in this code and run this example. Then, add some more suitable test
cases, say 3 or 4 more, to augment the testing exercise.

Vous aimerez peut-être aussi