Μεθοδολογίες Προγραμματισμού ΙΙ Έλεγχος Λογισμικού JUNIT - Test First Design
JUnit test and Project 3 simulation
description
Transcript of JUnit test and Project 3 simulation
![Page 1: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/1.jpg)
JUnit test and Project 3 simulation
![Page 2: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/2.jpg)
2
JUnit
The testing problemsThe framework of JUnitA case study
Acknowledgement: using some materials from JUNIT tutorial by Hong Qing Yu (www.cs.le.ac.uk/people/hqy1)
![Page 3: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/3.jpg)
3
The Testing Problems
programmers
Should write
few
Do
Why?
I am so busy
It is difficult
![Page 4: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/4.jpg)
4
The Testing Problems
Programmers need such kind of tool: “Writing a few lines of code, then a test that should run, or even
better, to write a test that won't run, then write the code that will make it run.”
JUnit is that kind of tool!
![Page 5: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/5.jpg)
5
JUnit
The testing problems The framework of JUnitA case study
![Page 7: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/7.jpg)
7
JUnit
The testing problemsThe framework of JUnitA case study
![Page 8: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/8.jpg)
8
A Case Study
Lab3Queue:enQueue methoddeQueue method
![Page 9: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/9.jpg)
9
Include junit library in eclipse
![Page 10: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/10.jpg)
10
How to Write A TestCase using Junit (available in Eclipse 3.1 or later)
Step 1:Create a JUNIT test case (File -> New -> Junit Test Case
![Page 11: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/11.jpg)
11
Create a test case
![Page 12: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/12.jpg)
12
Create a test case
import junit.framework.*;public class Lab3QueueTest {
public void setUp() throws Exception {}
public void tearDown() throws Exception {}
}
![Page 13: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/13.jpg)
13
Create a test case
import junit.framework.*;public class Lab3QueueTest extends TestCase {
Lab3Queue testQueue;int queueSize;public void setUp() throws Exception {
testQueue = new Lab3Queue();queueSize = testQueue.getSize();
}
public void tearDown() throws Exception {}
}
![Page 14: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/14.jpg)
14
Create a test case
For each method that you are going to test:Write a corresponding test method named:
test<method name> in the test case
![Page 15: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/15.jpg)
15
Create a test case
public void testenQueue() {int newItem = 1;queueSize = testQueue.getSize();testQueue.enQueue(newItem);Assert.assertEquals(queueSize+1, testQueue.getSize());int actualItem = ((Integer) testQueue.getLastNode()).intValue();Assert.assertEquals(newItem, actualItem);
}
![Page 16: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/16.jpg)
16
Assert assertEquals(expected, actual) assertEquals(message, expected, actual) assertEquals(expected, actual, delta) assertEquals(message, expected, actual, delta) assertFalse(condition) assertFalse(message, condition) Assert(Not)Null(object) Assert(Not)Null(message, object) Assert(Not)Same(expected, actual) Assert(Not)Same(message, expected, actual) assertTrue(condition) assertTrue(message, condition)
![Page 17: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/17.jpg)
17
Structure
setUp() Storing the fixture's objects in instance variables of your
TestCase subclass and initialize them by overriding the setUp method
tearDown() Releasing the fixture’s
![Page 18: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/18.jpg)
18
Writing a test suite
Step 2: Create a test suite by choosing
![Page 19: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/19.jpg)
19
Writing a test suite
![Page 20: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/20.jpg)
20
Writing a test suite
import junit.framework.Test;import junit.framework.TestSuite;public class AllTests {
public static Test suite() {TestSuite suite = new TestSuite("Test for
AiportSimulation");//$JUnit-BEGIN$suite.addTestSuite(Lab3QueueTest.class);//$JUnit-END$return suite;
}}
![Page 21: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/21.jpg)
21
Running a test
AllTests -> choose Run -> Run As -> Junit Test
![Page 22: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/22.jpg)
22
Running a test
![Page 23: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/23.jpg)
23
Design Test Cases
The real world scenarios The number boundaries
![Page 24: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/24.jpg)
24
TipsTestcases must extend TestCaseAll ‘test’ methods must include at least one call toan assert method or to fail:
assertEquals (String message, ...)assertNotNull (String message, Object obj)assertNull (String message, Object obj)assertSame (String message, Obj exp, Obj
actual)assertTrue (String message, boolean condition)fail (String message)
Remove System.out.println after test cases are working and rely on Junit assert methods to determine success/failure.
![Page 25: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/25.jpg)
25
Dynamic Run
Since JUnit 2.0 there is an even simpler dynamic way. You only pass the class with the tests to a TestSuite and it extracts the test methods automatically.
suite.addTestSuite(Lab3QueueTest.class);
![Page 26: JUnit test and Project 3 simulation](https://reader035.fdocuments.net/reader035/viewer/2022062301/56815ea5550346895dcd349e/html5/thumbnails/26.jpg)
26
Project 3 - Algorithm