Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Unit Testing CS 414 – Software Engineering I Don Bagert Rose-Hulman Institute of Technology January 16, 2003
  2. 2. Outline <ul><li>Overview of Testing </li></ul><ul><li>Test Plan Creation </li></ul><ul><li>Test Plan Implementation </li></ul><ul><li>Unit Testing Techniques </li></ul><ul><li>Basis Path Testing Steps </li></ul><ul><ul><li>Hands-on Example </li></ul></ul><ul><li>Summary </li></ul>
  3. 3. Overview of Testing <ul><li>Software testing is inherently limited in nature! </li></ul><ul><li>Why? Because there in general there is no method to determine the “perfect” set of test cases </li></ul><ul><li>(It’s a variation of the halting problem) </li></ul><ul><li>That said, the alternatives (e.g. formal proofs of correctness) are very time-consuming and therefore most people use testing </li></ul>
  4. 4. Overview of Testing (continued) <ul><li>The main objective of testing: </li></ul><ul><li>Create test cases that have a high probability of finding an error </li></ul>
  5. 5. Overview of Testing (continued) <ul><li>Important Testing Principles: </li></ul><ul><ul><li>Traceability to customer requirements </li></ul></ul><ul><ul><li>Top-down, early planning </li></ul></ul><ul><ul><li>Bottom-up test Implementation </li></ul></ul>
  6. 6. Test Plan Creation <ul><li>Order: </li></ul><ul><ul><li>Validation Test Plan (from Requirements Specification) </li></ul></ul><ul><ul><li>Integration Test Plan (from Design Document) </li></ul></ul><ul><ul><li>Unit Test Plan (from source code) </li></ul></ul>
  7. 7. Test Implementation <ul><li>Order: </li></ul><ul><ul><li>Unit Test Plan (tests functions and methods) </li></ul></ul><ul><ul><li>Integration Test Plan (test classes and subsystems) </li></ul></ul><ul><ul><li>Validation Test Plan (test entire software) </li></ul></ul>
  8. 8. Unit Testing Techniques <ul><li>Unit and integration testing is generally “glass-box” (also called “white-box”) i.e. it is done with knowledge of the code </li></ul><ul><li>Validation testing is always “black-box” i.e. it is done from the user’s point-of-view, without knowledge of the code </li></ul>
  9. 9. Unit Testing Techniques (continued) <ul><li>One of the most effective methods of glass-box testing at the unit level is basis path testing </li></ul><ul><li>First proposed by Tom McCabe in 1976 </li></ul><ul><li>Uses graph theory to develop a set of test cases which ensures that each statement and branch in a unit is executed in at least one case </li></ul>
  10. 10. Look at the Example Code
  11. 11. Basis Path Testing Steps <ul><li>1. Using the code for the unit, draw the corresponding flow graph </li></ul>
  12. 12. Flow Graph Fundamentals <ul><li>The flow graph is similar to a flowchart </li></ul><ul><li>There are nodes (vertices) for </li></ul><ul><ul><li>Assignment and procedure call statements </li></ul></ul><ul><ul><li>Predicates (conditional expressions) </li></ul></ul><ul><ul><ul><li>If the predicate involves an “and” or an “or”, multiple nodes will be needed </li></ul></ul></ul><ul><ul><li>All “End If”, “End While” and “End Case” clauses </li></ul></ul><ul><li>A linear sequence of nodes can be combined into one node (we’ll talk about this later) </li></ul>
  13. 13. Convert the Example Code
  14. 14. Basis Path Testing Steps (continued) <ul><li>2. Determine the cyclomatic complexity of the resultant flow graph </li></ul>
  15. 15. Cyclomatic Complexity <ul><li>A software metric that provides a quantitative measure of the complexity of a unit </li></ul><ul><li>Is denoted as V(G) </li></ul>
  16. 16. Cyclomatic Complexity (continued) <ul><li>Can be computed in one of three ways </li></ul><ul><ul><ul><li>Number of edges – Number of Nodes + 2 </li></ul></ul></ul><ul><ul><ul><li>Number of regions created in the two-dimensional plane by the flow graph </li></ul></ul></ul><ul><ul><ul><li>Number of predicates + 1 </li></ul></ul></ul>
  17. 17. Compute V(G) for the Flow Graph
  18. 18. Basis Path Testing Steps (continued) <ul><li>3. Determine the set of linearly independent paths </li></ul>
  19. 19. Rules of Thumb (Customized by Don) <ul><li>Each path should introduce at least one new edge </li></ul><ul><li>In general, each new path should introduce as few new edges as possible </li></ul><ul><li>Each edge should be included in at least one path </li></ul>
  20. 20. Determine the Linearly Independent Paths
  21. 21. Basis Path Testing Steps (continued) <ul><li>Prepare test cases that will force execution of each path in the basis set </li></ul><ul><li>Note: </li></ul><ul><li>One test case might be usable for more than one path </li></ul><ul><li>Some paths on their own may not be possible </li></ul>
  22. 22. Create Test Cases from the Paths
  23. 23. Unit Testing Techniques (revisited) <ul><li>Basis path testing is only one technique for generating test cases </li></ul><ul><li>Several techniques can be used, and the union of the test case sets can be used to create the Unit Test Plan </li></ul>
  24. 24. Summary <ul><li>Software testing is inherently limited in nature </li></ul><ul><li>The main objective of testing is to create test cases that have a high probability of finding an error </li></ul><ul><li>Test planning is top-down </li></ul><ul><li>Test implementation is bottom-up </li></ul><ul><li>The “unit” in unit testing refers to an individual function or method </li></ul><ul><li>Unit testing is usually “glass-box” in that it is done with knowledge of the code </li></ul><ul><li>Basis path testing ensures that each statement and branch in a unit is executed at least once </li></ul>