unit testing (lab 6) tool junit on eclipse sdk by asst.prof.dr. wararat songpan(rungworawut) 322 235...

11
Unit Testing (Lab 6) Unit Testing (Lab 6) Tool Junit on Eclipse SDK By Asst.Prof.Dr. Wararat Songpan(Rungworawut) 322 235 Software Testing Department of Computer Science, Faculty of Science Khon Kaen University

Upload: donna-parks

Post on 18-Jan-2018

224 views

Category:

Documents


0 download

DESCRIPTION

Benefit of Junit It is a framework that help to test source code more effectiveness. ◦ Automated Testing ◦ No impact with internal program. ◦ Find defects by source code viewing.

TRANSCRIPT

Page 1: Unit Testing (Lab 6) Tool Junit on Eclipse SDK By Asst.Prof.Dr. Wararat Songpan(Rungworawut) 322 235 Software Testing Department of Computer Science, Faculty

Unit Testing (Lab 6)Unit Testing (Lab 6)Tool • Junit on Eclipse SDK

ByAsst.Prof.Dr. Wararat Songpan(Rungworawut)

322235 Software TestingDepartment of Computer Science, Faculty of

ScienceKhon Kaen University

Page 2: Unit Testing (Lab 6) Tool Junit on Eclipse SDK By Asst.Prof.Dr. Wararat Songpan(Rungworawut) 322 235 Software Testing Department of Computer Science, Faculty

JunitJunitis a tool for unit testing in Java

Language by Erich Gamma and Kent Beck  

open source framework

Page 3: Unit Testing (Lab 6) Tool Junit on Eclipse SDK By Asst.Prof.Dr. Wararat Songpan(Rungworawut) 322 235 Software Testing Department of Computer Science, Faculty

Benefit of JunitBenefit of JunitIt is a framework that help to test

source code more effectiveness.◦Automated Testing◦No impact with internal program.◦Find defects by source code viewing.

Page 4: Unit Testing (Lab 6) Tool Junit on Eclipse SDK By Asst.Prof.Dr. Wararat Songpan(Rungworawut) 322 235 Software Testing Department of Computer Science, Faculty

Example Grade programExample Grade programAssume that we test the program Grade.java

public class Grade { public static char getLetterGrade(int mark) {

if (mark >= 75) { return 'A'; } else if (mark >= 60) { return 'B'; } else if (mark > 50) { return 'C'; } else if (mark >= 30) { return 'D'; } else { return 'F'; } }

public static void main(String[] args) {// TODO Auto-generated method stub System.out.print(getLetterGrade(70));}

123456789101112

4

1

3

5

4

7

6

2

8 9

101112

Page 5: Unit Testing (Lab 6) Tool Junit on Eclipse SDK By Asst.Prof.Dr. Wararat Songpan(Rungworawut) 322 235 Software Testing Department of Computer Science, Faculty

Create Test cases by Junit Create Test cases by Junit Test file is created by Junit and set the name is GradeUnitTest.java

import static org.junit.Assert.*;import org.junit.Test;public class GradeUnitTest {@Test

public void testTypical() { // test a typical value in partitions EC

assertEquals('A', Grade.getLetterGrade(95)); assertEquals('B', Grade.getLetterGrade(72)); assertEquals('C', Grade.getLetterGrade(55)); assertEquals('C', Grade.getLetterGrade(30)); assertEquals('F', Grade.getLetterGrade(20)); }@Test

public void testEC() { // test the boundaries of the partitions EC

assertEquals('A', Grade.getLetterGrade(100));assertEquals('A', Grade.getLetterGrade(75)); assertEquals('B', Grade.getLetterGrade(74));assertEquals('B', Grade.getLetterGrade(60));assertEquals('C', Grade.getLetterGrade(59));assertEquals('C', Grade.getLetterGrade(50));assertEquals('D', Grade.getLetterGrade(49));assertEquals('D', Grade.getLetterGrade(30));assertEquals('F', Grade.getLetterGrade(29));assertEquals('F', Grade.getLetterGrade(0));}

}

Page 6: Unit Testing (Lab 6) Tool Junit on Eclipse SDK By Asst.Prof.Dr. Wararat Songpan(Rungworawut) 322 235 Software Testing Department of Computer Science, Faculty

When junit is executed, the results is shown.

Page 7: Unit Testing (Lab 6) Tool Junit on Eclipse SDK By Asst.Prof.Dr. Wararat Songpan(Rungworawut) 322 235 Software Testing Department of Computer Science, Faculty

Path Testing by EclEmmaPath Testing by EclEmmaEclEMMA is an open-source toolkit for

measuring and reporting Java code coverage.

http://www.eclemma.org/installation.html

Page 8: Unit Testing (Lab 6) Tool Junit on Eclipse SDK By Asst.Prof.Dr. Wararat Songpan(Rungworawut) 322 235 Software Testing Department of Computer Science, Faculty

The Question 1:The Question 1:Why is the program shown Why is the program shown Defects on the screen in part of Defects on the screen in part of Junit running? Please fix it!!Junit running? Please fix it!!

The Question II:The Question II:Note your EclEmma running, Note your EclEmma running, How many percentages of your How many percentages of your unit testing before and after unit testing before and after fixed the program?fixed the program?

Page 9: Unit Testing (Lab 6) Tool Junit on Eclipse SDK By Asst.Prof.Dr. Wararat Songpan(Rungworawut) 322 235 Software Testing Department of Computer Science, Faculty

Please you test the program till like this Pass!

Page 10: Unit Testing (Lab 6) Tool Junit on Eclipse SDK By Asst.Prof.Dr. Wararat Songpan(Rungworawut) 322 235 Software Testing Department of Computer Science, Faculty

Other method in Junit testOther method in Junit testAssert Method is method in Junit that checks test cases will

be alert when do not meet with excected resultsAssert Method Expected Results

assertTrue() When we expect the result is trueassertFalse() When we expect the result falseassertNull() When we expect the result null

assertNotNull() When we expect the result not null

**assertEquals() When we expect the result is equals

assertSame() When we expect the result the same object

assertNotSame() When we expect the result not the same object

Fail() When we expect the result is always fails

Page 11: Unit Testing (Lab 6) Tool Junit on Eclipse SDK By Asst.Prof.Dr. Wararat Songpan(Rungworawut) 322 235 Software Testing Department of Computer Science, Faculty

Lab6Lab6Create Project as Calculate in

sheets Chapter 6 (page 41) and create Junit test follows as table in page 45 to coverage all path as possible.

How many percentages of your Junit code to test Calculate program using EclEmma?