Upload
donna-parks
View
220
Download
0
Embed Size (px)
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.
Citation preview
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
JunitJunitis a tool for unit testing in Java
Language by Erich Gamma and Kent Beck
open source framework
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.
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
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));}
}
When junit is executed, the results is shown.
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
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?
Please you test the program till like this Pass!
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
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?