0% found this document useful (0 votes)
8 views35 pages

3cs510ic24 - Stqa Minor Lect-6

Uploaded by

aafadf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views35 pages

3cs510ic24 - Stqa Minor Lect-6

Uploaded by

aafadf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

3CS510IC24- Software Testing & Quality

Assurance

Lecture-6 Odd Sem 24-25


5th Semester – Minor Course
Prof. Daiwat Vyas
Unit Testing
DELL , 15-11-2024, 05:31:41

• Definition: Testing individual units or components of a software.

• Analogy: Testing a single part of a car engine before assembling it


into the entire engine.

• Example: In a calculator app, testing the addition function


separately from the entire calculator.
Unit Testing
DELL , 15-11-2024, 05:31:41

• What is Unit Testing?


• Definition: Unit testing is a type of software testing where individual components (or
units) of a software program are tested in isolation to ensure they work correctly.
• Purpose: It aims to validate that each unit of the software performs as expected. A
"unit" can be a function, method, or procedure in code.

• Analogy: Testing Each Part of a Car


• Imagine you are building a car. Before assembling the entire car, you would want to
make sure that each part works perfectly. For example:
• Engine: You'd test the engine alone to ensure it runs smoothly.
• Brakes: You’d test the brakes separately to make sure they stop the car effectively.
• Lights: You’d check each light individually to see if they turn on and off.
Unit Testing

• 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.”

• Program is in next slide:


Unit Testing
• Technical Example:
• A Calculator Program
Unit Testing
• Technical Example: A Calculator Program
• Unit Testing Example:
• For each function, we can write a unit test to check if the function works as expected.
1. Testing the add Function:
o Test Case 1: Add 2 and 3, expecting the result to be 5.
o Test Case 2: Add -1 and 1, expecting the result to be 0.
2. Testing the subtract Function:
o Test Case 1: Subtract 5 from 3, expecting the result to be -2.
o Test Case 2: Subtract 0 from 0, expecting the result to be 0.
3. Testing the multiply Function:
o Test Case 1: Multiply 2 by 3, expecting the result to be 6.
o Test Case 2: Multiply 0 by any number, expecting the result to be 0.
4. Testing the divide Function:
o Test Case 1: Divide 6 by 3, expecting the result to be 2.
o Test Case 2: Divide 6 by 0, expecting the result to be 0 (handling division by zero).
Unit Testing
• Why Unit Testing is Important
• Catches Errors Early: Just like testing car parts or pizza
ingredients, unit testing helps find problems early before they affect
the whole system.
• Simplifies Debugging: If a test fails, you know exactly which part
(unit) is broken, making it easier to fix.
• Improves Code Quality: Regular unit testing ensures each
component works perfectly, leading to better overall software
quality.
Unit Testing
• Unit testing is like testing individual parts of a car or ingredients of a
pizza before putting them together. It ensures that each small piece
of your program works correctly before integrating it into the larger
system.
Defect Prevention
• Definition: Techniques used to prevent defects from occurring in
the first place.

• Analogy: Using a checklist before a flight to ensure all safety


measures are in place.

• Example: Code reviews, pair programming, and using coding


standards.
Defect Prevention
• Defect Prevention refers to proactive measures and practices
aimed at avoiding defects or issues in a software product before
they occur.

• Instead of waiting for defects to appear during testing or after


the software is released, defect prevention focuses on improving
the development process, quality assurance methods, and team
practices to minimize the chances of defects from the outset.
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
• 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.

• Training and Knowledge Sharing:

• Regularly training developers on the latest tools, technologies, and techniques.


• Example: A company organizes workshops on secure coding practices to prevent
security-related defects.
Defect Prevention
• Analogies:

• 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.

• Routine Health Checkups: Regular health checkups can help identify


and prevent health issues before they become serious, just as regular
code reviews and testing can prevent defects in the software.
Defect Prevention
• Example Scenario:

• 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.

• Analogy: Changing a recipe slightly to see if the chef notices the


difference in taste.

• Example: Altering an arithmetic operator in a function and


checking if existing test cases catch the error.
Mutation Testing
• Mutation Testing is a software testing technique used to evaluate
the quality and effectiveness of existing test cases.

• The main idea behind mutation testing is to introduce small


changes (mutations) to the program's code and then run the test
cases to see if they can detect these changes.

• If the test cases catch the mutations, they are considered


effective; if not, it indicates that the test cases might need
improvement.
Mutation Testing
• Key Concepts:

• 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:

• Replacing an arithmetic operator: Changing + to - or * to /.


• Negating a condition: Changing > to <= in an if-statement.
• Changing a variable: Substituting one variable with another in an expression.
Mutation Testing
• Key Concepts:
• Test Case Evaluation:
• Once mutants are created, the test suite is run against them. If a test case fails when
run on a mutant, the mutant is "killed," meaning the test case successfully detected the
mutation.
• If no test case detects the mutation, the mutant survives, indicating that the test suite
might be insufficient.
• Mutation Score:
• The effectiveness of the test cases is measured by the mutation score, calculated as:

• A higher mutation score indicates better test coverage and effectiveness.


Mutation Testing
• Analogies:

• 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

• Running the Test:

• 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:

• Improves Test Quality: By identifying weaknesses in your test suite, mutation


testing helps you write more effective and comprehensive test cases.
• Ensures Robustness: It ensures that your code is not only working for the tested
cases but is also resilient to potential small changes or mistakes.

• Challenges:

• Computationally Expensive: Generating and testing a large number of mutants


can be resource-intensive.
• Complexity: Understanding and analyzing the results of mutation testing can be
complex, especially for large programs.
Debugging

• Definition: The process of finding and fixing defects in software.

• Analogy: A detective searching for clues to solve a mystery.

• Example: Identifying the cause of a crash in a mobile app by


examining logs and error messages.
Debugging
• Key Concepts:
1. Bug Identification:
The first step in debugging is to identify that there is a problem. This can be
triggered by a test failure, a user report, or an unexpected behavior in the
software.
2. Isolation:
Once a bug is identified, the next step is to isolate the section of code where the
problem is occurring. This involves narrowing down the possible causes and
locating the exact source of the defect.
3. Analysis:
After isolating the bug, the developer analyzes the faulty code to understand
why the issue is happening. This involves reviewing the logic, checking variable
values, and understanding the flow of the program.
Debugging
• 4. Fixing the Bug:

• 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.

• 5. Testing the Fix:

• 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:

• Detective Solving a Mystery: Debugging is like a detective solving a


mystery. The detective (developer) has to gather clues (error
messages, logs), interview witnesses (check code dependencies), and
piece together the sequence of events (code execution flow) to find
the culprit (bug).

• 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))

Suppose you mistakenly call the function with incorrect parameters:


print("The area of the rectangle is:", calculate_area(width, length))
Debugging
• Steps in Debugging:

• 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.

• Fixing the Bug:


• Correct the function call to pass the parameters in the correct order:
Debugging
• Steps in Debugging:

• Testing the Fix:

• 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.

You might also like