3cs510ic24 - Stqa Minor Lect-6
3cs510ic24 - Stqa Minor Lect-6
Assurance
• Just like testing each car part individually, in unit testing, each
piece of code is tested separately to ensure it works as intended.
Unit Testing
• Another Analogy: Assembling a Pizza
• Think of making a pizza. Before you bake the entire pizza, you
might want to taste each ingredient separately:
• Sauce: Is it flavorful?
• Cheese: Is it the right kind?
• Dough: Is it soft and well-prepared?
• Similarly, in unit testing, you "taste" (or test) each "ingredient" (or
unit) of your program separately to ensure it’s correct before
combining everything.
Unit Testing
• Technical Example: A Calculator Program
• We have a simple calculator program that can add, subtract, multiply,
and divide numbers. Each of these operations can be considered a
“unit.”
• Using tools to analyze code for potential errors without executing the program.
• Example: A tool like SonarQube checks for common programming errors, code
smells, and security vulnerabilities during the development process.
• Automated Testing:
• Writing automated tests that run every time code is changed to ensure nothing
breaks.
• Example: Unit tests are written for each function in a program so that if a developer
changes the code, the tests will quickly identify if the change introduces a defect.
Key Strategies for Defect Prevention
• Static Code Analysis:
• Using tools to analyze code for potential errors without executing the program.
• Example: A tool like SonarQube checks for common programming errors, code
smells, and security vulnerabilities during the development process.
• Automated Testing:
• Writing automated tests that run every time code is changed to ensure nothing
breaks.
• Example: Unit tests are written for each function in a program so that if a developer
changes the code, the tests will quickly identify if the change introduces a defect.
Key Strategies for Defect Prevention
• Use of Standards and Best Practices:
• Adhering to industry standards and best practices in coding, design, and testing.
• Example: Following coding standards such as the SOLID principles in object-oriented
programming to ensure the code is maintainable and less prone to defects.
• Checklist Before a Flight: Just like a pilot uses a checklist before taking
off to ensure that all systems are functioning correctly and to prevent
any possible issues during the flight, developers use defect prevention
techniques to ensure the software is free of potential defects before it
is released.
• Imagine you're building a website that allows users to sign up for an account. To
prevent defects:
• You might create automated tests to check that the sign-up form correctly validates
input.
• You would also review the code with another developer to ensure there are no security
loopholes or logic errors.
• Finally, you might use a static code analysis tool to scan the code for common security
vulnerabilities, ensuring that the website is secure and bug-free before it's launched.
• Defect prevention is crucial for building high-quality, reliable software and can save
significant time and resources by reducing the number of defects that need to be fixed
later in the development process.
Mutation Testing
• Definition: Testing by making small changes (mutations) to the
program and checking if the test cases detect them.
• Mutants: Mutants are the slightly altered versions of the original program.
Each mutant contains one or more small changes (mutations), such as altering
an operator, changing a constant, or modifying a control flow statement.
• Mutation Operators: These are the rules or methods used to create mutants.
• Examples include:
• Recipe Testing: Imagine you have a recipe for baking a cake. You decide to test the
accuracy of the recipe by slightly altering the ingredients (e.g., using salt instead of
sugar) and seeing if someone notices the difference when tasting the cake. If they do,
the recipe is considered robust; if not, it suggests that the recipe (or the taste test)
might not be detailed enough.
• Security Drill: Consider a scenario where a security team conducts a drill by introducing
a minor security breach to see if the security systems detect it. If the breach is
detected, the systems are effective. If not, the systems need improvement.
Mutation Testing
• Example Scenario:
• Let’s say you have a simple function that calculates the sum of two numbers:
def add(a, b):
return a + b
• Creating Mutants:
• Mutation 1: Change the + operator to -:
def add(a, b):
return a – b
• Mutation 2: Change the + operator to *:
def add(a, b):
return a * b
Mutation Testing
• Evaluating Test Cases:
• Assume you have a test case for this function:
def test_add():
assert add(2, 3) == 5
• On Mutation 1: The test will fail because 2 - 3 is not equal to 5. This mutant is "killed."
• On Mutation 2: The test will fail because 2 * 3 is not equal to 5. This mutant is also
"killed.“
• If all mutants are killed, it indicates that the test case is effective. If any mutants survive,
it suggests that additional test cases are needed to cover those scenarios.
Mutation Testing
• Benefits of Mutation Testing:
• ????
• Challenges:
• ???
Mutation Testing
• Benefits of Mutation Testing:
• Challenges:
• Once the root cause is understood, the developer modifies the code to fix the
issue. This could involve correcting logic errors, fixing syntax mistakes, or
changing how data is handled.
• After fixing the bug, the modified code is tested to ensure that the problem is
resolved and that no new issues have been introduced.
• 6. Documentation:
• It is good practice to document the bug, its cause, and the steps taken to fix it.
This helps in understanding the issue better if it occurs again and aids other
developers who might work on the code in the future.
Debugging
• Analogies:
• Mechanic Fixing a Car: Imagine a car that won’t start. The mechanic
has to check different parts (battery, fuel system, ignition) to isolate
the problem. Once the faulty part is found, the mechanic fixes or
replaces it and then tests the car to ensure it works properly. Similarly,
in debugging, developers check different parts of the code, fix the
issue, and then test the software.
Debugging
• Example Scenario
def calculate_area(length, width):
area = length * width
return area
#Example usage:
length = 5
width = 10
print("The area of the rectangle is:", calculate_area(length, width))
• Identification:
• You notice that the output is incorrect because the parameters are swapped.
• Isolation:
• You review the function call and notice that width and length were passed in the
wrong order.
• Analysis:
• You analyze the code and realize that swapping the parameters changes the
expected output.
• Run the program again to ensure the output is now correct: "The area of the
rectangle is: 50".
• Documentation:
• You might document this issue in the code comments or a bug tracking
system, explaining the importance of correct parameter order.
Control Flow Testing
• Definition: Testing the logical flow of the program, including all
possible paths.
• Analogy: Checking every possible route on a map to ensure there
are no roadblocks.
• Example: Testing a loop structure in a program to ensure it
behaves correctly with different inputs.
Summary
• • Unit Testing: Focuses on individual parts of a program.
• • Defect Prevention: Techniques to avoid defects before they
occur.
• • Mutation Testing: Checks the effectiveness of test cases.
• • Debugging: The process of identifying and fixing errors.
• • Control Flow Testing: Ensures all logical paths are tested.