210 likes | 286 Vues
Design For Testability. 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
E N D
Design For Testability 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
publicclassOrderManager { public void ProcessOrder(Order order) { OrderRepository orderRepository = newOrderRepository(); orderRepository.ProcessOrder(order); } } publicclassOrderManager { public void ProcessOrder(Order order) { IOrderRepository orderRepository = newOrderRepository(); orderRepository.ProcessOrder(order); } }
public void ProcessOrderAgainstService(Order order) { IOrderRepository orderRepository = newOrderService(); orderRepository.ProcessOrder(order); } public void ProcessOrder(Order order) { IOrderRepository orderRepository = newOrderRepository(); 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
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