JUnit Unit Test & JUnit Execution Examples 200511349 장기웅 200511300 강정희 200511310 김진규 200711472 진교선
Content 1. Unit Testing 1. Concept of TDD 2. Concept of Unit Testing 3. Unit Test Benefit & Limitation 4. Unit Test Diagram 2. JUnit 1. JUnit Annotation 2. JUnit Method 3. JUnit Examples 1. JUnit Example 1 2. JUnit Example 2 3. JUnit Example 3 4. References
Unit Testing 1. Concept of TDD 2. Concept of Unit Testing 3. Unit Test Benefit & Limitation 4. Unit Test Diagram
Concept of TDD TDD Test Driven Development Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle 자동화된 Unit Test 를요구 Code 작성이전에만들어진 Test Case 를요구 Test 의통과여부가개발진행과정을통제한다.
Concept of TDD Extreme Programming Extreme Programming (XP) is the best-known agile method. XP is a test-first development Extreme Programming 은대표적은 TDD method 이다. 새로운 Version 이계속 Release 되는방식이다. 모든 Version 에대해 Test 가이루어진다. 이러한테스트의성공여부가 Version 의수용여부를결정한다.
Concept of Unit Testing What is Unit the smallest testable part of an application. In procedural programming a unit may be an individual function or procedure. In object-oriented programming a unit is usually a method. created by programmers or occasionally by white box testers. Application 에서테스트할수있는최소단위 Procedure-Oriented Programming OOP Function Procedure Method - Unit of Each Programming Style
Concept of Unit Testing (Cont.) What is Unit Testing In computer programming, unit testing is a method by which individual units of source code are tested to determine if they are fit for use. 단위코드에서문제발생소지가있는부분을테스트 보통클래스의 Public Method 를테스트 이상적인테스트는코딩전에 Test Case 를작성 Test 는개발후에하는것이아니라개발단계에서수행 Test 를통한 Code 의 Robustness 를보장!!!!
Unit Test Benefits & Limitations Benefits of Unit Testing Facilitate Change Unit testing provides a sort of living documentation of the system. 작은단위의테스트이기때문에 Code Refactoring 의부담이줄어든다. 또한다른 Unit 이올바르게작동한다는것을보장하기도한다. Simplifies Integration Unit testing may reduce uncertainty in the units themselves and can be used in a bottom-up testing style approach Bottom Up 방식의 Unit 통합에서 Unit 의불확실함을줄여준다. 올바르게작동하는것이보장된 Unit 의통합을통해통합시발생하는문제점들을줄여준다.
Unit Test Benefits & Limitations Benefits of Unit Testing Documentation Unit testing provides a sort of living documentation of the system. Unit Test 는자동화된테스트과정이기때문에유닛의적절 / 부적절한작동에대하여정보를문서화시켜제공해준다. Design When software is developed using a test-driven approach, the unit test may take the place of formal design. Unit Test 는 TDD 에서디자인과같은위치를가진다. Unit Test 의 Pass/Non-Pass 여부가개발의진행과정을결정하게된다. 또한 Test Case 의특성이 Unit 의특성을결정하고, 이러한 Unit 들이전체소프트웨어의특성이된다.
Unit Test Benefits & Limitations Limitation of Unit Testing Disable to catch all errors it is impossible to evaluate every execution path in all but the most trivial programs. The same is true for unit testing. Test Case 가모든 Error 를잡아낼수없다. 또한 Unit 에대한테스트만을하므로통합과정에서의에러를잡아낼수없다. Size of Unit For every line of code written, programmers often need 3 to 5 lines of test code. 각테스트과정에는 Unit 에대한테스트코드가삽입되는데, Unit 의분리가확실하지않거나적절하지못한경우중복된테스트가발생하거나테스트코드를작성하는데드는비용 ( 시갂 + 돈 ) 이증가할가능성이있다.
JUnit 1. JUnit Annotation 2. JUnit Method
J Unit Annotation @Test 실행할 method 앞에붙임. @Test(expected) 발생할것으로예상되는예외를지정한다. 예외가생기지않으면실패. @Test(timedout) 테스트가끝나는시갂을예측하여지정된 시갂보다길게테스트가진행되면실패.
J Unit Annotation @Ignore 다음에오는테스트를무시함. 테스트를하지 않을 method 앞에붙임.( 일종의주석처리와 같은역할을함 ) @Before, @After 각단위테스트 method 의실행앞뒤에서초 기화와자원정리작업을수행 @BeforeClass, @AfterClass 각단위테스트클래스수행전후에초기화와 자원정리작업을수행
J Unit Annotation @RunWith 지정된러너가아닌사용자가지정된러너를 통해특정클래스를실행하게해준다. @SuiteClasses 테스트하려고하는여러클래스를지정한다. @Parameters 여러개의파라미터값을테스트하려고할 때자동적으로테스트를실행한다.
J Unit Method assertequals 변수나객체의값이같은지비교한다. assertnull Null 값을리턴하는지비교 assertnotnull 인자로넘겨받은객체가 null 인지판정 하고반대인경우실패로처리한다.
J Unit Method assertsame assertsame 은 expected 와 actual 이같은객체를참조하는 지판정하고그렇지않다면실패로처리한다. assertnotsame Expected 와 actual 이서로 다른 객체를참조하는지판정 하고, 만약같은객체를참조한다면실패로처리한다. asserttrue Boolean 조건이참인지판정한다. 만약조건이거짓이라면 실패로처리한다. assertfalse Boolean 조건이거짓인지판정한다. 만약조건이참이라면 실패로처리한다.
JUnit Examples 1. JUnit Example1 2. JUnit Example2 3. JUnit Example3
JUnit Example 1 Target class public class targettest { public static int plus(int x, int y){ return x + y; public static int minus(int x, int y){ return x - y; public static String getstring(){ return "TEST"; Integer Return Method String Return Method
JUnit Example 1 Making Test Case Junit Test Case 생성
JUnit Example 1 Making Test Case (Cont.) Test 할 Source Test 할 Case
JUnit Example 1 Making Test Case (Cont.)
JUnit Example 1 Implemented Test Case Code import static org.junit.assert.*; import org.junit.*; public class Junit4Test { @Test public void testplus(){ int result = targettest.plus(1, 2); assertequals(result, 3); @Test public void testminus(){ int result = targettest.minus(2, 1); assertequals(result, 1); @Test public void testgetstring(){ arrertequals 를활용한 Testing assertequals("test", targettest.getstring());
JUnit Example 1 Test Case Runing Result Test 성공시초록색으로표시
JUnit Example 1 Test Case Runing Result (Fail) - assertequals() 의변수값을조정하여고의적으로 Fail 이나게조정하였다. Test 실패시붉은색으로표시 Error 메시지확인
JUnit Example 2 Example of Executing Annotations import static org.junit.assert.*; import org.junit.*; public class Junit4Test { static private int i, j; @BeforeClass public static void ExeBeforeTest() { i = 3; j = 2; System.out.println("Execute @BeforeClass"); @Before public void TestAdd() { int k = i + j; assertequals(5, k); System.out.println("Execute @Before"); BeforeClass Annotation 은전체 Test 에서 1 회실행된다 Before Annotation 은 Unit Test 마다 1 회실행된다
JUnit Example 2 Example of Executing Annotations @Test public void TestSub() { int k = i - j; assertequals(1, k); System.out.println("Execute @Test_1st"); @Test public void Test2nd() {System.out.println("Execute @Test_2nd"); @After public void TestMul() { int k = i * j; assertequals(6, k); System.out.println("Execute @After"); @AfterClass public static void ExeAfterTest() { System.out.println("Execute @AfterClass"); After Annotation 은 Unit Test 마다실행된다 AfterClass Annotation 은전체 Test 에서 1 회실행된다
JUnit Example 2 Example of Executing Annotations 각 Annotation 에대한실행수와실행우선순위를알수있다.
JUnit Example 3 Timeout Examples import org.junit.*; public class Junit4Test { @Test(timeout = 3000) public void testtimeout1() throws Exception { Thread.sleep(2000);// success @Test(timeout = 3000) public void testtimeout2() throws Exception { Thread.sleep(4000);// failure Timeout 값보다 Sleep 값이적다. Timeout 값보다 Sleep 값이크다.
References http://pentium3538.springnote.com/pages/174356 http://asisis.tistory.com/316 http://www.javajigi.net/pages/viewpage.action?pageid=278 http://devtainer.blogspot.com/2010/11/junit-4x.html http://www.java2go.net/blog/archive/20080106 http://scroogy.tistory.com/30 http://en.wikipedia.org/wiki/unit_test http://en.wikipedia.org/wiki/junit http://www.javajigi.net/pages/viewpage.action?pageid=278