0% found this document useful (0 votes)
9 views23 pages

1.1 Chapter - 22 - Unit Testing

Unit testing is a process where individual components of a software application are tested independently to ensure they function correctly. It involves validating both positive and negative scenarios, following specific rules, and can utilize various techniques such as structural and functional testing. While unit testing offers advantages like early issue detection and improved code quality, it also has disadvantages including time consumption and limitations in covering all potential errors.

Uploaded by

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

1.1 Chapter - 22 - Unit Testing

Unit testing is a process where individual components of a software application are tested independently to ensure they function correctly. It involves validating both positive and negative scenarios, following specific rules, and can utilize various techniques such as structural and functional testing. While unit testing offers advantages like early issue detection and improved code quality, it also has disadvantages including time consumption and limitations in covering all potential errors.

Uploaded by

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

SOFTWARE

1
TESTING
STRATEGY—THE
BIG PICTURE
Unit Testing
WHAT IS UNIT TESTING?
 Whenever the application/module is ready (done with
coding) and given to the Test engineer, he/she will start
checking every component of the module or module of
the application independently or one by one, and this
process is known as Unit testing or components
testing.

2
EXAMPLE OF UNIT TESTING?
 Let us see one sample example for a better understanding of the
concept of unit testing:

This is an example of
an Online Baking
Application like HBL
Mobile App. From the
picture, it is clear that
we’ve three modules.

3
 Taking in to account M1 “Amount Transfer” we will begin our unit
testing as follows:

Now, we will start


performing the unit
testing on the different
components such as
1. From account
number (FAN)
2. To account number
(TAN)
3. Amount
4. Transfer
5. Cancel

4
 The Components identified will be tested as follows:
 For Transfer component
1. Enter valid FAN value
2. Enter valid TAN value
3. Enter the correct value of Amount
4. Click on the Transfer button→ amount
transfer successfully
( confirmation message)
 For Cancel Component
1. Enter the valid values of FAN, TAN, and
amount.
2. Click on the Cancel button → a warning message should be
displayed about the cancellation of the transaction.

5
FOR THE FAN COMPONENTS
Values Description
1234 accept
4311 Error message→ account valid
or not
blank Error message→ enter some
values
5 digit/ 3 digit Error message→ accept only 4
digit
Alphanumeric Error message → accept only
digit
Blocked account no Error message
Copy and paste the value Error message→ type the 6
value
While performing unit testing, we should
follow some rules, which are as follows:
 To start unit testing, at least we should have one
module.
 Test for positive values (validity)
 Test for negative values (in-validity)
 No over testing
 No assumption required

7
TRY TO APPLY!
 As we’ve discussed earlier, that unit testing should be
applied to check for validity as well as invalidity. The
following module has been tested for validity.
1. For Transfer component
 Enter valid FAN value
 Enter valid TAN value
 Enter the correct value of Amount
 Click on the Transfer button→ amount transfer successfully
( confirmation message)
 Can you try to check it for invalidity?

8
 Unit Test Considerations: Unit tests are illustrated
schematically in Figure 22.3
• The module interface is tested to ensure
that information properly flows into and out
of the program unit under test.
• Local data structures are examined to
ensure that data stored temporarily
maintains its integrity during all steps in an
algorithm’s execution.
• Boundary conditions are tested to ensure
that the module operates properly at
boundaries established to limit or restrict
processing.
• All independent paths through the control
structure are exercised to ensure that all
statements in a module have been executed
at least once.
9
• And finally, all error-handling paths are
 Unit-Test Procedures. Unit testing is normally
considered as an adjunct to the coding step. The
design of unit tests can occur before coding begins or
after source code has been generated.
 The unit test environment is illustrated in Figure 22.4

Stubs and drivers both


are dummy modules and
are only created for test
purposes.

10
 Driver is nothing more than a “main program” that accepts test-case data,
passes such data to the component (to be tested), and prints relevant
results.
 Stubs (dummy subprogram) serve to replace modules that are
subordinate (invoked by) the component to be tested.
 A stub or “dummy subprogram” uses the module’s interface, may do
minimal data manipulation, prints verification of entry, and returns control
to the module undergoing testing.

11
A REAL-WORLD EXAMPLE
You have written a function to add two numbers:
int Add(int a, int b) { return a+b; }

 The above function takes two numbers as input and returns their sum.

 A unit test code would look something like this:

 void TestAdd1() { Assert.IsEqual(Add(5, 10), 15) }


 The above unit test “asserts” that 5 + 10 is equal to 15. If the Add function returns anything
else Assert.IsEqual result in error and the test case will fail.

 You will probably add a few more unit test cases like these:
 void TestAdd2() { Assert.IsEqual(Add(500, 1000), 1500) }
 void TestAdd3() { Assert.IsEqual(Add(0, 1000), 1000) }
 void TestAdd4() { Assert.IsEqual(Add(-100, 100), 0) }
 void TestAdd5() { Assert.IsEqual(Add(-100, -1100), -1200) } 12
UNIT TESTING - EXAMPLE
Public int A(int a,int b)
{
if (a%2==0)
return a;
else
return b;
}
Public int B (int a,int b,int c)
{
int val=A(a,b);
val=A(val,c);
return val,
}
Perform UT for function A & B so that the coverage is100%
UNIT TESTING - EXAMPLE
Test Case ID Description Input Data Expected Actual Pass / Fail Remarks
Results Results

