03 testing v2

4
Programming Methodology A ti Assertion Testing คืออะไร Test-Driven Development (TDD) JUnit JU T El JUnit Testing in Eclipse assertion – เงื อนไขที องเป ็ นจริง จุดใดจุดหนึ งของ โปรแกรม เมื อเงือนไขเป ็ นเท็จ (false) จะเรียกว assertion นั “fail” assert Expression 1 ; assert Expression 1 : Expression 2 ; assert Expression 1 : Expression 2 ; เมfailจะ throw AssertionError i เมอ fail จะ throw AssertionError (1) ไม มีรายละเอียด () แสดงรายละเอยดตาม E i ression 2 (2) แสดงรายละเอยดตาม Expression 2 อย าใช assert เพอยาใช assert เพอ ตรวจสอบ argument ของ public method ทํางานในคําสงทโปรแกรมต องทํา assert name.remove(null); // assert can be disabled. assert name.remove(null); // assert can be disabled. แก เป็น b l ll d ( ll) boolean nullsRemoved = name.remove(null); assert nullsRemoved; เพราะเรา disable assertion Internal Invariants if (i % 3 == 0) { if (i % 3 == 0) { ... } else if (i % 3 == 1) { ... ... } else if (i % 3 == 1) { ... } else { // We know (i % 3 == 2) ... } else { assert i % 3 == 2 : i; ... } } Control-Flow Invariants ใช ในจดทคาดว าจะไปไม ( ll b h d) ใชในจดทคาดวาจะไปไมถง (will not be reached) assert false; void foo() { for (...) { if ( ) if (...) return; } void foo() { // Execution should never reach this point!!! } for (...) { if (...) return; return; } assert false; // Execution should never reach this point! } } Preconditions/postconditions /** * Sets the refresh rate. * *@ t f h t i f d *@param rate refresh rate, in frames per second. * @throws IllegalArgumentException if rate <= 0 or * rate > MAX_REFRESH_RATE. */ public void setRefreshRate(int rate) { // Enforce specified precondition in public method if (rate <= 0 || rate > MAX_REFRESH_RATE) throw new IllegalArgumentException("Illegal rate: " + rate); setRefreshInterval(1000/rate); setRefreshInterval(1000/rate); } Preconditions/postcondition /** * Sets the refresh interval (which must correspond to a * legal frame rate). * * @param interval refresh interval in milliseconds. */ / private void setRefreshInterval(int interval) { // Confirm adherence to precondition in nonpublic method // Confirm adherence to precondition in nonpublic method assert interval > 0 && interval <= 1000/MAX_REFRESH_RATE : interval; ... // Set the refresh interval } Bdf lt ti di bl d t ti By default, assertions are disabled at runtime. To enable, use the -enableassertions, or -ea, switch. To disable, use the -disableassertions, or -da, switch. E bl d bl ll l no arguments - Enables or disables assertions in all classes except system classes.

Upload: chate-patanothai

Post on 28-Nov-2014

132 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 03 Testing v2

Programming Methodology

• A ti• Assertion• Testing คออะไรg• Test-Driven Development (TDD)• JUnit• JU T E l• JUnit Testing in Eclipse

• assertion – เงอนไขทตองเปนจรง ณ จดใดจดหนงของโปรแกรม • เมอเงอนไขเปนเทจ (false) จะเรยกวา assertion นน “fail”

assert Expression1 ;

assert Expression1 : Expression2 ;assert Expression1 : Expression2 ;

เมอ “fail” จะ throw AssertionError

i

เมอ fail จะ throw AssertionError(1) ไมมรายละเอยด( ) แสดงรายละเอยดตาม E iression2(2) แสดงรายละเอยดตาม Expression2

• อยาใช assert เพออยาใช assert เพอ– ตรวจสอบ argument ของ public method

– ทางานในคาสงทโปรแกรมตองทาassert name.remove(null); // assert can be disabled.assert name.remove(null); // assert can be disabled.แกเปนb l ll d ( ll)boolean nullsRemoved = name.remove(null);assert nullsRemoved;

ไ • เพราะเรา disable assertion ได

• Internal Invariants

if (i % 3 == 0) {  

if (i % 3 == 0) {  ... 

} else if (i % 3 == 1) { ... 

... } else if (i % 3 == 1) { 

... } else { 

// We know (i % 3 == 2)... 

} else { assert i % 3 == 2 : i;... 

} }

