Download presentation
Presentation is loading. Please wait.
Published byPhilip Alexander Modified over 9 years ago
1
Unit Testing by Jon Edgar
2
Structure of Presentation Structure What is Unit Testing? Worked Example Extreme Programming (XP) Implementation Limitation
3
What is Unit Testing? Primarily Unit Testing is taking the smallest piece of testable software, isolating it from the remainder of the code and then determining if it behaves in the expected manner Often in large projects Unit Testing can be automated providing an efficient way to test a large piece of software by re-testing all the unit tests created in order to determine where the error lies
4
What is Unit Testing? Example 1: Unit A and Unit B are joined. The resulting integrated unit has a problem with the output. Error with Unit A? Error with Unit B? Error with both Unit A and Unit B? Error in integrating Unit A and Unit B? Required output of test doesn’t match requirements?
5
What is Unit Testing? Example 2: Unit tests are performed on Unit A and Unit B to ensure their correctness, again there is a problem with the output of the integrated unit Integration of Unit A and Unit B? Required output of test doesn’t match requirements?
6
Worked Example Addition Programme Unit Test: public class AddTest { public void testAdd() { AddTool summing = new AddToolImpl(); assertTrue(summing.add(1,1) == 2); assertTrue(summing.add(0,1) == 1); assertTrue(summing.add(-1,-2) == -3); assertTrue(summing.add(456,899) == 1355); }
7
Worked Example The Unit Test gives us the following requirements: There must be an interface called AddTool AddTool Interface must implement a Class with a Zero-Argument constructor called AddToolImpl AddTool Interface should have a method called add with 2 integer paramaters, which reutrns another integer It also specifies the expected outcomes for a small range of values
8
Worked Example Addition Programme Code: interface AddTool { int add(int a, int b); } class AddToolImpl implements AddTool { int add(int a, int b) { return a + b; } }
9
Worked Example Advantages of Unit Testing Design over UML Names of classes, objects and methods can be specified and must be adhered to in order for the test to work Coding is designed to be the “least possible” to satisfy the test – greater speed and efficiency of code
10
Extreme Programming (XP) XP using Unit Testing extensively Testing is used to drive implementation Unit Test is constructed from a specific requirement in the specification New Tests should fail first time Unit Tests have to be self contained
11
Extreme Programming (XP) Benefits to XP of Unit Testing Keeps programmers “goal orientated” Each line of code is written to satisfy a test Each line of code pushes the programme to becoming fully functional Removes duplication Freedom to change source code
12
Implementation “Unit Test should be a test on the smallest piece of testable software, isolating it from the remainder of the code and then ensuring it behaves in the correct manner” Unit Tests have to be self contained 2 types of implementation, pre-coding and post-coding
13
Implementation Pre-Coding discussed earlier - Key Points: Allows specification of names Leads to an inferring of Design Efficient Code Time Saving
14
Implementation Post Coding – Key Questions: What is the code required to do? What is the code required to accept as an input? What is the code required to process? What is the code required to Output?
15
Implementation Using these questions we can ensure any mistakes in the code aren’t compounded Automated Testing utilises Frameworks e.g. JUnit, NUnit Frameworks allow the code to conduct unit tests to determine whether various sections of the code are acting as expected over potentially large projects Automated testing can be used on all or part of the code, depending on the implementation framework and maintenance cost
16
Limitations Testing cannot be expected to catch every error! Its impossible, for most programmes, to evaluate all execution paths Unit Testing wont catch integration errors, system errors (functions performed across multiple units) Unit Testing wont deal with issues such as performance or efficiency. Versioning software allows for changes to be tracked
17
Unit Testing Any Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.