UT-01 To test A function when a is 2,3 2 2 Pass -


even

UT-02 To test A function when a is 1,2 2 2 Pass -


odd

UT-03 To test B function providing 2,3,4 2 2 Pass -


any 3 values

If only UT for function B is done then the coverage of function A is 50%.


Why?
UNIT TESTING
TECHNIQUES

15
STRUCTURAL UNIT TESTING
 Structural testing is a white box testing technique in which a developer
designs test cases based on the internal structure of the code, in a white box
approach. The approach requires identifying all possible paths through the
code. The tester selects test case inputs, executes them, and determines the
appropriate output.

 Primary structural testing techniques include:

 Statement, branch, and path testing—each statement, branch, or path in


a program is executed by a test at least once.
 Conditional testing—allows a developer to selectively determine the path
executed by a test, by executing code based on value comparisons.
 Expression testing—tests the application against different values of a
regular expression.
16
FUNCTIONAL UNIT TESTING
 Functional unit testing is a black box testing technique for testing the
functionality of an application component.

 Main functional techniques include:

 Input domain testing—tests the size and type of input objects and
compares objects to equivalence classes.
 Syntax checking—tests that check whether the software correctly interprets
input syntax.
 Equivalent partitioning—a software testing technique that divides the
input data of a software unit into data partitions, applying test cases to each
partition.
17
ERROR-BASED TECHNIQUES
 Error-based unit tests should preferably be built by the developers who
originally designed the code. Techniques include:

 Fault seeding—putting known bugs into the code and testing until they
are found.
 Mutation testing—changing certain statements in the source code to see
if the test code can detect errors. Mutation tests are expensive to run,
especially in very large applications.
 Historical test data—uses historical information from previous test case
executions to calculate the priority of each test case.

18
ADVANTAGES OF UNIT
TESTING:
1.Unit testing allows the programmer to refine code and make sure the module
works properly.
2.Early Detection of Issues: Unit testing allows developers to detect and fix issues
early in the development process, before they become larger and more difficult to
fix.
3.Improved Code Quality: Unit testing helps to ensure that each unit of code works
as intended and meets the requirements, improving the overall quality of the
software.
4.Faster Development: Unit testing enables developers to work faster and more
efficiently, as they can validate changes to the code without having to wait for
the full system to be tested.
5.Better Documentation: Unit testing provides clear and concise documentation of
the code and its behavior, making it easier for other developers to understand and
maintain the software.
6.Facilitation of Refactoring: Unit testing enables developers to safely make changes
to the code, as they can validate that their changes do not break existing 19
functionality.
DISADVANTAGES OF UNIT TESTING:
1. The process is time-consuming for writing the unit test cases.
2. Unit Testing will not cover all the errors in the module because there is a chance of having errors
in the modules while doing integration testing.
3. Unit Testing is not efficient for checking the errors in the UI(User Interface) part of the module.
4. It cannot cover the non-functional testing parameters such as, the performance of the system,
etc.
5. Time and Effort: Unit testing requires a significant investment of time and effort to create and
maintain the test cases, especially for complex systems.
6. Dependence on Developers: The success of unit testing depends on the developers, who must
write clear, concise, and comprehensive test cases to validate the code.
7. Difficulty in Testing Complex Units: Unit testing can be challenging when dealing with complex
units, as it can be difficult to isolate and test individual units in isolation from the rest of the
system.
8. Difficulty in Testing Interactions: Unit testing may not be sufficient for testing interactions
between units, as it only focuses on individual units.
9. Over-reliance on Automation: Over-reliance on automated unit tests can lead to a false sense of
security, as automated tests may not uncover all possible issues or bugs.
10.Maintenance Overhead: Unit testing requires ongoing maintenance and updates, as the code
and test cases must be kept up-to-date with changes to the software. 20
UNIT TESTING TOOLS:
1. Junit: Junit is a free to use testing tool used for Java programming language. It provides
assertions to identify test method. This tool test data first and then inserted in the piece of code.
2. NUnit: NUnit is widely used unit-testing framework use for all .net languages. It is an open source
tool which allows writing scripts manually. It supports data-driven tests which can run in parallel.
3. JMockit: JMockit is open source Unit testing tool. It is a code coverage tool with line and path
metrics. It allows mocking API with recording and verification syntax. This tool offers Line
coverage, Path Coverage, and Data Coverage.
4. EMMA: EMMA is an open-source toolkit for analyzing and reporting code written in Java language.
Emma support coverage types like method, line, basic block. It is Java-based so it is without
external library dependencies and can access the source code.
5. PHPUnit: PHPUnit is a unit testing tool for PHP programmers. It takes small portions of code
which is called units and test each of them separately. The tool also allows developers to use pre-
define assertion methods to assert that a system behave in a certain manner.
21
CLASS ACTIVITY 02: PRACTICE UNIT TESTING
 You can do it either individually or can make a group of 2-4
people.
 Tear a page out and write your course name and section,
complete ID, and full name on it.
 Statement: Take into consideration any online software
application, mention its name, and identify its at-least five
units to perform unit testing.
 Hint: I am not talking about the components.

 Solution: Discussed during the class.

22
BOOK READING
 Contents covered so far.

23

You might also like