Chapter 12 - Software Debugging
- Authors
- Ms. Samavi Salman
- Dr. Rao Muhammad Adeel Nawab
- Supporting Material
Quick Recap
- Quick Recap – Software Testing
In previous Chapter, I presented
- Testing
- Testing is the process of evaluating a system or its component(s) with the intent to find whether it satisfies the specified requirements or not
- In simple words, testing is executing a system to identify any gaps, errors, or missing requirements in contrary to the actual requirements
- Software Testing
- Software Testing is the process of verifying a system with the purpose of identifying any errors, gaps, or missing requirement versus the actual requirement
- Formal Definition (ANSI/IEEE 1059 standard)
- A process of analyzing a software item to
- Detect the differences between existing and required conditions (i.e., defects)
- Evaluate the features of the software item
- A process of analyzing a software item to
- Types of Software Testing
- Software Testing can be categorized in multiple ways
- Automated Software Testing vs Manual Software Testing
- Software Testing can be categorized in multiple ways
- Manual Software Testing Method
- In Manual Software Testing Method, a Software is manually tested by a Human (Domain Expert)
- Automatic Software Testing Method
- Automation testing is the process of testing the software using an automation tool to find the defects
- Software Testing Life Cycle
- STLC stands for Software Testing Life Cycle. STLC is a sequence of different activities performed during the software testing process
- unitTest Tool
- The unittest module provides classes that make it easy to support these qualities for a set of tests
- The principles of unittest are easily portable to other frameworks
Bugs
- Bugs
- Definition
- A bug refers to an error, fault or flaw in any computer program or a hardware system
- A bug identified needs to be tracked and fixed to ensure optimum quality in the software/system being developed
- Main Reasons for Bug(s) in a Software
- There are bugs in software due to
- Unclear or constantly changing requirements
- Software complexity
- Programming errors
- Timelines
- Errors in bug tracking
- Communication gap
- Documentation errors
- Deviation from standards
- There are bugs in software due to
- Importance
- It can be used to describe an
- Error
- Flaw
- failure or
- fault in a computer program
- It can also be used to describe
- unexpected behavior even if the software is working
- It can be used to describe an
- Impact of Bug
- The impact of the Bug is measured based on Severity and Priority
- Impact to business is measured in terms of Severity
- Critical
- High
- Medium
- Low
- Impact to business execution is measure in terms of Priority
- Immediate
- High
- Medium
- Low
- Bug Life Cycle / Defect Life Cycle
- Following are the steps involved in Bug Life Cycle
- New
- When a defect is posted, the default status is New
- Open
- When the defect is accepted by developers it is moved to the Open status
- Rejected
- When the defect is rejected by developers it is moved to the Rejected status
- Fixed
- When the defect is fixed by developers it is moved to the Fixed status. Testers would pick up all defects for testing that are in the status Fixed
- Reopen
- If the testing has failed, the defect is moved to Reopen status
- Closed
- If the testing has passed, the defect is moved to Closed status
- New
- Following are the steps involved in Bug Life Cycle
- Parameters of a Bug
- The Following details should be part of a Bug:
- Date of issue, author, approvals, and status
- Severity and priority of the incident
- The associated test case that revealed the problem
- Expected and actual results
- Identification of the test item and environment
- Description of the incident with steps to reproduce
- Status of the incident
- Conclusions, recommendations, and approvals
- The Following details should be part of a Bug:
- Disadvantages
- The main disadvantage is that developers, customers, and product managers locate it difficult to maintain which bug is fixed in which release
- Source of Bugs
- Ambiguous Requirements
- This can be avoided by having strong Static Testing techniques, Reviews, Walkthroughs, etc.
- Programming Errors
- Incorrect coding standards is a source which can be minimized with proper code review and unit testing
- Unachievable Deadlines
- The main disadvantage is that developers, customers, and product managers locate it difficult to maintain which bug is fixed in which release
- Reasons of Bug
- There are bugs in software due to unclear or constantly changing requirements, similarly software complexity, programming errors, timelines, errors in bug tracking, communication gap, documentation errors, deviation from standards etc.
- Miscommunication or No Communication
- Software Complexity
- Programming Errors
- Changing Requirements
- Time Pressures
- Egotistical or Overconfident People
- Poorly Documented Code
- Software Development Tools
- Obsolete Automation Scripts
- Lack of Skilled Testers
- Types of Bugs
- Following are the types of bugs
- Syntax bugs
- These refer to occurrences where a user incorrectly places characters within a command, thus preventing proper command execution
- Runtime bugs
- These cause errors while the program is running. It can be a logic error where the program produces incorrect output
- Logic bugs
- These cause errors in script functions. An example would be when a command results in a different function, arriving at the wrong output
- Arithmetic bugs
- These occur due to an integer overflow or underflow and cause an arithmetic operation to result in a numeric value beyond the given range
- Syntax bugs
- Following are the types of bugs
- Example
- Communication Error (User Interface)
- Redirection of Application
- Breakdown of Application
- Responsiveness of Web Page
- Browser Capabilities
- Validation Fields
Error
- Error
- Definition
- A mistake in coding is called Error
- Purpose
- It may be system specification issue or design issue or coding issue, which leads to a defect.
- Error leads to defects and if the defect uncovered by QA leads to Failure
Defect
- Defect
- Definition
- A defect is a system error that does not allow the intended action to be completed
- Purpose
- Finding defects is the tester’s most important task. It is important to start testing as early as possible because defects can be found throughout the entire software development process
- Defect Status
- Definition
- Defect Status or Bug Status in defect life cycle is the present state from which the defect or a bug is currently undergoing
- Purpose
- The goal of defect status is to precisely convey the current state or progress of a defect or bug to better track and understand the actual progress of the defect life cycle
- Defect Life Cycle
- New
- Potential defect that is raised and yet to be validated
- Assigned
- Assigned against a development team to address it but not yet resolved
- Active
- The Defect is being addressed by the developer and investigation is under progress
- At this stage there are two possible outcomes
- Deferred
- Rejected
- Test
- The Defect is fixed and ready for testing
- Verified
- The Defect that is retested and the test has been verified by QA
- Closed
- The final state of the defect that can be
- closed after the QA retesting
- closed if the defect is duplicate
- considered as NOT a defect
- The final state of the defect that can be
- Reopened
- When the defect is NOT fixed, QA reopens/reactivates the defect
- Deferred
- When a defect cannot be addressed in that particular cycle it is deferred to future release
- Rejected
- A defect can be rejected for any of the 3 reasons
- duplicate defect
- NOT a Defect
- Non-Reproducible
- A defect can be rejected for any of the 3 reasons
Types of Errors
- Types of Errors
- Missing/misplaced semicolons
- Variable type mismatches
- Incorrect passing of arguments
- Undefined or out of scope variables
Introduction to Software Bugs
- Software Bug
- Definition
- A software bug is an error, flaw or fault in a computer program or system that causes it to produce an incorrect or unexpected result, or to behave in unintended ways
- A software bug may be defined as a coding error that causes an unexpected defect, fault, flaw, or imperfection in a computer program. In other words, if a program does not perform as intended, it is most likely a bug.
- Main Reason of Software Bugs in Software
- Following are the reasons of occurrence of bugs in a Software:
- Software bugs may cause the software to completely lock up or crash, but may also cause erroneous output without crashing the software
- Bugs originate when the test cases are executed.
- Wrong coding
- Missing coding
- Extra coding
- The most common reason is human mistakes in software design and coding
- Miscommunication or No Communication
- Software Complexity
- Programming Errors
- Unclear or constantly changing requirements
- Time Pressures
- Poorly Documented Code
- Software Development Tools
- Lack of Skilled Testers
- Obsolete Automation Scripts
- Errors in bug tracking
- Documentation errors
- Deviation from standards
- Occurrence of Bug
- A software bug usually occurs when the software does not do what it is intended to do nor does something that it is not intended to do
- Flaws in specifications, design, code, or other reasons can cause these bugs
- Impact of Software Bugs on Software Performance
- Software performance is critical for how users perceive the quality of software products
- Performance bugs
- programming errors that cause significant performance degradation led to poor user experience and low system throughput
- Designing effective techniques to address performance bugs requires a deep understanding of how performance bugs are discovered, reported, and fixed
- Bugs may have subtle effects or cause the program to crash or freeze the computer
- Identifying and fixing bugs in the early stages of the software is very important as the cost of fixing bugs grows over time
- Performance bugs
- Types of Software Bugs
- Three main types of Software Bugs are
- Compile time Bugs
- Most of the compile time errors can be overcome with careful attention to language syntax and use. Finding syntax errors is the first step in a debugging process
- Run time Bugs
- Run time errors can cause program to crash, or give you the incorrect output. First step in dealing with run time errors is to remove reasons for program to crash.
- Compile time Bugs
Syntax Bug
- Example 1 - Syntax Bug
'''
__author__ = Ms. Samavi Suleman
__copyright__= Copyright I 2020 Ms. Samavi Suleman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main Purpose of the Program is to take two Numbers (Integers), calculate sum of Integers and display the sum on the Output Screen
'''
'number1' = 10
'number2' = 20
sum = number1 + number2
print(sum)
Analysis, Main Findings and Conclusions
- Analysis – Where is the Bug?
- The initialization of variable in the above program is syntactically incorrect
- Reason for Bug
- Because ‘number1’ and ‘number2’ are String Variables however values assigned to them are Integers
- Example 2 - Syntax Bug
'''
__author__ = Ms. Samavi Suleman
__copyright__= Copyright I 2020 Ms. Samavi Suleman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main Purpose of the Program is to take two Numbers (Integers), calculate sum of Integers and display the sum on the Output Screen
'''
number1 = 10
number2 = 20
sum = number1 + number2
print(sum
Analysis, Main Findings and Conclusions
- Analysis – Where is the Bug?
- The syntax of print() Statement in the above program is incorrect
- Reason for Bug
- Because ‘print(sum’ is lacking the ending bracket
- Example 3 - Syntax Bug
'''
__author__ = Ms. Samavi Suleman
__copyright__= Copyright I 2020 Ms. Samavi Suleman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main Purpose of the Program is to take two Numbers (Integers), calculate sum of Integers and display the sum on the Output Screen
'''
number1 = 10
number2 = 20
print('number1' + number2')
Analysis, Main Findings and Conclusions
- Analysis – Where is the Bug?
- The syntax of print() Statement in the above program is incorrect
- Reason for Bug
- Because ‘ print(‘number1′ + number2′)’ is lacking the starting quote
- Example 4 - Syntax Bug
'''
__author__ = Ms. Samavi Suleman
__copyright__= Copyright I 2020 Ms. Samavi Suleman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main Purpose of the Program is to print a message on the Output Screen
'''
message = 'don't'
print(message)
Analysis, Main Findings and Conclusions
- Analysis – Where is the Bug?
- The value assigned to Variable message message = ‘don’t’ is incorrect
- Reason for Bug
- The escape character is missing message = ‘don’t’. It must be like don\’t
- Example 5 - Syntax Bug
'''
__author__ = Ms. Samavi Suleman
__copyright__= Copyright I 2020 Ms. Samavi Suleman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main Purpose of the Program is to take an Integer number from the User and display whether the number if even or odd on the Output Screen
'''
number = int(input('Enter a number: '))
whille number % 2 == 0:
print('You have entered an even number.')
else:
print ('You have entered an odd number.')
Analysis, Main Findings and Conclusions
- Analysis – Where is the Bug?
- The keyword is whille is incorrect
- Reason for Bug
- The Keyword used for iterations is while. In Programming Languages, keywords must be used as it is with correct spelling and spacings
Logical Bug
- Example 1 – Logical Bug
'''
__author__ = Ms. Samavi Suleman
__copyright__= Copyright I 2020 Ms. Samavi Suleman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main Purpose of the Program is to take two Numbers (Integers), calculate sum of Integers and display the sum on the Output Screen
'''
number1 = float(input('Enter a number: '))
number2 = float(input('Enter a number: '))
average = number1 + number2 / 2
print ('The average of the two numbers you have entered is:' , average)
Analysis, Main Findings and Conclusions
- Analysis – Where is the Bug?
- Logically the number1 + number2 / 2 of the program is incorrect
- Reason for Bug
- The precedence of division if high. In case number2 is first divided by 2 and then added with number1
- result = number2 / 2
- number1 + result
- The precedence of division if high. In case number2 is first divided by 2 and then added with number1
- Example 2 – Logical Bug
'''
__author__ = Ms. Samavi Suleman
__copyright__= Copyright I 2020 Ms. Samavi Suleman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main Purpose of the Program is to take two Numbers (Integers), calculate exponent of Integers and display the result on the Output Screen
'''
number1 = 10
number2 = 20
if (number1 = 1) {
sum = number1 + number2
print(sum)
}
Analysis, Main Findings and Conclusions
- Analysis – Where is the Bug
- Logically the condition of the program is incorrect. AS instead of Equality operator it is using assignment operator
- Reason for Bug
- According to the condition, 1 is assigned to variable number1 instead of checking whether number1 is equal to 1 or not
- Example 3 – Logical Bug
'''
__author__ = Ms. Samavi Suleman
__copyright__= Copyright I 2020 Ms. Samavi Suleman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main Purpose of the Program is to take two Numbers (Integers), calculate sum of Integers and display the sum on the Output Screen
'''
number1 = 10
number2 = 2
exponent = number1 * number2
print(exponent)
Analysis, Main Findings and Conclusions
- Analysis – Where is the Bug
- Logically the program is incorrect number1 * number2
- Reason for Bug
- Because instead if calculating the exponent of numbers it is calculating the product of numbers
- Example 4 – Logical Bug
'''
__author__ = Ms. Samavi Suleman
__copyright__= Copyright I 2020 Ms. Samavi Suleman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main Purpose of the Program is to display the count from 1 to 9 on the Output Screen
'''
for (Count = 1; Count <= 10; Count++):{
print("Count is ", Count)
}
Analysis, Main Findings and Conclusions
- Analysis – Where is the Bug
- Logically the program is incorrect
- Reason for Bug
- The program will print the count from 1 to 10 because the condition in for loop i.e., Count <= 10 should be Count < 10 to print numbers from 0 to 9
- Example 5 – Logical Bug
'''
__author__ = Ms. Samavi Suleman
__copyright__= Copyright I 2020 Ms. Samavi Suleman
__license__ = Public Domain
__version__ = 1.0
'''
'''
The main Purpose of the Program is to take two Numbers (Integers), calculate division of Integers and display the result on the Output Screen
'''
number1 = 10
number2 = 0
result = number1 / number2
print(result)
Analysis, Main Findings and Conclusions
- Analysis – Where is the Bug?
- Logically the program is incorrect
- Reason for Bug
- number1 is divided by 0 is logically incorrect
Debug
- Debug
- Definition
- To locate and remove bug from a computer program is termed as debug
- Purpose
- Hard, but critically important
- Any bug not detected in the design phase will cost ten times more to detect at the coding phase and an additional ten times more at the debugging phase
- Almost nothing kills usability faster than bugs
- Importance
- Debug will point the exact location where the bug occurs
Debugger
- Debugger
- Definition
- A debugger or debugging tool is a computer program used to test and debug other programs
- A dynamic debugger allows the software engineer to navigate by stepping through the code, observe which paths have executed, and observe how values of variables change during the controlled execution.
- Purpose
- A debugger can start some process and debug it, or attach itself to an existing process
- It can single-step through the code, set breakpoints and run to them, examine variable values and stack traces
- Debuggers let us see the state of any variable in an application, allowing us to stop and resume the application’s execution process anytime to see how every line of code impacts its internal state.
- Importance
- The main use of a debugger is to
- run the target program under controlled conditions
- The main use of a debugger is to
- Advantages
- The primary advantage to a true debugger is
- the ability to set breakpoints in your code and view the values of variables when that breakpoint is reached
- The primary advantage to a true debugger is
- Disadvantages
- Not running real-time, so may not expose all problems
- Suitable to Use
- This allows the user to identify incorrect code and find out how a program flow. The debugger lets the user see how their program is viewed by the computer.
- A debugger is very useful to find semantic errors in the program.
- Top 3 Debuggers for Software’s Developed in Python
- Pdb
- Ipdb
- GNU Debugger
- Winpdb
- Chapter Focus
- In Sha Allah, in this Chapter, I will use the following Python Debugger to Debug Python Programs
- pdb
- pdb — The Python Debugger
- Definition
- The module pdb defines an interactive source code debugger for Python programs
- Purpose
- pdb is a debugger, which means that it allows you to spy on a program while it executes by inspecting or modifying its runtime or to see what the program did when it crashed. The latter technique is called post-mortem debugging
- Importance
- pdb also supports
- conditional breaking
- breakpoints with corresponding Boolean expressions which determine whether debugger should stop upon reaching the breakpoint
- Post-mortem debugging
- Inspection of stack frames
- Single-stepping of source lines
- Code evaluation
- Looking through code with pdb helps to identify bugs that are usually hard to find and fix problematic code quickly
- pdb also supports
- Applications
- This module provides a command-line interface and works much like common C language debuggers (e.g., dbx, gdb)
- The pdb debugger can be run either interactively or from a command line and can be imported and called from a Python program.
- pdb also includes a useful postmortem analysis call, pdb.pm(), which starts the debugger after an exception has been encountered.
- Suitable to Use
- Execute the program step by step
- Main Functions of pdb
- Some of the Main Functions of pdb are as follows
- pdb.run()
- Execute the statement under debugger control
- pdb.set_trace()
- Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged
- pdb.post_mortem
- Enter post-mortem debugging of the given traceback object. If no traceback is given, it uses the one of the exception that is currently being handled (an exception must be being handled if the default is to be used)
- pdb.run()
- Commands in pdb
- Some of the Main Commands of pdb are as follows
- n(ext)
- execute the current line
- s(tep)
- step into functions called at the current line
- r(eturn)
- execute until the current function’s return is encountered
- c(ont(inue))
- Continue execution, only stop when a breakpoint is encountered
- j(ump)
- Set the next line that will be executed. Only available in the bottom-most frame. This lets you jump back and execute code again, or jump forward to skip code that you do not want to run.
- q(uit)
- Quit from the debugger. The program being executed is aborted.
- n(ext)
Debugging
- Debugging
- Definition
- Debugging is the process of detecting and removing of existing and potential errors (also called as ‘bugs’) in a software code that can cause it to behave unexpectedly or crash
The process of determining the cause of a failure is known as debugging
Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a computer program
Debugging is what you do after you have executed a successful test case
- Debugging is the process of detecting and removing of existing and potential errors (also called as ‘bugs’) in a software code that can cause it to behave unexpectedly or crash
- Purpose
- Debugging is the process to
- Correct the bugs found during testing
- Identify the failure of implemented code
- Absolution to code failure
- During the debugging phase, your goal is to find errors, so you should try everything you can think of that might break the program.
- Debugging is the process to
- Occurrence of Debugging
- Debugging occurs because of a test revealing a failure
- Debugging is conducted by programmers, preferably by those who wrote the code
- Debugging is primarily a code-level activity that is initiated after a defect is detected in the software
- The process of debugging begins as soon as the code of the software is written
- Importance
- To identifying, analyzing, and removing errors.
- This process begins after the software fails to execute properly and concludes by solving the problem and successfully testing the software
- Debugging Strategies
- Backward analysis
- The problem traces the program backward from the location of failure message to identify the region of faulty code
- Forward analysis
- The program involves tracking the program forward using breakpoints or print statements at different points in the program
- Backward analysis
- Steps involved in Debugging
- Identify the Error
- Find the Error Location
- Analyze the Error
- Prove the Analysis
- Cover Lateral Damage
- Fix & Validate
- Advantages
- Earlier detection of an error
- It also provides maximum useful information of data structures and allows easy interpretation
- Reduce useless and distracting information
- Through debugging the developer can avoid complex one-use testing code to save time and energy in software development
- Disadvantages
- The original justification for setter methods was the realization that allowing anybody to modify instance variables made them indistinguishable from global variables – hence making debugging much more difficult. Therefore, if one prevented direct access to the instance variable, it would fix that problem
- Debugging is usually a time consuming and error-prone process
Steps – Debugging Software
- Steps – Debugging Software
- Debugging is a two-step process that begins when you find an error because of a successful test case.
- Step 1 is the determination of the exact nature and location of the suspected error within the program.
- Step 2 consists of fixing the error.
- Steps – Debugging Software
- In Sha Allah, I will debug a Software in the following Steps
- Step 1: Obtain the Software to be Debugged
- Step 2: Select a Suitable Debugger
- Step 3: Use Debugger to Debug the Software
- Example 1 - Steps (Debugging a Software)
- In Sha Allah, I will debug a Software in the following Steps
- Step 1: Obtain the Software to be Debugged
- I will Debug addition-v1.1.py Software
- Step 2: Select a Suitable Debugger
- In Sha Allah, I will use the pdb Python Library for Debugging addition-v1.1.py Software
- Step 3: Use Debugger to Debug the Software
- In Sha Allah, I will use pdf Python Library for Debugging addition-1.1.py Software in the following Steps
- Step 1.1: Import pdb
import pdb - Step 1.2: Use the following command at the point from where you want to start debugging
pdb.set_trace()
- Step 1.1: Import pdb
- In Sha Allah, I will use pdf Python Library for Debugging addition-1.1.py Software in the following Steps
- Step 1: Obtain the Software to be Debugged
- Step 1.3: Press Shift + Enter keys to execute the Software
- Step 1.4: Enter an Integer Number (i.e, value for Variable (number1))
- Press Enter key
- Step 1.4: Enter an Integer Number (i.e, value for Variable (number1))
- Step 1.5: Software enters pdb state
- Step 1.5.1: check the current value of Variable (number1)
- Step 1.5: Software enters pdb state
- Step 1.6: Write the following command to exit the pdb mode or continue the program
Continue
- Step 1.6: Write the following command to exit the pdb mode or continue the program
- Step 1.7: Enter an Integer Number (i.e., value for Variable (number2))
- The Sum of numbers will be displayed on the Output Screen to the User
- Step 1.7: Enter an Integer Number (i.e., value for Variable (number2))
Chapter Summary
- Chapter Summary
In this Chapter, I presented the following main concepts:
- Bug
- A bug refers to an error, fault or flaw in any computer program or a hardware system
- Types of Bugs
- Following are the types of bug
- Syntax bugs
- Runtime bugs
- Logic bugs
- Arithmetic bugs
- Following are the types of bug
- Error
- A mistake in coding is called Error
- Defect
- A defect is a system error that does not allow the intended action to be completed
- Defect Status
- Defect Status or Bug Status in defect life cycle is the present state from which the defect or a bug is currently undergoing
- Software Bug
- A software bug is an error, flaw or fault in a computer program or system that causes it to produce an incorrect or unexpected result, or to behave in unintended ways
- Debugger
- A debugger or debugging tool is a computer program used to test and debug other programs
- pdb — The Python Debugger
- The module pdb defines an interactive source code debugger for Python programs
- Debugging
- Debugging is the process of detecting and removing of existing and potential errors (also called as ‘bugs’) in a software code that can cause it to behave unexpectedly or crash
- Steps – Debugging Software
- Step 1: Obtain the Software to be Debugged
- Step 2: Select a Suitable Debugger
- Step 3: Use Debugger to Debug the Software
In Next Chapter
- In Next Chapter
- In Sha Allah, in the next Chapter, I will present a detailed discussion on
- Software Deployment