Your SlideShare is downloading. ×
Unit Testing SharePoint Applications
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Unit Testing SharePoint Applications

1,571
views

Published on

Presented at the SharePoint Israeli user group, June 2010

Presented at the SharePoint Israeli user group, June 2010

Published in: Technology, Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,571
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • I am not a sharepoint manI’m a unit testnig man. I’m going to show you examples, from real life codeHow many people do unit tests? Use isolation frameworkSo let’s talk about me.
  • My Tank storyI couldn’t test parts of the system separately, and that’s what unit testing is about. If I could, it will not take 3 months till the next test, and I would know if my fixes were correct (they we’re not).
  • How do you do unit testingHow is the SharePoint world different (or similar) to the rest of the world
  • Testing units of the software, without the restSimple, but not easily achievable.Software is already kvetched together from the get go.And SharePoint is a 3rd party which we don’t have control over. We cannot disconnect from the server.
  • My team was always fighting over because of broken builds.My testers did not trust the devs.As a manager, I had to deal with that. Oh the headache.Two questions: How do you know your stuff works?How do you know you haven’t broken anything?And then comes magicPeople are not afraid to make changes to their codeYou know exactly what failedIt takes a lot less time to debugNo more stupid bugzIt makes you THINK about the code you’re writing.It makes you a professional – you are responsible to test your code, not QA
  • Moment of Hebrew:Unit testing = Automated unit testing.When I started at Bio-Rad (1997) the first thing they taught me was unit testing. Manual.VB Form, for a C++ COM componnet.This is not repeatable.When I changed my code, I did not go through all the scenarios I’ve already tested.And when you fix a bug, in a component written 6 months ago, would you even remember? No.Unit tests help you remember, and remind you.When something breaks, you’ll know immediately. The sooner the better. And mostly, that’s what automated unit tests do. They shorten the feedback cycle from weeks and months to seconds and minutes. It’s easier to fix something that you just wrote.And this is key: unit tests should run quickly. If they don’t the vicious cycle begins: No quick feedback, less test runs. Then why should I write tests if I don’t run them, and the road to hell is now paved.
  • Let’s take a look at the environment.First we need a framework. We need to say – this is a test, and that’s the criteria for pass/fail.Then you need a runner, and result view.MSTest (yes, there’s NUnit, but you don’t need it).Show logic of webpartCreate a new projectAdd referenceShow attributesWrite //AAARename testWrite testRun test.Notice we’re testing logic.In the real world, it’s not that easy.
  • Code is not testable by default.All kind of tutorials, don’t prepare you for testability. They give you unstructured code. This has changed about a year ago with P&P promoting unit tests.Who knows what P&P is?It’s still not enough. We’ll go into MS ambivalent relations with unit testing later.Who is using SP 2010? Some improvement there since 2008.The F5 Build/Deploy/ Test cycle is slow. It takes minutes. Slowness, kills unit tests.Speaking of which, who uses a virtual environment for development? It’s slow by nature.
  • SharePoint is the example I use for dependency problem.For the case I showed in my webpart, I need to set up differnet SharePoint setups.Imagine how many I setups I’ll need to test every scenario in the application? How long it will take them to run?Will I do it? Will you do it?No. The problem, is that SharePoint is a dependency, and it’s a problem to get rid of.Because SharePoint objects don’t lend themselves for simulations.They have private consttructors. Cannot inject them directly or through Dis.They are sealed and cannot be overridden. These are the two major ways to change behavior.
  • We’ll see about that in the next session.
  • Dependencies is why most people stop doing unit testing.Overriding dependencies is hard.
  • Let you change the behavior of your code, without changing it. (mostly)Basically, isolation frameworks allow you to do 2 thing by creating fake objects.
  • This is important: Without changing my production code.
  • The Asserts I showed you before test state: properties, fields.What happens if I want to make sure a method was called with the correct arguments?Like database connect?With SharePoint its worse, because most of the object model is designed for reading, not building it..
  • For the SharePoint crowd, there are currently two options: Typemock Isolator and Microsoft Moles.I’ll show you examples for both.Isolator was until recenty the only way to test SharePoint. That’s because it can fake any type – statics, sealed, you name it. P&P said so.Microsoft is between two places: the P&P says do unit tests. But the SharePoint team doesn’t do it themselves., and produce something not testable.What did they do?Moles was until 3 months ago a research project, part of the Pex project. It is now a power tool. This means most people don’t even know it exists. MS doesn’t want to push unit testing.So I’m going to show you how tests look like in both cases. I’m not going to do all the process. I want to give you a feeling on how tests looks like with SharePoint.Example 1: Webpart.Isolator References APIsMoles Lambdas
  • CostBoth can fake everything.Isolator is easier to write/readMoles doesn’t have a verify.With Moles you really need to know your lambdas. It’s painful.Isolator has more features, more functionality. It is a decidedly unit testing toolMicrosoft is not pushing it – hard to find examples on the net. 1 man show.And besides, we’re smarter and better looking.
  • Which ever tool you use you need to understand, that you need a tool.Stupid bugz.You can start by downloading Isolator and Moles. Start writing unit tests.
  • Transcript

    • 1. Unit Testing SharePoint Applications
      Episode IV: A New Hope
      Gil Zilberfeld
      Typemock
    • 2. Who am I?
      Software developer for 15 years
      Technical Evangelist at Typemock
      @gil_zilberfeld
      • www.gilzilberfeld.com
      Let me tell you about my first REAL job...
    • 3. What’s the plan?
      Unit Testing 101
      Tools
      Examples
      Start now
      • It's not that hard
    • What is unit testing?
    • 4. Why Unit Testing?
      Reduce pain
      Improve productivity
    • 5. Automated Unit Testing
      Short feedback cycle
    • 6. Where do I start?
    • 7. SharePoint Development
      The real world…
    • 8. And the worst part is
    • 9. So is SharePoint Untestable?
    • 10. Unit Testing SharePoint Applications
      Episode V:
      The frameworks strike back
    • 11. Dependencies
    • 12. Why do we need isolation?
      • Applications are complex
      • 13. We can’t touch a live system
      When we call SharePoint
      Setup
      Teardown
      Repeat
    • 14. Isolation Frameworks to the Rescue
    • 15. Fakes help me change behavior without changing the code
       
    • 16. Fakes help me verify calls
       
    • 17. Isolation Frameworks Demo
    • 18. What are the differences?
    • 19. Unit tests are the antidote to stupid bugs
      it's impossible to stick with unit testing without a framework
    • 20. Start now
      It's a productivity tool
      Make it part of your tool belt
    • 21. Resources
      Typemock
      http://www.typemock.com
      Moles
      http://research.microsoft.com/en-us/projects/moles/
      Mock Comparison Project
      http://code.google.com/p/mocking-frameworks-compare/
      Me:
      Twitter: @gil_zilberfeld
      http://www.gilzilberfeld.com
      gilz@typemock.com
    • 22. Questions?