2/24/2019 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.

Slides:



Advertisements
Similar presentations
MIX 09 4/15/ :14 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Advertisements

Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day benday.com |
Test Driven Development using Visual Studio Team System Ariel Gur-Arieh VP R&D – MCD Software Solutions
Session 1.
Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day.
@benday #vslive Better Unit Tests through Design Patterns: Repository, Adapter, Mocks, and more… Benjamin
@benday #vslive Automated Build, Test & Deploy with TFS, ASP.NET, and SQL Server Benjamin
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Customer Combiner and Modifier © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
Chris Menegay VP of Consulting Notion Solutions, Inc. DTL319.
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
demo Instance AInstance B Read “7” Write “8”

customer.
Zero to Hero: Untested to Tested with Visual Studio Fakes Benjamin
demo © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
demo Demo.
demo QueryForeign KeyInstance /sm:body()/x:Order/x:Delivery/y:TrackingId1Z
Feature: Suggested Item Enhancements – Analysis and Assignment © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and.
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks.
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.

Benjamin Unit Testing & Test-Driven Development for Mere Mortals.
Better Unit Tests through Design Patterns: Repository, Adapter, Mocks, and more… Benjamin
Top 10 Mistakes in Unit Testing
TechEd /3/2018 4:18 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
8/8/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
TechEd /13/2018 9:06 PM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
9/18/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Unit Testing & Test-Driven Development for Mere Mortals
9/22/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Unit Testing & Test-Driven Development for Mere Mortals
Get Good at DevOps: Feature Flag Deployments with ASP
11/22/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Branching and Merging Practices
TechEd /3/2018 8:11 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Brian Keller Sr. Technical Evangelist Microsoft Session Code: DEV310
TechEd /7/2018 6:07 AM DEV-B331 Brownfield Development: Taming Legacy Code with Better Unit Testing and Microsoft Fakes David Starr Senior Program.
MAP & ACT Pre deployment planning for Windows 7 or Server 2008 R2
DEV411 Testing Un-Testable Code with Visual Studio 2012 Fakes
Team Foundation Server 2010 for Everyone
Compile and Execute Requirements in Microsoft .NET
Tech·Ed North America /17/2019 1:47 AM
Tech·Ed North America /17/2019 6:01 PM
Brian Keller Sr. Technical Evangelist Microsoft Session Code: DEV310
TechEd /18/ :08 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered.
TechEd /18/2019 2:43 PM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Zero to Hero: Untested to Tested with Visual Studio Fakes
Unit Testing & Test-Driven Development for Mere Mortals
2/16/2019 8:43 AM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
TechEd /23/2019 7:16 PM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Building Silverlight Apps with RIA Services
From Development to Production: Optimizing for Continuous Delivery
Developing and Managing SharePoint Solutions with Visual Studio 2012
TechEd /3/ :48 PM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
TechEd /7/2019 1:14 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
8/04/2019 9:13 PM © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
From Development to Production: Optimizing for Continuous Delivery
Виктор Хаджийски Катедра “Металургия на желязото и металолеене”
TechEd /28/2019 7:58 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Jason Zander Unplugged
Шитманов Дархан Қаражанұлы Тарих пәнінің
7/5/2019 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
TechEd /12/ :12 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered.
Tech Ed North America /6/2019 2:07 PM Required Slide
Jamie Cool Program Manager Microsoft
TechEd /5/2019 2:08 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
What’s New in Visual Studio 2012 for Web Developers
Presentation transcript:

2/24/2019 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

2/24/2019 Zero to Hero: Untested to Tested with Microsoft Fakes Using Visual Studio Benjamin Day © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Benjamin Day Brookline, MA Consultant, Coach, & Trainer Microsoft MVP for Visual Studio ALM Team Foundation Server, Software Testing, Scrum, Software Architecture Scrum.org Classes Professional Scrum Developer (PSD) Professional Scrum Foundations (PSF) www.benday.com, benday@benday.com, @benday

Let’s start out with a story.

In the beginning, I was a Pascal & C programmer.

Not object-oriented.

Wrote my code on paper.

99% of my day in the debugger.

Everyone else spent that much time in the bugger, too.

Then I become a VB6 & Java programmer.

Object-oriented.

50% of my time in the debugger.

Why so much less time in the debugger?

Bugs are encapsulated.

I learn C#.

Still 50 % of my life in the debugger.

Then someone tells me about this thing called “unit testing.”

“Well, that’s the dumbest thing I’ve ever heard.”

“I don’t need another test harness for my code.”

(Ignore.)

(Ignore.)

At some point, someone paid me to write a course on unit testing.

(cha-ching.)

“You think about what you’re trying to build before you build it?”

“Then you write an automated test that doesn’t even compile?!?!”

Make the code pass, then you have a failing automated test.

“Then you code until the test passes?”

“Well, that’s just plain stupid.”

