Unit/Mutation Testing Flashcards
What do programmers write tests to do?
- Confirm an understanding of the specification
- Can ensure that all different aspects are verified
- Can test individual components that are outside of normal view
- Internal (private) classes /functions /methods that are not directly exposed or deep in the application
What are the two options for testing code?
- Code Trace
- A code Trace is a static analysis that is done by viewing the code and walking through the code by hand.
- Debugging to understand or manipulate operation. - Write coded tests to execute specific components of a program with particular data sets.
- Add code using print to screen or print to file to confirm operation.
- Add structured Unit Tests to the Project
What is a Unit Test?
A unit test is a test case using a single data set that tests a function/method or confirms the state of a component (class instance or object)
- A unit test is constructed as a method of a special testing class that is not part of the application, but is included in the project.
- Units tests may or may not ship with the source code of the product.
How do you confirm a software component it operational?
Software components can have their operation confirmed by using a number of data sets that reflect the operation of a component over a range of values.
- In general, most methods / functions will require more than a single unit test to confirm operation.
- The goal of unit testing is to provide tests that provide data coverage and code coverage
Define Data Coverage
- Testing that uses different data combinations of input data that will ensure that the range of acceptable and unacceptable will produce the correct results/
- 100% data coverage indicates that ALL combinations of data sets have been supplied to the component being tested **
- ** Generally not practical
AIM to achieve 100% PRACTICAL data coverage
- ** Generally not practical
Define Code Coverage
- Testing that will ensure that all code paths have been executed at least once during the set of test cases used.
- NOTE: Practical data coverage MUST yield 100% code coverage
What are the main gotcha’s to remember in code coverage tests?
- Check both sides of OR statements in an IF
- Check both upper and lower limit boundaries
What is the goal of unit testing?
- Confirmation of expected operation
- These are considered
normal data sets
- Use cases (supplied
during specification
development) are useful
to develop meaningful
test data sets - Confirmation of expected error conditions
- These are considered to
be non-normal datasets
- Out of range data sets.
invalid data should
result in continued
program operation
using a standard error
handling mechanism
Unit testing works on which principal?
The principal of applying assertions to method/function results
What are assertions?
An assertion is a way to confirm that a return value or object variable contains a value that is known to be correct.
eg. if (rectangle.getHeight() == height)System.out.println(“PASSED – Height set successfully”);elseSystem.out.println(“FAILED – Height set failed”);
How are Assertions used when developing unit tests?
Assertions are placed into unit tests to confirm or verify an expected result.
- If the assertion results in a true response value the test is said to pass
- A false return value from an assertion is considered a fail
What annotation is used to mark the method as a test method?
@Test before the method
Give an example of a more general assertion that could be used for testing a result
@Test
public void testCalculateAmount() {
double expectedAmount = 1279.61;
Mortgage instance = new Mortgage(0.06,25,200000);
instance.calculateAmount();
int result =
instance.getAmount();
// Confirm values are the same
assertEquals (expectedAmount, result, 1.0E-6);}
(the last param is the accuracy since testing doubles “0.0001% accuracy”)
What are the 2 @Test options?
Expected=exception
Timeout=value in ms
What is the expected exception option?
@Test (expected = ArithmeticException.Class)
- Used to manage cases where the pass case produces an exception