• Control-Flow Invariantsใชในจดทคาดวาจะไปไมถง ( ll b h d)• ใชในจดทคาดวาจะไปไมถง (will not be reached)assert false;;

void foo() { for (...) { 

if ( )if (...) return; 

} void foo() { // Execution should never reach this point!!!

}

() {for (...) { 

if (...) return; return; 

} assert false; // Execution should never reach this point!

} } 

• Preconditions/postconditions/** * Sets the refresh rate. * * @ t   f h  t  i  f     d  * @param rate refresh rate, in frames per second. * @throws IllegalArgumentException if rate <= 0 or * rate > MAX_REFRESH_RATE. */ public void setRefreshRate(int rate) { 

// Enforce specified precondition in public methodp p pif (rate <= 0 || rate > MAX_REFRESH_RATE) 

throw new IllegalArgumentException("Illegal rate: " + rate); setRefreshInterval(1000/rate); setRefreshInterval(1000/rate); 

• Preconditions/postcondition/** * Sets the refresh interval (which must correspond to a * legal frame rate). g )* * @param interval refresh interval in milliseconds. */ / private void setRefreshInterval(int interval) { 

// Confirm adherence to precondition in nonpublic method// Confirm adherence to precondition in nonpublic methodassert interval > 0 && interval <= 

1000/MAX_REFRESH_RATE : interval; ... 

// Set the refresh interval } 

• B d f lt ti di bl d t ti• By default, assertions are disabled at runtime. • To enable, use the -enableassertions, or -ea, switch. • To disable, use the -disableassertions, or -da, switch.

• E bl d bl ll l• no arguments - Enables or disables assertions in all classes except system classes. y

Page 2: 03 Testing v2

• packageName Enables or disables assertions in the namedpackageName... - Enables or disables assertions in the named package and any subpackages.

• ... - Enables or disables assertions in the unnamed package in the current working directory. g y

• className - Enables or disables assertions in the named class• For example, the following command runs a program, BatTutor,

with assertions enabled in only package com.wombat.fruitbat and y p gits subpackages: java ‐ea:com.wombat.fruitbat... BatTutor

• กระบวนการในการเปรยบเทยบระหวาง “ผลท• กระบวนการในการเปรยบเทยบระหวาง ผลทได” กบ “สงทควรจะเปน”

• จาก IEEE Standard 610.12-1990, "IEEE Standard Glossary of Software Engineering Terminology“g g gy

"The process of operating a system or component under specified conditions, observing or recording the results, and making an evaluation of some aspect of the system or component."

• E i ทาอะไรจะตอง • Engineer ทาอะไรจะตอง – มการวางแผน– ทาการออกแบบตามมาตรฐาน– ตามมาตรฐาน

– ลงมอทา และ

–มแผนทดสอบมแผนทดสอบ

• ตองไดรบการออกแบบมาเปนอยางด• ตองไดรบการออกแบบมาเปนอยางด• Inputs คออะไร มอะไรบางp• Outputs ทคาดหวงของ input แตละตว• Order of execution

di– cascading test cases– independent test cases

Unit Testing Integration Unit Testing Testing

System Acceptance   yTesting

pTesting

• ทดสอบหนวยทเลกทสดของโปรแกรม• C++, Java คลาส

C f• C function• ทาไปพรอมกบการ

โ Unit Testing Integration 

Testingพฒนาโดยผพฒนา

• เพอความมนใจในโปโปรแกรม

• ตองผาน 100%System Testing

Acceptance  Testing

• based on requirements and specifications• ไมตองรรายละเอยดภายในของโปรแกรมทจะทดสอบ

Black boxทดสอบ

• ทดสอบ internal path, structure และ implementation• ตองการความสามารถทางดาน Programming สงWhite boxWhite box• แอบดนดนง จนพอเขาใจการ implement แลวGrey box • แอบดนดนง จนพอเขาใจการ implement แลวกทาตาม black boxGrey box

•0 ‐ 59F• จะตองทดสอบอะไรบาง?• ตองทดสอบคะแนนไหน

•60 ‐ 69D• ตองทดสอบคะแนนไหนบาง?

60  69D

•70 79C •70 ‐ 79C

•80 ‐ 89B

•90 ‐ 100A

• P iti t t• Positive test case– Valid input valid resltp– Functionality

• Negative test caseg– Invalid input/condition– Robustness test

• จานวน negative อาจจะมากกวา positive

Page 3: 03 Testing v2

• Boundary Value Analysis• Equivalence Partitioning/Class

• ทดสอบทงหมด ทาไมไหวq g

• Logic Coverage• มอกหลายวธ (แตไมสอน)

• เปลองแรง เวลา เงน และ ทรพยากรอนๆ• มอกหลายวธ (แตไมสอน)

เชน– all pair testing– all-pair testing– cause-effect– decision tabledecision table– state transition– domain analysisdomain analysis– ฯลฯ

• boundary conditionอยตรงขอบเขตพอด– อยตรงขอบเขตพอด

– อยสงกวาขอบเขต 1 จด

– อยตากวาขอบเขต 1 – อยตากวาขอบเขต 1 จด

• ของ input และ output ของ equivalence classของ equivalence class

• ปรบ จดแบง equivalence class 0 590 59Fclass

• หา boundary value ของทงหมด

• 0 ‐ 59• 0 ‐ 59F

• 60 ‐ 69• 60 ‐ 69D‐1,    0,   1, 58, 59,   60, ‐1,    0,   1, 58, 59,   60, ทงหมด

• 70 ‐ 79• 70 ‐ 79C59, 60, 61, 68, 69,   70, 69, 70, 71, 78, 79,   80, 79, 80, 81, 88, 89,   90,

59, 60, 61, 68, 69,   70, 69, 70, 71, 78, 79,   80, 79, 80, 81, 88, 89,   90,

• 80 ‐ 89• 80 ‐ 89B

A

, , , , , ,89, 90, 91, 99, 100, 101

, , , , , ,89, 90, 91, 99, 100, 101

• 90 ‐ 100• 90 ‐ 100A

• ลดจานวนกรณทดสอบลง โดยยงครอบคลมทงหมด– แบงเปน partition/class หรอ กลมp – แตละกลมทดสอบสงเดยวกน– ถามกรณทดสอบไหนเจอ bug กรณอนในกลมเดยวกนกจะถามกรณทดสอบไหนเจอ bug กรณอนในกลมเดยวกนกจเจอเหมอนกน

– ถาไมเจอ กรณอนกจะไมเจอ

• จะทดสอบกรณไหนบาง– เฉพาะ valid input หรอ

• 0 ‐ 59F

• 60 69Dp– ทง valid และ invalid input

• Design by contract

• 60 ‐ 69D

• 70 ‐ 79CDesign by contract– preconditions/postconditionsทดสอบเฉพาะ lid i

• 80 ‐ 89B– ทดสอบเฉพาะ valid input– ต.ย. ม 5 สวน • 90 ‐ 100A

• Defensive design– ทดสอบหมด

invalid input• อาจจะแบงเปน equivalence partition ยอยๆ ไดอก

– ต.ย. ม 5 + 1 สวน ยอยๆ ไดอก

• แตกตางกนตามชนดของ input (สาหรบ defensiveแตกตางกนตามชนดของ input (สาหรบ defensive design)

• continuous rangecontinuous range– 1 valid + 2 invalids (below/above)

• discrete values within a range– 1 valid + 2 invalids

• แตกตางกนตามชนดของ input (สาหรบ defensiveแตกตางกนตามชนดของ input (สาหรบ defensive design)

• set of input valuep– 1 valid + 1 invalid

• “must be” situation– “fi t h t t b l tt ”– e.g. first character must be a letter– 1 valid + 1 invalid

• หา i l titi / l ของ• หา equivalence partition/class ของ– รหสไปรษณย– username

• ยาว ๘ อกขระ ตองขนตนดวยตวอกษร และตองม• ยาว ๘ อกขระ ตองขนตนดวยตวอกษร และตองมทงตวอกษรและตวเลขผสมกนสป ส– รหสประจาตวนสต

• ๑๐ หลก

• E th i th i t d t l t• Every path in the program is executed at least once

Page 4: 03 Testing v2

• T t D i D l t (TDD) เปนวธปฏบตอยางหนง• Test-Driven Development (TDD) เปนวธปฏบตอยางหนงของ Extreme Programming (XP)– Kent Beck และ Ron Jeffries

• ทาใหกระบวนการพฒนาซอฟตแวรงายขน ทาใหกร บวนการพฒนาซอฟตแวรงายขน กระชบขน และมวงจรการพฒนาอยางตอเนอง

• เปนเครองมอทใชชวยในการทา it t t สาหรบ• เปนเครองมอทใชชวยในการทา unit test สาหรบภาษา Java

• open source (www.junit.org, junit.sourceforge.net)ส • Framework มาตรฐานสาหรบการทา unit test

สาหรบภาษา JavaJ• ทาการทดสอบแบบ black box

• สรางคลาสทจะเปนตวทดสอบ• สรางคลาสทจะเปนตวทดสอบ• ภายในคลาสจะประกอบดวยเมทอดตางๆ มากมาย• ตองการใหเมทอดไหนเปนกรณทดสอบ กเตม

@ j it T t (หรอ @T t แตตองม i t j it T t ท@org.junit.Test (หรอ @Test แตตองม import org.junit.Test ทตนไฟล

• สามารถเขยน 1 test method สาหรบ 1 กรณทดสอบ หรอ 1 test method สาหรบหลายกรณทดสอบกไดหรอ 1 test method สาหรบหลายกรณทดสอบกได– ถาเปน cascading test กม test method ทเรยก test ยอย

ตามลาดบ– ถาเปน independent test กแยกเมทอดกนไดp

• เมอตองการทดสอบคาตางๆ กจะใช เมทอด เมอตองการทดสอบคาตางๆ กจะใช เมทอด org.junit.Assert.assertXXXX ทาการทดสอบ

• เตม i t j it A t * ทตนไฟล จะไดไมตองพมพชอ• เตม import org.junit.Assert.*; ทตนไฟล จะไดไมตองพมพชอยาวแตล จ มเงอนไขทจ เปนจรงแตกตางกนออกไป• แตละ assert จะมเงอนไขทจะเปนจรงแตกตางกนออกไป

• ถาไมผาน จะเกด AssertionError

assertEquals assertFalse assertNotNull assertNotSame

assertNull assertSame assertTrue fail

Assertion ผานเมอassertTrue พารามเตอรเปน trueassertFalse พารามเตอรเปน falseassertFalse

assertNull พารามเตอรเปน nullพารามเตอรไมเปน llassertNotNull พารามเตอรไมเปน null

assertEquals พารามเตอรทงคเทากน ตาม ใช การเรยกใช equals

assertSame พารามเตอรทงคคอ object เดยวกนassertNotSame พารามเตอรทงคเปนคนละ objectfail Fail เสมอfail Fail เสมอ

• เมอตองการทดสอบวาม exception ทคาดหวงเกดขนเมอตองการทดสอบวาม exception ทคาดหวงเกดขนหรอไม

• ตอนเพม @Test ใหเพมรายละเอยดดงน

@Test (expected=YourExpectedException.class)@ ( p p p )

• บางครงเราตองสราง object ของคลาสตางๆ เพอบางครงเราตองสราง object ของคลาสตางๆ เพอใชในการทดสอบหลายๆ อน ทาใหตองกาหนด local variable และ initialize ซาๆ เหมอนกนในหลายlocal variable แล initialize ซาๆ เหมอนกนในหลายเมทอด

• เปลยน local variable เหลานนมาเปน field ของคลาสเปลยน local variable เหลานนมาเปน field ของคลาสทดสอบ

• เขยนเมทอดทจะทาการ i iti li fi ld เหลานน• เขยนเมทอดทจะทาการ initialize field เหลานน• เพม @org.junit.Before หรอ @Before (ถาม import

f แลวorg.junit.Before แลว)• เมทอดทถก กาหนดดวย @Before จะถกเรยกทก

ครง กอนททกๆ เมทอดทกาหนดดวย @Test

• S t JU it โดยการ dd JU it lib ใน J B ild• Setup JUnit โดยการ add JUnit library ใน Java Build Path ของโปรเจค

• Demo คลาส Grade.java

• Johannes Link and Peter Frohlick Unit Testing in Java: How testsJohannes Link and Peter Frohlick, Unit Testing in Java: How tests Drive the Code, Morgan Kaufmann Publishers, 2003.

• Russell Gold Thomas Hammell and Tom Snyder Test Driven• Russell Gold, Thomas Hammell and Tom Snyder, Test Driven Development: A J2EE Example, Apress, 2005.

• L C l d A P i i ' G id S f T D i A h• Lee Copeland, A Practitioner's Guide to Software Test Design, ArtechHouse, 2004.

• Lasse Koskela, Test Driven: TDD and Acceptance TDD for Java Developers, Manning, 2007.

• JavaTM 2 SDK, Standard Edition Documentation Version 1.4.2, Programming With Assertions, Available: http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html [Access July 23, 2008].