Design For Testability

advertisement
Magnus Härlin
magnus.harlin@iptor.com
@MagnusHarlin
What is testability?
 Repeatable - You should be able to expect the outcome
for known inputs
 Easy to write
 Easy to understand
 Fast
Value of automated testing
 Reduce the time coding + debugging + testing
 Gives you better maintainability
 Quick feedback mechanism
 Tests as documentation
Comparison
the ease of developing in one code base compared to
another
Separation of Concerns
 Readability
 Less complexity
 Higher cohesion
 Lower coupling
Isolate the ugly stuff
(infrastructure)




Db-access
Active Directory
Web services
Configuration files
Not isolated
Service Layer
Business Layer
Data Layer
Interface
Refers to a set of named operations
that can be invoked by clients
public class OrderManager
{
public void ProcessOrder(Order order)
{
OrderRepository orderRepository = new OrderRepository();
orderRepository.ProcessOrder(order);
}
}
public class OrderManager
{
public void ProcessOrder(Order order)
{
IOrderRepository orderRepository = new OrderRepository();
orderRepository.ProcessOrder(order);
}
}
public void ProcessOrderAgainstService(Order order)
{
IOrderRepository orderRepository = new OrderService();
orderRepository.ProcessOrder(order);
}
public void ProcessOrder(Order order)
{
IOrderRepository orderRepository = new OrderRepository();
orderRepository.ProcessOrder(order);
}
Dependency Injection (DI)
Instead of a class creating its own dependencies,
its dependencies are inserted into it.
Service Layer
Business Layer
Data Layer Interface
Data Layer
Mocking
 Use mock object as placeholders for classes that don’t
yet exist
 Don’t mock chatty interfaces
 Fake objects with return and pre-canned values
Small Tests Before Big Tests
 Small focused test will tell you where something is
wrong
 Fast and accurate feedback loops
 Both big (end to end) and small test are important
The Gateway Pattern
 Encapsulate access to external services by
encapsulating the implementation with interfaces
 Object that encapsulates external system
Questions
The big picture - Conclusion
 Testable system will change your design.
 There are times when a design decision is made only to
enable testing
 Testability goes hand in hand with classical definition
of good design - high cohesion, low coupling and
separation of concerns
 Design for testability gives you maintainability
Download