Post

C# - Recap of Testing, Debugging and Exceptions

Testing

  • Software testing categories can be organized under the types of testing, the approaches to testing, or a combination of both.
  • One way to categorize the types of testing is to split testing into Functional and Nonfunctional testing.
  • The functional and nonfunctional categories each include subcategories of testing.
  • For example, functional and nonfunctional testing could be divided into the following subcategories:

  • Functional testing

    • Unit testing
    • Integration testing
    • System testing
    • Acceptance testing
  • Nonfunctional testing
    • Security testing
    • Performance testing
    • Usability testing
    • Compatibility testing

Although most developers probably don’t consider themselves to be testers, some level of testing is expected before a developer hands off their work. When developers are assigned a formal role in the testing process, it’s often at the level of unit testing.

Code debugging and developer responsibilities

  • Code debugging is a process that developers use to isolate an issue and identify one or more ways to fix it.
  • The issue could be related to either code logic or an exception.
  • Either way, you work on debugging your code when it isn’t working the way you want it to.
  • Generally speaking, the term debugging is reserved for runtime issues that aren’t easy to isolate.
  • Therefore, fixing syntax issues such as a missing “;” at the end of a code statement, isn’t normally considered debugging.
  • Code debugging is definitely a developer responsibility.

The Code Debugging Process

  • The code debugging process is a systematic approach to identifying and fixing issues in your code.
  • Here are some approaches that are NOT recommended when faced with a bug:
    • Reading through your code (just one more time) hoping that this time the issue jumps out at you.
    • Breadcrumbing a few Console.WriteLine(“here”) messages in your code to the track progress through your app.
    • Rerunning your app with different data. Hoping that if you see what works, you’ll understand what doesn’t work.
  • You might have experienced various degrees of success with these methods, but don’t be fooled. There is a better way.

Working with a debugger

  • A debugger is a software tool used to observe and control the execution flow of your program with an analytical approach.
  • Debuggers help you isolate the cause of a bug and help you resolve it.
  • A debugger connects to your code using one of two approaches:
    • By hosting your program in its own execution process.
    • By running as a separate process that’s attached to your running program.
  • Debuggers come in different flavors.
  • Some work directly from the command line while others come with a graphical user interface.
  • Visual Studio Code integrates debugger tools in the user interface.
  • Every debugger has its own set of features.
  • The two most important features that come with almost all debuggers are:
    • Control of your program execution. You can pause your program and run it step by step, which allows you to see what code is executed and how it affects your program’s state.
    • Observation of your program’s state. For example, you can look at the value of your variables and function parameters at any point during your code execution.
  • Mastering the use of a code debugger is an important skill. Unfortunately, it’s a skill that developers often overlook.
  • Effective use of a debugger helps you to be more efficient at hunting bugs in your code. Debuggers can also help you to understand how a program works.

Exception handling and developer responsibilities

  • Errors that occur during the application runtime are referred to as exceptions.
  • If an application generates an exception, and that exception isn’t managed in code, it can result in the application being shut down.
  • Handling exceptions is definitely a responsibility of the developer.
  • C# provides a way for you to “try” the code that you know might generate an exception, and a way for you to “catch” any exceptions that do occur.
  • Exceptions are used in C# to propagate errors at runtime, and are represented by classes derived from the Exception class.
  • Exceptions are thrown by code that encounters an error and caught by code that can correct the error.
  • When an exception is caught, code can access its contents and take corrective action to mitigate the error.
  • The .NET runtime generates exceptions when it detects an error and the exception contains information about the type of error that occurred.
This post is licensed under CC BY 4.0 by the author.