Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSC309 Lecture 11

CSC309 Lecture 11

Software Engineering II
Unit Testing
(202302)

Javier Gonzalez-Sanchez
PRO

February 05, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSC 309
    Software Engineering II
    Lecture 11: Software Testing
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    users.csc.calpoly.edu/~javiergs | javiergs.com
    Building 14 -227
    Office Hours: By appointment

    View Slide

  2. jgs
    Software Testing
    Unit Testing

    View Slide

  3. jgs
    Fall 2022 | 00000001
    Roadmap
    idea
    requirements
    architecture
    design
    code
    unit testing
    quality measure

    View Slide

  4. jgs
    Fall 2022 | 00000001
    Definition
    Testing a unit - the smallest
    piece of code that can be
    logically isolated in a system.
    (a method in Java)

    View Slide

  5. jgs
    Fall 2022 | 00000001
    Source Code

    View Slide

  6. jgs
    Fall 2022 | 00000001
    Test Case

    View Slide

  7. jgs
    Fall 2022 | 00000001
    JUnit assertion methods
    § Each method can also be passed a string to display if it fails:
    assertTrue(test) fails if the boolean test is false
    assertFalse(test) fails if the boolean test is true
    assertEquals(expected, actual) fails if the values are not equal
    assertSame(expected, actual) fails if the values are not the same (by ==)
    assertNotSame(expected, actual) fails if the values are the same (by ==)
    assertNull(value) fails if the given value is not null
    assertNotNull(value) fails if the given value is null
    fail() causes current test to immediately fail

    View Slide

  8. jgs
    Fall 2022 | 00000001
    Run With Coverage

    View Slide

  9. jgs
    Fall 2022 | 00000001
    Run With Coverage

    View Slide

  10. jgs
    Fall 2022 | 00000001
    ArrayIntList JUnit test
    import org.junit.*;
    import static org.junit.Assert.*;
    public class TestArrayIntList {
    @Test
    public void testAddGet1() {
    ArrayIntList list = new ArrayIntList();
    list.add(42);
    list.add(-3);
    list.add(15);
    assertEquals(42, list.get(0));
    assertEquals(-3, list.get(1));
    assertEquals(15, list.get(2));
    }
    @Test
    public void testIsEmpty() {
    ArrayIntList list = new ArrayIntList();
    assertTrue(list.isEmpty());
    list.add(123);
    assertFalse(list.isEmpty());
    list.remove(0);
    assertTrue(list.isEmpty());
    }
    ...

    View Slide

  11. jgs
    Test-Driven Development
    Example

    View Slide

  12. jgs
    Fall 2022 | 00000001
    Definition
    TDD suggests that the code is
    developed or changed exclusively
    on the basis of Unit Testing.

    View Slide

  13. jgs
    Fall 2022 | 00000001
    Test Case

    View Slide

  14. jgs
    Fall 2022 | 00000001
    Source Code

    View Slide

  15. jgs
    Fall 2022 | 00000001
    Run

    View Slide

  16. jgs
    Fall 2022 | 00000001
    Test Case

    View Slide

  17. jgs
    Fall 2022 | 00000001
    Run

    View Slide

  18. jgs
    Fall 2022 | 00000001
    TDD

    View Slide

  19. jgs
    Fall 2022 | 00000001
    Tips for Testing
    § You cannot test every possible input, parameter value, etc.
    So you must think of a limited set of tests likely to expose bugs.
    § Think about boundary cases
    positive; zero; negative numbers
    right at the edge of an array or collection's size
    § Think about empty cases and error cases
    o0, -1, null; an empty list or array
    § Test behavior in combination
    maybe add usually works, but fails after you call to remove
    make multiple calls; maybe the size fails the second time only

    View Slide

  20. jgs
    Fall 2022 | 00000001
    Trustworthy tests
    § Test one thing at a time per test method.
    10 small tests are much better than 1 test 10x as large.
    § Each test method should have a few (likely 1) assert
    statements.
    If you assert many things, since the first that fails will stop the
    test, you won't know whether a later assertion would have
    failed.
    § Tests should avoid logic.
    ominimize if/else, loops, switch, etc.
    § Torture tests are okay, but only in addition to simple tests.

    View Slide

  21. jgs
    Fall 2022 | 00000001
    Tests and Data Structures
    § Need to pass lots of arrays? Use array literals
    public void exampleMethod(int[] values) { ... }
    ...
    exampleMethod(new int[] {1, 2, 3, 4});
    exampleMethod(new int[] {5, 6, 7});
    § Need a quick ArrayList? Try Arrays.asList
    List list = Arrays.asList(7, 4, -2, 3, 9, 18);
    § Need a quick set, queue, etc.? Many collections can take a list
    Set list
    = new HashSet( Arrays.asList(7, 4, -2, 9));

    View Slide

  22. jgs
    Fall 2022 | 00000001
    Questions

    View Slide

  23. jgs

    View Slide

  24. jgs
    CSC 309
    Software Engineering II
    Lab 11: Unit Testing
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    users.csc.calpoly.edu/~javiergs | javiergs.com
    Building 14 -227
    Office Hours: By appointment

    View Slide

  25. jgs
    Fall 2022 | 00000001
    Think about this
    Test Cases for your lexer
    Test Cases for your parser
    Implement 6 test cases
    (one per team member)

    View Slide

  26. jgs
    Fall 2022 | 00000001
    Lexer
    class Student{
    Hello x; World w() {}
    Input:
    class
    Student
    {
    Hello
    x
    ;
    Output:
    World
    w
    (
    )
    {
    }

    View Slide

  27. jgs
    Fall 2022 | 00000001
    Empty Class
    class Student{}
    Input:
    Classes: Student
    Relationships:
    Output:

    View Slide

  28. jgs
    Fall 2022 | 00000001
    Clas with Global Variable
    class Student {
    Hello world;
    }
    Input:
    Classes: {Student, Hello}
    Relationships: {{Student, has, Hello}}
    Output:

    View Slide

  29. jgs
    Fall 2022 | 00000001
    Inheritance and Variable
    class Student extends Person{
    Hello world;
    }
    Input:
    Classes: {Student, Person, Hello}
    Relationships:{{ Student, has, Hello}, {Student ,is, Person}}
    Output:

    View Slide

  30. jgs
    Fall 2022 | 00000001
    Inheritance, Method and Local Variable
    class Student extends Person{
    Hello world() {
    Computer c = new Computer();
    }
    }
    Input:
    Classes: {Student, Person, Computer}
    Relationships: {{Student, is, Person}, {Student, uses, Computer}}}
    Output:

    View Slide

  31. jgs
    Setup Tutorial
    In case you need it

    View Slide

  32. jgs
    Fall 2022 | 00000001
    PS. You would like to
    a) Create a directory for test cases
    b) Add Junit (version 5 for Java 8 and above)

    View Slide

  33. jgs
    Fall 2022 | 00000001
    Step 1. Select Your Project

    View Slide

  34. jgs
    Fall 2022 | 00000001
    Step 2. Select Your New Folder

    View Slide

  35. jgs
    Fall 2022 | 00000001
    Step 3. Select a Class Name

    View Slide

  36. jgs
    Fall 2022 | 00000001
    Step 4. Generate a Test

    View Slide

  37. jgs
    Fall 2022 | 00000001
    Junit 5

    View Slide

  38. jgs
    Fall 2022 | 00000001
    Step 5. Add Junit Library (if needed)

    View Slide

  39. jgs
    Fall 2022 | 00000001
    Questions

    View Slide

  40. jgs
    Fall 2022 | 00000001
    Let’s Work

    View Slide

  41. jgs
    CSC 309 Software Engineering II
    Javier Gonzalez-Sanchez, Ph.D.
    [email protected]
    Winter 2023
    Copyright. These slides can only be used as study material for the class CSC308 at Cal Poly.
    They cannot be distributed or used for another purpose.

    View Slide