Microsoft Fakes, Unit Testing the (almost) Untestable Code

Download Microsoft Fakes, Unit Testing the (almost) Untestable Code

Post on 22-Jan-2018

437 views

Category:

Software

4 download

Embed Size (px)

TRANSCRIPT

<ol><li> 1. Microsoft Fakes Aleksandar Bozinovski Technical Lead, Seavus Unit Testing the (almost) Untestable Code </li><li> 2. Agenda Testing in software Unit testing Why unit tests Unit tests conventions Dependencies, Coupling Microsoft Fakes How does it help How does it work Stubs Shims How Do I choose Q&amp;A </li><li> 3. Unit Testing </li><li> 4. Page 5 Strategy Description Visual Studio Tooling Exploratory Test Tester tries to think of possible scenarios not covered by other test strategies and tests. Useful when engaging users with the testing and observing their (expected) usage of the system. There are no predefined tests. Exploratory testing with Microsoft test Manager (MTM) Integration Test Testing different solution components working together as one. Visual Studio Unit Test features Load Test Testing under load, in a controlled environment. Visual Studio Load Test Agent Regression Test Regression testing ensures that the system still meets the quality bar after changes such as bug fixes. Uses a mixture of unit tests and system tests. Automated testing with MTM Smoke Test Smoke testing is used to test a new feature or idea before committing the code changes. System Test Testing of the entire system against expected features and non-functional requirements. Visual Studio Lab Management Unit Test A test of the smallest unit of code (method / class, and so on) that can be tested in isolation from other units. Visual Studio Test Explorer Unit Test Frameworks User Acceptance Test Toward the end of the product cycles users are invited to perform acceptance testing under real-world scenarios, typically based on test cases. Automated testing with MTM Testing types </li><li> 5. Page 6 Testing types effort </li><li> 6. Page 7 Unit testing? Unit testing is a software testing method by which individual units of source code, are tested to determine whether they are fit for use. Units of source code are methods/functions. The unit test is simply a method/function that calls another method and determines that the later works correctly. How we can verify that our GetEmployeeById(int id) works correctly? Isolation, a mechanism that will isolate the code under test, and resolve all dependencies Unit tests naming conventions: MethodName_StateUnderTest_ExpectedBehavior WithdrawMoney_InvalidAccount_ExceptionThrown MethodName_ExpectedBehavior_StateUnderTest Should_ExpectedBehavior_When_StateUnderTest </li><li> 7. Make sure the code works Refactor with confidence Support emerging design Reducing the cost of fixing bugs by fixing them earlier, rather than later Get a quick understanding of the quality of code Page 8 Why Unit Tests? </li><li> 8. Unit tests conventions Unit tests naming conventions (there are many) MethodName_StateUnderTest_ExpectedBehavior WithdrawMoney_InvalidAccount_ReturnsFalse() Login_InvalidCredentials_ReturnsErrorMessage() MethodName_ExpectedBehavior_StateUnderTest WithdrawMoney_ReturnsFalse_InvalidAccount () The 3A of unit testing Arrange, Act, Assert Arrange: Set up the object to be tested. We may need to surround the object with collaborators. For testing purposes, those collaborators might be test objects (mocks, fakes, etc.) or the real thing. Act: Act on the object (through some mutator). You may need to give it parameters (again, possibly test objects). Assert: Make claims about the object, its collaborators, its parameters, and possibly (rarely!!) global state. Page 9 </li><li> 9. Demo First unit test Page 10 </li><li> 10. In software engineering, coupling is the manner and degree of interdependence between software modules. Much of unit tests is solving dependencies and coupling. Practicing unit tests will improve code quality on the long run. Some principles and patterns must be employed to improve testability. Single responsibility principle (SRP) Open Closed Principle (OCP) Dependency Inversion Principle (DI) Dont Repeat Yourself (DRY) You Arent Going to Need It (YAGNI) Page 11 Dependencies, Coupling </li><li> 11. Microsoft Fakes </li><li> 12. Microsoft Fakes is a framework that enables us to isolate the code we are testing by replacing dependencies of our code with stubs or shims. The Fakes Framework in Visual Studio 2012 is the next generation of Moles &amp; Stubs. (Read: migrate not upgrade) Available in VS 2012/2013 Ultimate Works with .Net framework 2.0 and Above Newest version available with VS 2015 Enterprise Page 13 What is the Microsoft Fakes Framework? </li><li> 13. Allows us to quickly implement doubles to support testing in isolation Allows us to decouple from slow running dependencies like DB, file system, message system. Decoupling allows us to write order independent unit tests Stage data in test methods, not in a DB. One unit test failure will not cause a chain reaction No need to reset a database to a golden state. Allows us to intercept calls to dependencies we do not control. Page 14 How the Fakes framework helps </li><li> 14. In Microsoft Fakes, the developer must right-click the assembly reference they wish to mock and select Add Fakes Assembly. This will generate a new assembly that must be referenced to create the fake objects. Page 15 Generated code </li><li> 15. Demo Intro to Stubs </li><li> 16. Should you, or should you not, change the design of existing code to make it more testable? Page 17 Question - Code isolation </li><li> 17. Demo Membership Provider </li><li> 18. For every public type in the referenced assembly which are included into shim-based faking via configuration, the Microsoft Fakes mechanism generates a shim class. The type name is the same as the original type, with "Shim" as a prefix. Page 19 Shims (all instances) </li><li> 19. Shim methods can be injected per instance of an object. In this example myClass1.MyMethod() will return 5 while myClass2.MyMethod() will return 10; Page 20 Shims (one instance) </li><li> 20. Shims must be used within a ShimsContext.Create() using statement. If need to execute original code a call to ShimsContext.ExecuteWithoutShims must be placed inside the shimmed method. Page 21 Shims Context and Behavior </li><li> 21. Demo Log Aggregator </li><li> 22. Demo WPF Clock </li><li> 23. Stubs Helps if youre interface-driven Creates default() implementations of an interface including properties &amp; methods Shims Substitute hard-coded types with *something else* at runtime Dae Page 24 Stubs and Shims </li><li> 24. Stubs If youve got interfaces already Youre building from scratch If you want to save yourself some typing You arent battling sealed and static keywords Shims Stuff is hopelessly stuck together Stuff is hopelessly non-testable Youre supporting legacy code You are Ninja Shims are not a long-term solution (except when there is no other solution). Page 25 Stubs and Shims </li><li> 25. Thank you for your attention! Copyright: 2016 Seavus. All rights reserved. | www.seavus.com | www.career.seavus.com </li></ol>