Exception testing

exception testing in software testing and exception handling in automation testing
Dr.MasonHanks Profile Pic
Published Date:23-07-2017
Your Website URL(Optional)
Object Oriented Software Development 8. Exceptions, testing and debugging WWW.ThesisScientist.comWriting high quality software  Programs should handle unexpected error conditions in a controlled way  Report or log error as appropriate  Don’t “crash”  Programs should be thoroughly tested  Prove that requirements are met  Program can run without error but still produce incorrect behaviour  Continuous testing of “units” of code and integration of units throughout development WWW.ThesisScientist.comSupport for the programmer  Exception handling in programming language provides a way to deal with error conditions  Unit testing tools provide support for continuous testing  Debugging tools provide support for tracing through code as it runs to diagnose error conditions or incorrect behaviour WWW.ThesisScientist.comRun-time errors  “Anything that can go wrong, will go wrong” (Murphy’s law)  Many things can, and do, go wrong when a computer program runs, for example when it:  attempts to open a file that doesn’t exist  tries to convert a string to a numeric type  performs a calculation which divides by zero  tries to send a message to a null object reference  If error is not handled, program will crash WWW.ThesisScientist.comWhat are exceptions?  In C (and other languages) when a run-time error occurs an exception is generated  Usually say exception is thrown  An exception is an object that contains information about a run-time error  Exception-handling involves writing code which is executed when an exception is thrown  Interrupts normal flow of execution WWW.ThesisScientist.comExceptions examples  SimpleExceptions project  Program.cs  InfoSaver.cs WWW.ThesisScientist.comException handling  This code tries to open a file  Will cause an error if the file does not exist this code opens access to a file (FileStream) and creates an object which can write to the file (StreamWriter) closing the StreamWriter also closes the FileStream WWW.ThesisScientist.comtry-catch block  Wrap code in try block and catch exception  Catch block can print message, log error, etc. WWW.ThesisScientist.comMultiple catch blocks  Exception classes are all derived from Exception  Can catch different exception types  Usually catch most specific first this will catch any exception as all exceptions derive from Exception WWW.ThesisScientist.comSystem resources  Files, database connections, etc are system resources  Should be closed when no longer needed  Exception occurring can prevent code which closes resources being reached  Resources not released back to system  Files can become unavailable, system can become unstable, etc.  Need to clean up resources after exceptions WWW.ThesisScientist.comCleaning up afterwards could get exception while writing data even of file opens successfully close file normally – this code ... might not be reached finally block will always execute need to declare resource outside try block so that it is in scope in finally block WWW.ThesisScientist.comShould we catch exception immediately?  Wrapping code in try-catch means exception is handled immediately  This will prevent program crashing, but may not be the most useful behaviour  For example, does the user need to know about the error?  We don’t necessarily want to suppress error  Can opt to handle exception elsewhere, e.g. in user interface code WWW.ThesisScientist.comExceptions in class library code  Class library code may be used by many different programs  Each program may have its own way of making users aware of error situations or of logging errors  Class library code should not usually handle exceptions completely  Should pass them onto program to handle WWW.ThesisScientist.comOptions for handling an exception  Handle the exception condition immediately  Pass on the unhandled exception to the calling class  Handle and rethrow the exception to be handled by the calling class  Throw a new exception to be handled by the calling class WWW.ThesisScientist.comPassing on an exception method in class, does not handle exception calling method, in Main class, handles exception and reports it to user WWW.ThesisScientist.comExceptions and the call stack If no suitable exception handler is found, program will exit due to unhandled exception WWW.ThesisScientist.comThrowing a new exception may want to log exception then create a new exception object to pass on to calling class WWW.ThesisScientist.comCleaning up (again)  Difficult for calling class to clean up resources used by called method  Can use try-finally in called method  Doesn’t catch exception, but finally block will always execute even if exception occurs  More convenient shorthand for this in C with using key word  Other languages have similar constructs WWW.ThesisScientist.comCleaning up with using resources opened here will be closed properly even if exception occurs inside using block, as long as resource class implements IDisposable interface WWW.ThesisScientist.comDon’t overuse exceptions  Handling exceptions incurs overhead, so only use exceptions to deal with exceptional situations  a state that occurs infrequently or unexpectedly http://blogs.msdn.com/b/cdndevs/  Check for predictable error archive/2009/08/04/exceptions- the-airbags-of-code.aspx conditions  for example, check whether the number is zero before dividing WWW.ThesisScientist.com