CSC 213 - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

CSC 213

Description:

Discuss automated method of testing code. Java-based approach ... No spaces, blank lines, ifs, ands, or buts. Can be odd at first, but will adapt very quickly ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 12
Provided by: wwwcsCa
Category:
Tags: csc | buts | guis

less

Transcript and Presenter's Notes

Title: CSC 213


1
CSC 213 Large Scale Programming
  • Lecture 5
  • Testing jUnit

2
Todays Goal
  • Discuss automated method of testing code
  • Java-based approach to define test cases
  • Show how to run these test cases
  • How often code should be tested
  • Present methods of handling failed tests
  • How to find the source of the bug
  • What can be done to fix bugs once they occur

3
Assertions
  • Tests that a boolean property holds
  • Can be placed anywhere in a method
  • Will be tested each time method is executed
  • Can slow down performance, since always tested
  • But very useful while program is incomplete
  • Assertions added or removed at runtime
  • Runs will test assertions if run with ea
    argument
  • Otherwise assertions will not be run
  • But tracking bugs source can be difficult

4
Automated Testing
  • Automation important for several reasons
  • Allows for testing as code is written
  • Keeps code in working order as it is developed
  • Prevent regression failures as project continues
  • Using JUnit for unit testing
  • Written in Java and incorporated into Eclipse
  • Simplifies process of performing checks
  • Working in Java enables writing precise
    detailed tests

5
Format of JUnit Test Cases
  • Define test class for each class tested
  • Include one method for each test case
  • Helpfully, JUnit runs all tests within a class
  • Eclipse can execute all test classes in a project
  • Requires Java annotations for this to work
  • Annotations add information to individual methods
  • Annotation immediately precede method
  • No spaces, blank lines, ifs, ands, or buts
  • Can be odd at first, but will adapt very quickly

6
Running JUnit Tests
  • Begins by executing _at_BeforeClass method
  • Then for each method marked _at_Test
  • Run all methods marked _at_Before
  • Perform any setup you would like for your tests
  • Execute the one _at_Test method
  • Run all methods marked _at_After
  • These close files other cleanup from _at_Before
  • Finally executes _at_AfterClass method

7
JUnit assert____ Methods
  • Actual tests performed using these methods
  • assertTrue(boolean test)
  • assertFalse(boolean test)
  • assertSame(Object expected, Object actual)
  • assertNotSame(Object expected, Object actual)
  • assertNull(Object object)
  • assertNotNull(Object object)
  • fail()

8
Other Important Tests
  • When working properly, tests defined first
  • Tests had better fail until the code actually
    written
  • But do not want to see these as failures, yet
  • Tell JUnit to skip the test with _at_Ignore
    annotation
  • Also test that methods fail correctly
  • Must state thrown exception in _at_Test
    annotation_at_Test(expectedMyException.class)
  • Only passes test if MyException exception thrown

9
My Test Failed, Now What?
  • Run your tests often
  • If only 1 line changes, source of bug is on that
    line
  • If 1000 lines change, bug is somewhere
  • Keep test cases simple
  • Limit number of causes for the failure
  • Trace your code by hand to find error or
  • Use System.err.println to follow values
  • Double-check that preconditions are met
  • If not, check methods for where preconditions fail

10
Daily Activity
  • Finish documenting methods in the Board class and
    start writing assertions unit tests

11
For Next Lecture
  • Discuss automated method of testing code
  • Java-based approach to define test cases
  • Provide simple approach to run test cases
  • Discuss range of possible tests to perform
  • How often code should be tested
  • Begin discussing GUIs
  • How to implement in Java
  • What event-driven programming is
Write a Comment
User Comments (0)
About PowerShow.com