Week 5 Flashcards
What is Unit Testing?
Testing individual parts (units) of code to ensure they work correctly
What is the purpose of Unit Testing?
To test that the code works as intended
Remember…
It is often very difficult or impossible to “prove” a piece of code works, we can only satisfy ourselves that we havent disproved it works
Designing our tests while keeping this in mind becomes critical
What is the importance of Unit Tests?
Reduces time spent debugging
Helps communicate code’s intended use - illustrates how you expect the code to perform on various inputs and conditions
Code shouldn’t drift away from the limits put on them by tests (unless you stop running them)
How do you prepare a Unit Test?
First decide how to test that aspect of the code in question - typically a unit test will test a single method, and some subset of the funtionality that method may provide
What does the Right-BICEP pneumonic mean?
Right - Are the boundary results correct (or “right)
B - Are all the Boundary conditions correct?
I - Can you check inverse relationships?
C - Can you cross-check the results using some other means?
E - Can you force error conditions to occur?
P - are the performance characteristics within bounds?
What are the properties of good tests (using pnemonic)?
You’ve flexed your Right-BICEP
Automatic
Thorough
Repeatable - (should produce same results each time)
Independant - (of each other test and of the environment)
Professional - (written and maintained to same standard as the shipped code)
How should Unit Tests be structured?
If production code class is called Account and a method is called createAccount
The test should be called testCreateAccount
Method testCreateAccount will call createAccount with necessary arguments to ensure createAccount operates as expected
You may have more than one test method that tests createAccount but they should all begin with test
eg. testCreateAccountDuplicates
What is an Assertion?
JUnit frameworks have assertions which are fundamental building blocks of a unit test
A single unit test may have one to many assertions within it - althought a unit test is concerned with a specific subset of the functionality of an object
Assertions help verify that the code behaves as expected during unit tests.
Example: If you have a function that adds two numbers, you might assert that the result is correct:
assertEquals(5, add(2, 3));
This checks if adding 2 and 3 gives you 5.
If True: The test passes, meaning your code works as intended.
If False: The test fails, signaling that there’s a problem to fix
What is the AssertEquals method?
assertEquals (String msg, expected, actual)
String optional and will be displayed if test fails
expected - value you would hope to see
actual - value produced in code under test
Why do you have to be careful with AssertEquals and floating points?
Due to representation limitations (finite memory) floating point values are not exact - checking exact quality of floating point values is therefore unwise
Instead - specify some tolerance of nearness
What are some more examples of Assertions?
assertNull (String msg, java.lang.Object object)
AssertNotNull (String msg, java.lang.Object object)
assertSame (String msg, expect, actual) - Checks object references are the same
assertNotSame (String msg, expected, actual)
assertTrue (String msg, boolean condition)
assertFalse (String msg, boolean condition)
fail (String msg) - will fail test immediately if flow of control passes to this statement
What are Mock Objects?
Can be difficult when object depends on other objects/ parts of system which are hard to control or reason about
Cheap placeholder objects that are predictable and allow us to test the method we need to in the current class
In what cases should you use Mock Objects?
When the real object:
- has non-deterministic behaviour
- is difficult to set up
- has behaviour that is hard to trigger (eg. network error)
- is slow
- has (or is) a user interface
*does not yet exist
What is the benefits of Test-Driven Development?
Can help you design code better
You can see the code from the user’s perspective - not the developers