Properties of a good Unit Test
- Atomic : A unit test should test a small piece of functionality
- Deterministic : A unit test should always either pass or fail.
- Repeatable: A test is repeatable when it passes consitently. If a test passes and fails without changing the test or any of the dependent code is not repeatable.
- Order independent and Isolated – A test should not be dependent on any other test.
- Fast: if a test is taking 1 second to run it is slow. It should take milisecond.
- Easy to Setup
A mocking framework replaces these dependencies with Fake copies of dependencies:
Hand rolled Mock Objects:
Fake implementations of abstractions
Used to verify class interactions in automated tests.
Created by hand writing code
Creating hand rolled mocks:
- Basic class
- Implement the dependency interface type
- Flush out the dependency functionality needed
- Return Values
- Exception throwing
- “was it called?”
- “How many times it called?”
AAA Syntax (Arrange Act and Assert)
Arrange
Create a mock object
Act
Execute the SUT (System under test)
Assert
Verify SUT’s interaction with the mock object
No comments:
Post a Comment