Producing robust programs - OCRSelecting and using suitable test data

Programs must run correctly or they are of little value. Careful planning and testing of a program are essential, as is writing maintainable code.

Part ofComputer ScienceComputational thinking, algorithms and programming

Selecting and using suitable test data

Test data is that is used to test whether or not a is functioning correctly. Ideally, test data should cover a range of possible and impossible , each designed to prove a program works or to highlight any flaws. Three types of data are:

  • - sensible, possible data that the program should accept and be able to process
  • - valid data that falls at the boundary of any possible ranges
  • - data that the program cannot process and should not accept

Testing requires a . This is a list of all the tests that the programmer intends to use to ensure the program functions as intended. It should include several examples of valid, extreme and invalid data.

Testing tables

Tests are laid out in a testing table, which indicates:

  • the test number
  • a description of what the test intends to check
  • the test data being used
  • the type of test (valid, extreme or invalid)
  • expected outcome
  • actual outcome

Consider this simple program which asks a user to input a number from 1 to 10:

valid = FALSE
while valid == FALSE number = int(input("Enter a number from 1 to 10")) if number <1 OR number >10 then print("Number outside the range 1 to 10. Enter another number") else valid = TRUE
endwhile
print("Number entered is ", number)

This program could be tested using the following valid, extreme and invalid data:

Test noDescriptionTest dataTest typeExpectedActual
1Test that a possible number is accepted5ValidData is acceptedData is accepted
2Test the lower boundary1ExtremeData is acceptedData is accepted
3Test the upper boundary10ExtremeData is acceptedData is accepted
4Test that the program does not accept a number less than 1-5InvalidData is not acceptedData is not accepted
5Test that the program does not accept a number greater than 1020InvalidData is not acceptedData is not accepted
Test no1
DescriptionTest that a possible number is accepted
Test data5
Test typeValid
ExpectedData is accepted
ActualData is accepted
Test no2
DescriptionTest the lower boundary
Test data1
Test typeExtreme
ExpectedData is accepted
ActualData is accepted
Test no3
DescriptionTest the upper boundary
Test data10
Test typeExtreme
ExpectedData is accepted
ActualData is accepted
Test no4
DescriptionTest that the program does not accept a number less than 1
Test data-5
Test typeInvalid
ExpectedData is not accepted
ActualData is not accepted
Test no5
DescriptionTest that the program does not accept a number greater than 10
Test data20
Test typeInvalid
ExpectedData is not accepted
ActualData is not accepted

Ideally, a programmer should run as many tests as is sensible. Many large programs, especially games, contain simply because it may not be possible to test every possible input or action.

Refining algorithms

After an algorithm has been tested, it may need to be refined to improve how it works and to make it efficient. This is a part of the testing life cycle. Refining an algorithm helps to ensure that it is as good as it can be, before being used in a live and active system. Refining an algorithm requires changes that:

  • fix problems that are found during testing
  • make the algorithm more efficient by reducing any unnecessary or repetitive steps
  • improving readability and maintainability so that others can understand the algorithm.