(Have you noticed that I'm resistant to change and unsettled by new things?)

Then I start to like it.

My debugger time drops to near zero.

IMHO, time in the debugger is time I’m not writing features.

Before unit testing, long lists of defects.

After unit testing, practically no defects.

I’m delivering much higher quality code to my customers.

Happier customers.

They’re no longer telling me my stuff is broken.

Since then, I’m obsessed with making sure my code is testable.

(…and now the real world comes crashing in like a dinosaur-killing meteorite out of the emptiness of space.)

We don’t always get to write our code from scratch.

We sometimes have to build on top of existing code.

Existing.

Awful.

Just-this-side-of-barely-working.

2/24/2019 Code. You’re welcome. http://en.wikipedia.org/wiki/File:Monkey-typing.jpg © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Ever tried to add unit tests to an existing application?

Darned close to impossible.

Why is it so hard to add tests? When you build with TDD… Lots of design decisions… You always pick the testable one. Assumes automated tests. End result: testable & maintainable. When you build without TDD… You picked the one that gets it done. Testing is focused on integration testing & QA. Assumes human-driven tests. End result: not that testable.

What makes it not testable? Tightly coupled Hidden or embedded dependencies Required data & databases Usually, insane amount of setup code for the test.

Design goals in a testable system (Testable, obviously.) Code to interfaces rather than concrete types Single Responsibility Principle (SRP) Dependency Injection Layered

Here’s a best practice…

Design For Testability.

Build your app so that it can be tested.

“Sure, Ben. That’s nice. So. Uhhh. Yah. We didn’t do that. What’s next

Design For Testability. Refactor For Testability.

It’s called a unit test. Small units of functionality Tested in isolation Unit Test != Integration Test If you designed for testability, you (probably) can test in isolation. If you *didn’t*, testing that monolithic app looks scary.

“How am I supposed to test THAT?!” http://www.pdphoto.org/PictureDetail.php?mat=&pg=8307

1. You have to break it apart.

2. You’ve got to start somewhere.

The Microsoft Fakes Framework Helps you to unit test in isolation Uses delegates & lambda expressions Shims (Magic.) Substitute hard-coded types with *something else* at runtime Stubs Helps if you’re interface-driven Creates default() implementations of an interface including properties & methods

Stubs vs. Shims Stubs Shims If you’ve got interfaces already You’re building from scratch If you want to save yourself some typing You aren’t battling “sealed” and “static” keywords Shims Stuff is hopelessly stuck together Stuff is hopelessly non-testable You’re supporting legacy code Suggestion: Shims are not a long-term solution Use them to help you refactor and actually fix the testability problem

Demos. Write the PersonDataAccess unit test Use shims replace PersonDataAccess

Techniques for going from 0 to hero Remember: you don’t *have to* start writing tests for existing code. Although that might be a good idea. Write tests for new features. Write tests for changes to existing features. Fix bugs with unit tests.

Things to think about.

How would you test this? http://www.flickr.com/photos/ericejohnson/4427453880/

What is Design For Testability? Build it so you can test it. How would you test this? Do you have to take the plane up for a spin? http://www.flickr.com/photos/ericejohnson/4427453880/

try { DoSomething(); } catch(SqlServerOutOfDiskSpaceException ex) { SaveTheUniverseFromDestruction(); }

Demos. Use Stubs to save typing / pain Use Stubs to get code coverage on exceptions

Dependency Injection.

What is Dependency Injection? Classes advertise their dependencies on the constructor Need to save & load Person objects? Add a person data access instance on the constructor. Helps you to design for testability Related to the Factory Pattern Keeps classes loosely coupled

http://lostechies.com/derickbailey/files/2011/03/DependencyInversionPrinciple_0278F9E2.jpg

Advertise Dependencies on Constructor Less Awesome Now With More Awesome

Why does DI help with testability? Helps you focus on the testing task at hand Only test what you’re trying to test. Skip everything else. Makes interface-driven programming simple Interface-driven programming + DI lets you use mocks and stubs in your tests.

Demos. Refactor Person Manager to use a Logger DI param Add verification to Logger calls with Stubs

Avoid End-to-End Integration Tests Does a good test… …really have to write all the way to the database? …really have to have a running WCF service on the other end of that call? …really need to make a call to the mainframe?

Reminder: Only test what you have to test. To test this latch, do you have to take the plane up for a spin? http://www.flickr.com/photos/ericejohnson/4427453880/

The Repository Pattern “Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.” http://martinfowler.com/eaaCatalog/repository.html Encapsulates the logic of getting things saved and retrieved

Demos. Refactor Person Manager to use a Repository Convert tests to use in-memory fake database

What’s some of the dullest code to write that’s the most error prone?

Service data transfer objects to/from Domain objects.

ADO.NET objects to/from Domain objects.

This code probably sits in an object named *DataAccess, right?

*DataAccess makes a call into the database and then converts the data into Domain objects, right?

http://lostechies.com/derickbailey/files/2011/03/SingleResponsibilityPrinciple2_71060858.jpg

1) It makes the database call 2) It converts the objects

Adapter Pattern.

Repository Pattern & Adapter Pattern.

Demos. Refactor to Repository & Adapter

Summary Tightly-coupled app Type replacement to make it testable Stubs to save typing and cover exceptions Eliminate or reduce dependency on database from tests Refactor to Repository & Adapter

Any last questions?

Come see DEV-B358 “Using Functional, Exploratory and Acceptance Testing to Release with Confidence” tomorrow at 10:15a.

Thank you. www.benday.com | benday@benday.com

Resources Learning TechNet msdn http://channel9.msdn.com/Events/TechEd 2/24/2019 Resources Sessions on Demand http://channel9.msdn.com/Events/TechEd Learning Microsoft Certification & Training Resources www.microsoft.com/learning TechNet Resources for IT Professionals http://microsoft.com/technet msdn Resources for Developers http://microsoft.com/msdn © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Complete an evaluation and enter to win! 2/24/2019 Complete an evaluation and enter to win! © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Evaluate this session Scan this QR code to evaluate this session. 2/24/2019 Evaluate this session Scan this QR code to evaluate this session. © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

2/24/2019 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.