1 / 26

JUnit test and Project 3 simulation

JUnit test and Project 3 simulation. JUnit. The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT tutorial by Hong Qing Yu ( www.cs.le.ac.uk/people/hqy1). The Testing Problems. Should write. Do. programmers. few. Why?. I am so busy.

idalee
Download Presentation

JUnit test and Project 3 simulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JUnittest and Project 3 simulation

  2. JUnit • The testing problems • The framework of JUnit • A case study Acknowledgement: using some materials from JUNIT tutorial by Hong Qing Yu (www.cs.le.ac.uk/people/hqy1)

  3. The Testing Problems Should write Do programmers few Why? I am so busy It is difficult

  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!

  5. JUnit • The testing problems • The framework of JUnit • A case study

  6. The Framework of JUnit

  7. JUnit • The testing problems • The framework of JUnit • A case study

  8. A Case Study • Lab3Queue: • enQueue method • deQueue method

  9. Include junit library in eclipse

  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

  11. Create a test case

  12. Create a test case import junit.framework.*; public class Lab3QueueTest { public void setUp() throws Exception { } public void tearDown() throws Exception { } }

  13. Create a test case import junit.framework.*; public class Lab3QueueTest extends TestCase{ Lab3Queue testQueue; intqueueSize; public void setUp() throws Exception { testQueue = new Lab3Queue(); queueSize = testQueue.getSize(); } public void tearDown() throws Exception { } }

  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

  15. Create a test case public void testenQueue() { intnewItem = 1; queueSize = testQueue.getSize(); testQueue.enQueue(newItem); Assert.assertEquals(queueSize+1, testQueue.getSize()); intactualItem = ((Integer) testQueue.getLastNode()).intValue(); Assert.assertEquals(newItem, actualItem); }

  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)

  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

  18. Writing a test suite Step 2: Create a test suite by choosing

  19. Writing a test suite

  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; } }

  21. Running a test AllTests -> choose Run -> Run As -> Junit Test

  22. Running a test

  23. Design Test Cases • The real world scenarios • The number boundaries

  24. Tips Testcases must extend TestCase All ‘test’ methods must include at least one call to an 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.

  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);

  26. Project 3 - Algorithm

More Related