0% found this document useful (0 votes)
16 views

Chapter 5 Writing and Executing Unit Tests Using A Framework

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Chapter 5 Writing and Executing Unit Tests Using A Framework

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Mizan-Tepi UNIVERSITY

School of Computing and Informatics


Department of Software Engineering

Course: Software Engineering Tools and Practices


Chapter 5
Writing and executing Unit Tests using a
framework
CONTENTS
❖ What is Unit Testing?

❖ Why perform Unit Testing?

❖ How to execute Unit Testing

❖ Unit Testing Techniques and Unit Testing Tools

❖ Unit Testing Best Practices

2
What is Unit Testing?
❖Unit Testing is a type of software testing where individual units
or components of a software are tested.
❖The purpose is to validate that each unit of the software code performs
as expected.
❖Unit Testing is done during the development (coding phase) of
an application by the developers.
❖ Unit Tests isolate a section of code and verify its correctness.
❖A unit may be an individual function, method, procedure, module,
or object.
3
What is Unit Testing? …
❖ In SDLC, STLC (Software Testing Life Cycle), V Model, Unit
testing is first level of testing done before integration testing.
❖ Unit testing is a White-Box testing technique that is
usually performed by the developer.
❖ Though, in a practical world due to time crunch or reluctance
of developers to tests, QA engineers also do unit testing.

4
Why perform Unit Testing?
❖ Unit Testing is important because software developers sometimes
try saving time doing minimal unit testing and this is myth
because inappropriate unit testing leads to high cost Defect
fixing during System Testing, Integration Testing and even Beta
Testing (involves external users to obtain real-world feedback prior to
the official release.) after application is built.
❖ If proper unit testing is done in early development, then it saves
time and money in the end.
❖ Here, are the key reasons to perform unit testing in
software engineering:
5
Why perform Unit Testing? …
▪ Unit tests help to fix bugs early in the development cycle and save costs.
▪ It helps the developers to understand the testing code base and enables them to
make changes quickly.
▪ Good unit tests serve as project documentation.
▪ Unit tests help with code re-use.
• Migrate both your code and your tests to your new project.
• Tweak (improve) the code until the tests run again.

6
Why perform Unit Testing? …

7
How to execute Unit Testing?
❖ In order to execute Unit Tests, developers write a section of code
to test a specific function in software application.
❖ Developers can also isolate this function to test more
which reveals unnecessary dependencies between function being
rigorously
tested and other units so the dependencies can be eliminated.
❖ Developers generally use Unit Test framework to develop
automated test cases for unit testing.
❖ Unit Testing is of two types:

8
How to execute Unit Testing? …
▪Manual
▪Automated
❖ Unit testing is commonly automated but may still be
performed manually.
❖ Software Engineering does not favor one over the other
automation
but is preferred.
❖ A manual approach to unit testing may employ step-by-step
a instructional document.

9
How to execute Unit Testing? …
❖ Under the automated approach:
▪A developer writes a section of code in the application just to test the function.
• They would later comment out and finally remove the test code when the application
is deployed.
▪A developer could also isolate the function to test it more rigorously.
• This is a more thorough unit testing practice that involves copy and paste of code to
its own testing environment than its natural environment.
• Isolating the code helps in revealing unnecessary dependencies between the code
being tested and other units or data spaces in the product.
• These dependencies can then be eliminated.

10
How to execute Unit Testing? …
▪A coder generally uses a Unit Test Framework to develop automated test cases.
• Using an automation framework, the developer codes criteria into the test to
verify the correctness of the code.
• During execution of the test cases, the framework logs failing test cases.
• Many frameworks will also automatically flag and report, in summary, these failed test cases.
• Depending on the severity of a failure, the framework may halt subsequent testing.
▪The workflow of Unit Testing is:
1) Create Test Cases
2) Review/Rework
3) Baseline
4) Execute Test Cases.

11
Unit Testing Techniques
Unit Testing Techniques
❖ The Unit Testing Techniques are mainly categorized into three
parts which are:
▪Black box testing that involves testing of user interface along with input and
output,
▪White box testing that involves testing the functional behavior of the software
application and
▪Gray box testing that is used to execute test suites, test methods, test cases and
performing risk analysis.
❖ Actually in simple code coverage refers to the degree of which
the source code of the software code has been tested.
13
Unit Testing Techniques
❖ Code coverage is a software testing metric or also termed as a
code coverage testing which helps in determining how much code has
been tested which helps in assessing quality of test suite (set) and
analyzing how comprehensively a software is verified.
❖ This code coverage is considered as one of the white box testing.
❖ The formula to calculate code coverage is:
▪Code Coverage = (Number of lines of code executed)/(Total number of lines
of code in a system component)*100.

14
Unit Testing Techniques …
❖Code coverage techniques (criteria) used in Unit Testing are
listed below:
▪Statement Coverage
▪Decision Coverage
▪Condition Coverage
▪Finite State Machine Coverage
❖ Statement Coverage/Block coverage:
▪The number of statements that have been successfully executed in the program
source code.

15
Unit Testing Techniques …
▪Statement Coverage = (Number of statements executed)/(Total
number of statements)*100.
❖ Example of Statement Coverage:
❖ Consider the Source code on the next slide:
❖ Scenario 1: if a=3, b=9
❖ Scenario 2: if a=-3, b=-9
❖Find Statement coverage for both scenarios of the source code on the
next slide!

