Lab 02- Code Coverage

.docx
School
National University of Sciences & Technology, Islamabad**We aren't endorsed by this school
Course
SE 314
Subject
Computer Science
Date
Dec 18, 2024
Pages
5
Uploaded by MajorSalamander4734
SE-314: Software ConstructionPage 1Faculty of ComputingSE-314: Software ConstructionClass: BESE 13ABLab 02: Code Coverage CLO-02: Apply patterns, frameworks, and techniques for SoftwareConstruction.CLO-04: Use modern tools such as Eclipse, NetBeans etc. for softwareconstruction.Date: 16thSep 2024Time: 10:00 AM - 12:50 PM 02:00 PM – 04:50 PMInstructor: Dr. Mehvish RashidLab Engineer: Mr. Aftab Farooq
Background image
SE-314: Software ConstructionPage 2Introduction:Lab02:CodeCoverageStudents will have hands-on experience of coverage and unit testing tools such as EclEmma andJUnit.Lab Objectives:The objective of this lab is to get a practical understanding and knowledge of the unit testing andcoverage tools. After the completion of this lab, students will be able to apply unit testing tools.Helping Material:https://ocw.mit.edu/ans7870/6/6.005/s16/getting-started/https://github.com/junit-team/junit4/wiki/Download-and-Installhttps://github.com/junit-team/junit4/wiki/Getting-startedhttps://www.codejava.net/testing/junit-tutorial-for-beginner-with-eclipseLab TasksTask 1:Then create a new Java class called Hailstone.java containing this code:public class Hailstone {public static void main(String[] args) {int n = 3;while (n != 1) {if (n % 2 == 0) {n = n / 2;} else {n = 3 * n + 1;}}}}Run this class with EclEmma code coverage highlighting turned on, by choosing Run / Coverage As / Java Application.
Background image
SE-314: Software ConstructionPage 3By changing the initial value of n, you can observe how EclEmma highlights different lines of code differently.When n=3 initially, what color is the line n = n/2 after execution? When n=16 initially, what color is the line n = 3 * n + 1 after execution?What initial value of n would make the line while (n != 1) yellow after execution?Task 2:For the below Vowels class, construct JUnit tests considering the following Testingstrategy://// Testing strategy:////// Partition for vowelsIn(letters) -> result://// letters.length: 0, 1, >1// result.length: 0, 1, >1// letters contains repeated vowels or doesn't//// covers letters.length=0, result.length=0, no repeated vowels public class Vowels {/***@paramletters a string oflowercase English letters*@returnthesetofvowelsfoundinletters*/public static String getVowelsIn(String letters) {final String VOWELS = "aeiou";String vowelsFound = "";for (int i = 0; i < VOWELS.length(); ++i) {char vowel = VOWELS.charAt(i);if (letters.indexOf(vowel) != -1) {vowelsFound += vowel;}}return vowelsFound;}}
Background image
SE-314: Software ConstructionPage 4Task 3: Improve Code Coverage for Hailstone.java and Vowels.javaObjective:The objective of this task is to ensure that all possible execution paths (branches) of the code are covered by tests. This will allow you to get hands-on experience with achieving high code coverage and understanding how different tests impact coverage.Step 1: Achieving 100% Code Coverage for Hailstone.java1.Analyze Code Coverage: After running the initial version of Hailstone.java with EclEmma, you will notice some lines may not be fully covered. For instance, the conditionsinside the if-else statements (such as n = n / 2 and n = 3 * n + 1) may be partially covered, depending on the initial value of n.2.Task: Modify the Hailstone.java class so that you cover all possible branches of the code. Write a few test cases with different initial values of n to ensure all branches (both the if and else conditions) are executed at least once.oTry different starting values of n (e.g., 2, 3, 5, 16) to observe the coverage in different cases.oAim to achieve 100% line and branch coverage using EclEmma.3.Question: After running the tests, identify the value of n that ensures all branches in the if-else block are fully covered (marked green in EclEmma). What combination of initial values is required to cover all conditions?Step 2: Achieving High Code Coverage for Vowels.java1.Analyze Code Coverage: The Vowels.java class might not achieve full code coverage using a few basic test cases. For example, cases where the string contains repeated vowels or no vowels at all may not be covered.2.Task: Add additional JUnit tests to increase the coverage of Vowels.java. Create test cases for the following conditions:- Empty string (already suggested).- A string with no vowels (e.g., "bcdfg").- A string containing all vowels.- A string with repeated vowels (e.g., "aaaaaaa").- A string with a mix of vowels and consonants (e.g., "education").3.Question: After running the tests with EclEmma, which cases helped improve the code coverage for the getVowelsIn() method? What additional edge cases did you cover to achieve higher coverage?
Background image
Source Code: Make a word document and zip your source code and upload it as well.Task1: Provide screen shots of attained coverage by Ecl Emma tool for each question.Task2: Provide JUnit Test File containing test cases according to given Test Strategy.SolutionSE-314: Software ConstructionPage 5Answer:Deliverables:Compile a single word document by filling in the solution part and submit this Word file on LMS. Incase of any problems with submissions on LMS, submit your Lab assignments by emailing it toaftab.farooq@seecs.edu.pk.
Background image