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.
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..
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.
it's impossible to stick with unit testing without a frameworkWhich 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.
It's a productivity toolMake it part of your tool belt
I dream of ISPWeb
I Dream of ISPWeb:Unit Testing SharePoint Apps<br />Gil Zilberfeld<br />
Who am I?<br />Software developer for 15 years<br />Product Manager at Typemock<br />@gil_zilberfeld<br />www.gilzilberfeld.com<br />
What’s the plan?<br />Unit Testing 101<br />Tools <br />Examples<br />Start now<br /><ul><li>It's not that hard</li></li></ul><li>What is unit testing?<br />
Why unit testing?<br />Reduce pain<br />Improve productivity<br />
Automated unit testing shorten the feedback cycle<br />