16
Unit Testing Techniques …
if a=3, b=9
Read a=1,
Prints (int a, int b){ Read b=1,
------------------ Printsum is a function Int result=a+b;=1
Int result =a + b; If (result>0)=1
If (result>0) Print (“Positive”, result)=1
Print (“Positive”, result) 5/6*100=83.3%
Else
Print (“Negative”, result) if a=-3, b=-9
} Read a=1,
-------- end of the source code Read b=1,
If (result>0)=1
Int result=a+b;=1
Print (“Negative”, result)=1
5/6*100=83.3% 17
Unit Testing Techniques …
❖ Decision Coverage/Branch coverage:
▪The number of decision control structures that have been
successfully executed in the program source code.
▪Decision Coverage = (Number of decision/branch outcomes exercised)/Total
number of decision outcomes in the source code)*100.

18
Unit Testing Techniques …
❖ Function Coverage:
▪The number of function that are called and executed at least once in the source
code.
▪Function Coverage = (Number of functions called)/(Total
number of functions)*100.
❖ Condition Coverage/Expression coverage:
▪The number of boolean condition/expression statements executed
in the conditional statement.
▪Condition Coverage = (Number of executed operands)/(Total
number of operands)*100.

19
Unit Testing Techniques …

❖FSM Coverage:
▪ It is certainly the most complex type of code coverage method. This is because it works
on the behavior of the design.
▪ In this coverage method, you need to look for how many time-specific states are visited,
transited.
▪ It also checks how many sequences are included in a finite state machine.
▪ FSM coverage answers the question, “Did I reach all of the states and traverse all
possible paths through a given state machine?”
▪ There are two types of coverage detail for FSMs that covered can handle:
• State coverage: Answers the question, “Were all states of a FSM hit during simulation?”
• State transition coverage: Answers the question, “Did the FSM transition between all states (that are
achievable) in simulation?”

20
Unit Testing Techniques …
❖ Code coverage tools:
▪Cobertura:
• It is an open source code coverage tool.
• It measures test coverage by instrumenting a code base and analyze which lines of
code are executing and which are not executed when the test suite runs.
▪Clover:
• Reduces testing time by only running the tests which cover the application code
which was modified since the previous build.
▪DevPartner:
▪Enables developers to analyze java code for code quality and complexity.

21
Unit Testing Techniques …
▪ Emma:
• Supports class, method, line, and base block coverage, aggregated source file, class.
And method levels.
▪ Kalistick:
• Is a third part application which analyzes the codes with different perspectives.
▪ CoView and CoAnt:
• Is a code coverage tool for metrics, mock object creation, code testability, path and
branch coverage, etc.
▪ Bullseye for C++:
• Code coverage tool for C++ and C

22
Unit Testing Techniques …
▪Sonar:
• An open code coverage tool which helps you to manage code quality.

❖ Advantages of using code coverage:


▪Helpful to evaluate a quantitative measure of source code.
▪It allows you to create extra test cases to increase coverage.
▪It allows you to find the areas of a program which is not exercised by a set of
test cases.
❖ Disadvantages of using code coverage:
▪Even when any specific feature is not implemented in design, code coverage
still report 100% coverage.
23
Unit Testing Techniques …
▪ It is not possible to determine whether we tested all possible values of
a feature with the help of code coverage.
▪ Code coverage is also not telling how much and how well you have covered
your logic.
▪ In the case when the specific function hasn’t implemented, or a not included
from the specification, then structure-based techniques cannot find that issue.

24
Unit Testing Tools
❖ There are several automated unit test software available to assist
with unit testing:
▪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.
▪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.

25
Unit Testing Tools …

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

❖PHPUnit:
▪PHPUnit is a unit testing tool for PHP programmer.
▪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.
26
Test Driven Development (TDD) & Unit Testing
❖ Unit testing in TDD involves an extensive use of testing frameworks.
❖ A unit test framework is used in order to create automated unit tests.
❖ Unit testing frameworks are not unique to TDD, but they are
essential to it.
❖ Below we look at some of what TDD brings to the world of
unit testing:
▪Tests are written before the code
▪Rely heavily on testing frameworks
▪All classes in the applications are tested
27
Advantages of unit testing
❖ Developers looking to learn what functionality is provided by a
unit and how to use it can look at the unit tests to gain a
basic understanding of the unit API.
❖ Unit testing allows the programmer to refactor code at a later
date, and make sure the module still works correctly (i.e.,
Regression testing).
❖ Due to the modular nature of the unit testing, we can test parts of
the project without waiting for others to be completed.

28
Unit Testing Disadvantages
❖ Unit testing can’t be expected to catch every error in a program.
❖ Unit testing by its very nature focuses on a unit of code. Hence
it can’t catch integration errors or broad system level errors.

29
Unit Testing Best Practices

❖Unit Test cases should be independent.

❖Test only one code at a time.

❖Follow clear and consistent naming conventions for your unit tests.

❖In case of a change in code in any module, ensure there is a corresponding


unit Test Case for the module, and the module passes the tests before
changing the implementation.
❖Bugs identified during unit testing must be fixed before proceeding to the
next phase in SDLC. 30
Unit Testing Best Practices …

31

You might also like