top of page
Writer's pictureDavid Briffa

Chapter 47 Testing

Updated: Dec 11, 2023

We briefly touched on testing in Chapter 42 when we talked about Systems Analysis. If you covered this topic then you might remember that developers need to perform testing activities as they are coding. This kind of testing happens before users test the product. In this post we will be covering testing as performed by developers.

Why do developers test programs?


Developers perform testing with the intention of finding technical mistakes or errors in the code. An error takes the form of an unexpected event that causes the computer to not function properly. Various types of errors can occur during coding. You probably have come across these during the practical sessions.


3 Error Types: Syntax, Logical and Run-time


Syntax Error

When we use a programming language to do something we need to make sure we type the words in a particular way. When we do not, Visual Studio or BlueJ alert us about syntax errors. Syntax errors are simply 'typos' in the code, the image hereunder gives a few examples.

A program with syntax errors cannot be compiled. Below is a screenshot of Visual Studio when I try to run a program that has syntax errors.

Does this sound familiar now? These kind of errors are very easy to spot but very easy to fix, especially by your teacher.


Logical Error

When there is a logical error the program works but the output it produces is wrong. This can happen when the developer made a mistake in the reasoning. This image has a logical error, can you spot it?


The developer created a method that should add two numbers but the operator being used is an '*' instead of a '+'. Even though the program will work, the result will not be correct! When 5 and 2 are passed in, the method will return 10, but the desired outcome is actually 7.


These types of errors are sneaky and difficult to spot at times.


Run-time Error


This is an error that happens while the program is running and normally causes the program to stop abruptly, or in other words crash 💣. These errors are not always obvious mistakes, sometimes the developer just did not expect something to happen.


The code snippet below can cause a run-time in a particular case... do you know what can go wrong?

Yes, you guessed it. In the case that num2 is 0, then a division by zero run-time error will occur as shown below. In Java run-time errors are generally referred to as exceptions.

Experienced programmers try to prevent these sort of errors from happening by doing simple validation checks:

Or wrapping potentially radioactive code in a try...catch statement.


Dealing with obscure errors or preventing them


Prevention is better than cure but dealing with bugs is part of the job and it is not avoidable.

For this reason, developers have many tools at their disposal in order to deal with unexpected events.


Manual Tests


These are the kind of tests that you are used to performing during our lessons. Since we wrote the program, we know what is expected. Therefore, we simply run the program manually, as though we are a user, to see if it meets expectations with the test data that is inputted.

Feature Name

Add past paper

​Test Data

​1, Computing, 2019, 1, A

Automated Tests


Manual tests are effective and it is a feasible method for small programs, but for much larger programs that is made up of many modules, developers write programs to test. You might think that it is duplicate work but more often than not we use special tools to make our lives easier. In this software testing method developers script specific sequences that are executed with test data that is stored in the program. When the test program is run, it generates a report of all the tests that passed (P) or failed (F).


This is an exercise I normally with IB students since it is a bit advanced.




​Code with stored test data

​The output when test is run.


Debugging in Visual Studio Code


At times it is really helpful to see an error occur during execution but computers are so fast! Most IDEs come with a debugger so that you can see a program run slowly and the outcome at each and every step. If the program is too difficult to debug with a tool then normally the program maintains logs to describe what is happening.


Non-tech alternatives


Logical errors can be very tricky to resolve even with a debugger and logs. At times there is only one thing we can do and that is to go back to the drawing board. To ensure that the decisions we make are better we perform dry runs using pen and paper. A dry-run is a manual way of doing program tracing to examine how variable values change in an algorithm.


131 views0 comments

Comments


bottom of page