Definition Unit The smallest piece of code software















- Slides: 15
Definition: Unit The smallest piece of code software that can be tested in isolation. 1
This is a Unit Test public void My. Add. Test() { My. Math m = new My. Math(); Assert. Are. Equal(m. Add(2, 3), 5); } 2
Definition: Unit Test A unit test is a piece of code (usually a method) that invokes another piece of code and checks the correctness of some assumptions afterward. If the assumptions turn out to be untrue, the unit test has failed. 3
Unit Tests – Independence • Units are tested independent of each other and their environment. • Tests should only test one thing. • Try to keep a traceable connection between potential bugs and test code. 4
Why Unit Test? • Implicit documentation • Simplifies Integration • Improves code design – code becomes highly decoupled due to 2 users : application and test harness – code resists transition from 1 user to 2 users, the rest are easy • Facilitates Change • Allows programmer to refactor code at a later date and show individual parts are correct • Continuous unit tests support sustained maintenance 5
Limitations of Unit Testing • Don’t expect Unit Testing to solve all problems or catch all defects – not silver bullet • Will not catch integration errors – focuses on individual units • Only 1 pare of an effective testing methodology – cover all bases • Requires rigorous discipline • Requires sustainable process of construction and remediation 6
Elements of a Good Unit Test Fixture • Smell test (first reaction) • Unit test hierarchy should parallel units • Naming convention clear and consistent • Good coverage of units in module • Low to no % of unintended failures • Test intended failures, not just expected behavior 7
Unit Test Quality Types (Good, Bad, Ugly) Good – effective, well designed tests Bad – ineffective (wrong) tests (who cares about design? ) Ugly – effective, poorly designed tests 8
Elements of a Good Unit Test • Automatic • Invocation • Checking • Thorough • Depends on needs of project • Repeatable • Independent • Professional • Test code is “real code” (Source: Pragmatic Unit Testing with C#) 9
Software Development Testing Stages Unit Testing – testing of individual components Integration Testing – testing to expose problems from combination of components System Testing – test complete system prior to delivery Acceptance Testing – by users to check system from satisfaction of requirements 10
Unit Test vs. Acceptance Test • Unit Test – satisfy programmer that software does what programmer thinks it does • Acceptance Test – satisfy customer that software provides business value and makes them willing to pay for software 11
What is a Unit Test Framework • Automatically run a set of Unit Tests • Collects Unit Tests into cohesive groups (e. g. by module) • Collects Unit Test results into a consolidated report • Provides setup and teardown support to unit tests 12
Common Unit Test Patterns Boundary Conditions: • Format conformance • Result ordering • Input data range • External references required state • Resource existence (e. g database, file) • Correct number items (e. g. 0, 1, more than 1) • Time (e. g. timeout length, tolerance of delay) 13
Unit Test As Software Test Automation • Unit test execution best when part of an automated build system. • Unit Test practice can directly affect the quality of the code produced. • Good set of automated tests allow for incremental creation of code quality. • Unit Test starts with immature codebase while other automation requires mature codebase and design 14
Summary • Unit Tests are independent tests of small chunks of code created during development. • There are good Unit Test frameworks and harnesses available for many languages and environments. • Unit Testing can directly affect the quality of code during construction. • Unit Testing is only one part of an effective test automation methodology. 15