1 / 12

Μεθοδολογίες Προγραμματισμού ΙΙ Έλεγχος Λογισμικού JUNIT - Test First Design

Μεθοδολογίες Προγραμματισμού ΙΙ Έλεγχος Λογισμικού JUNIT - Test First Design. Παναγιώτης Σφέτσος , PhD http://aetos.it.teithe.gr/~sfetsos/ sfetsos@it.teithe.gr. Απλό Παράδειγμα …. Εισαγωγή των κλάσεων της junit Η κλάση Ελέγχου Ο δομητής της κλάσης ( κληρονομικότητα)

kat
Download Presentation

Μεθοδολογίες Προγραμματισμού ΙΙ Έλεγχος Λογισμικού JUNIT - Test First Design

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. Μεθοδολογίες Προγραμματισμού ΙΙ Έλεγχος Λογισμικού JUNIT - Test First Design Παναγιώτης Σφέτσος, PhD http://aetos.it.teithe.gr/~sfetsos/ sfetsos@it.teithe.gr

  2. Απλό Παράδειγμα…. Εισαγωγή των κλάσεων της junit Η κλάση Ελέγχου Ο δομητής της κλάσης (κληρονομικότητα) Η μέθοδος του Test. 1 2 3 4 Παναγιώτης Σφέτσος, Μεθοδολογίες Προγραμματισμού ΙΙ Junit 3.x import junit.framework.*; public class ATest extends TestCase { public ATest(String name) { super(name); } public void testATest() { int answer = 2; assertEquals((1+1), answer); }}

  3. Παράδειγμα – BoxVolume (1/3) Παναγιώτης Σφέτσος, Μεθοδολογίες Προγραμματισμού ΙΙ import junit.framework.*; public class BoxTest extends TestCase { class Box { double width; double height; double depth; Box(double x, double y, double z) { width = x; height = y; depth = z; } double volume() {return width * height * depth;}} public void testAdd() { Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(3, 6, 9); double vol; vol = mybox1.volume(); assertTrue(vol == 3000.00); vol = mybox2.volume(); assertTrue(vol == 162.00); }}

  4. Παράδειγμα – BoxVolume(2/3) Προσέξτε τις εντολές assert… Παναγιώτης Σφέτσος, Μεθοδολογίες Προγραμματισμού ΙΙ

  5. Παράδειγμα – BoxVolume(3/3) Προσέξτε τις εντολές assert…Λάθος στην πρώτη assert. Παναγιώτης Σφέτσος, Μεθοδολογίες Προγραμματισμού ΙΙ

  6. Παράδειγμα – Junit3.x import junit.framework.TestCase; public class TestCalculator extends TestCase { public void testAdd() { Calculator calc = new Calculator(); double result = calc.add(50,10); assertEquals(60, result,0); } } To 0 ονομάζεται παράγοντας Δέλτα και ορίζεται για ένα σίγουρο αποτέλεσμα (περιθώριο τιμών +/-)στα floating point αποτελέσματα. Παναγιώτης Σφέτσος, Μεθοδολογίες Προγραμματισμού ΙΙ

  7. Παράδειγμα – Junit3.x Παναγιώτης Σφέτσος, Μεθοδολογίες Προγραμματισμού ΙΙ

  8. Junit 3.x vs. Junit 4.x • Στο Junit 3.x είχαμε : • 1) κληρονομικότητα (..extends TestCase) • 2) κάθε μέθοδος-Test ξεκινά με την δεσμευμένη λέξη test • 3) άμεση χρήση των Assert-εντολών • Στο Junit4.x έχουμε : • 1) Annotations μετά την Java 5.0 (..όχι κληρονομικότητα) • 2) κάθε μέθοδος-Test δηλώνεται με το annotation @Test • 3) χρήση των Assert-εντολών, με την import (δες παρακάτω) • 4) έλεγχοι με exceptionsπ.χ: • @Test(expected=IndexOutOfBoundsException.class) • 5) έλεγχοι με timeoutπ.χ: • @Test(timeout=1) Παναγιώτης Σφέτσος, Μεθοδολογίες Προγραμματισμού ΙΙ

  9. Junit 3.x vs. Junit 4.x • παράληψη μεθόδου ελέγχου (και με σχόλιο) π.χ: • @Ignore(“to test auto den douleyeiakomi..") • @Test) • ευελιξία : • α) @BeforeClass και @AfterClass • β) @Before και @After: αντί των SetUp()καιTearDown(). • παραμετρικά tests με την χρήση της @Parameters • νέα Assert που υποστηρίζει arrays: • assertEquals(array1, array2); Παναγιώτης Σφέτσος, Μεθοδολογίες Προγραμματισμού ΙΙ

  10. Junit 3.x vs. Junit 4.x • Εκτελείς ελέγχους Junit4 με τον Junit3 – runner, χρησιμοποιώντας • τον Junit4 Adapter: • public static junit.framework.Test suite() { • return new JUnit4TestAdapter(SimpleMathTest.class);} • Όπως και για το Junit3 δεν απαιτείται δομητής. Παναγιώτης Σφέτσος, Μεθοδολογίες Προγραμματισμού ΙΙ

  11. Junit 4.0 - παράδειγμα– 1/2 import org.junit.*; importstatic org.junit.Assert.*; public class RectangleTest { Rectangle r; Rectangle[] rList=new Rectangle[5]; @Before // Ekteleitaiprinapo ka8e Test. public void testSetup() {System.out.println("Arxi/-kopoihsitou test.");} @After // Ekteleitai meta apo ka8e Test. public void testComplete() {System.out.println("Telostou Test.");} Εισαγωγή των κλάσεων της junit Εισαγωγή των Assert Η κλάση Ελέγχου Πριν από κάθε test Μετά από κάθε test 1 2 3 4 5 Παναγιώτης Σφέτσος, Μεθοδολογίες Προγραμματισμού ΙΙ

  12. Junit 4.0 - παράδειγμα– 2/2 @Test public void test1() {r=new Rectangle(); try{ assertTrue("Test 1: Oiarhikes times einailathos.", r.getH()==0 && r.getW()==0); System.out.println("To Test-1 oloklirothike me epityhia.");} catch (AssertionError e){ System.out.println(e.getMessage());} } Η μέθοδος Ελέγχου 6 Παναγιώτης Σφέτσος, Μεθοδολογίες Προγραμματισμού ΙΙ

More Related