Your SlideShare is downloading. ×
0
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
L2624 labriola
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

L2624 labriola

594

Published on

Lab on Unit

Lab on Unit

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
594
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
1
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
  • Image Copyright Wenger
  • Transcript

    • 1. Unit Testing ActionScript and Flex<br />Michael Labriola | Senior Consultant, Digital Primates<br />@mlabriola | labriola@digitalprimates.net<br />
    • 2. Who Am I?<br />Michael Labriola<br />Senior Consultant<br />Digital Primates<br />Client side architect specializing in Adobe Flex<br />Architect and developer of Fluint<br />Lead architect and developer of FlexUnit4.x<br />Benevolent Dictator of the Spoon Framework<br />Co-Author of Flex Training from the Source Series<br />Team Mentor<br />Fan of working code<br />2<br />
    • 3. Before We Begin<br />This lab is broken into five main tasks with various exercises along the way<br />In 90 minutes, if things go well, we will complete tasks one through four with some discussion along the way<br />Task five is something I would strongly advise you take a bit of time to review later. <br />It is a full rework of the application to be testable.<br />The lab workbook and all lesson files are available for download<br />At the end of several lessons there are ‘Advanced’ exercises. <br />People will move at different rates through the content. <br />If you find yourself done with a Task and have time, ponder the advanced exercise they will help demonstrate points in a Socratic way<br />They can be treated as thought exercises but work better if you actually try to implement<br />3<br />
    • 4. Types of Testing - Unit<br />Unit Testing - Our concern in this lab<br />Unit testing involves testing the smallest units of code<br />A single object<br />It requires that the object be isolated.<br />The test result should not be able to be affected by other objects <br />It should not be affected by global state<br />When a unit test fails the source of the error should be immediately clear<br />This requirement for isolation means that some code cannot be unit tested<br />Axiom - You can only unit test testable code<br />Creating testable code is both a question of implementation and of architecture<br />Many people want to unit test but fail<br />In my experience, this is usually because the code they wish to test is not testable in units<br />4<br />
    • 5. Types of Testing - Integration<br />Integration Testing<br />Involves testing several (ideally tested) units together<br />Very important part of testing<br />You need to know that individual objects work together properly<br />Failures are not as clear<br />When an integration test fails, there are (n) places it could have failed<br />As a general rule, the moment we discuss any of the following words, we are discussing some form of integration test:<br />Asynchronous<br />Server<br />Lifecycle<br />DisplayList<br />5<br />
    • 6. Types of Testing - Functional<br />Allows you to record and playback interactions with an application<br />Extremely important; ensures that the application works to specification<br />Many units need to be working before you can produce a single test<br />Failures are very unclear<br />Any number of units could be responsible for the failure<br />Many tools available:<br />Flex Monkey – Open Source<br />Test Complete<br />RIATest<br />HP QTP<br />And more<br />6<br />
    • 7. Exercise 1.1<br />7<br />
    • 8. Object Graphs<br />To understand unit testing we need understand object graphs<br />There are two types of objects we are concerned about Branch and Leaf<br />Leaf Nodes have no dependencies on other objects in your code (they will have dependencies on simple types and some flash player objects)<br />Trivial to test<br />Branch Nodes have dependencies on other objects<br />More difficult and sometimes impossible to test<br />8<br />
    • 9. Creating Objects<br />You will always have both types in your system, however, we often have too many dependencies because we forget some simple things:<br />An object is supposed to have one responsibility.<br />Single Responsibility Principle. <br />Objects with (n) responsibilities will be much harder to test effectively<br />Second objects shouldn’t reach into each other.<br />It breaks encapsulation<br />Don’t violate the Law of Demeter<br />myObject.childObject.grandChildObject.property<br />9<br />
    • 10. Exercise 1.2 and 1.3<br />10<br />
    • 11. Testing Framework<br />FlexUnit 4 is a unit testing framework<br />You don’t need a framework to test<br />You already test each time you walk through the same actions to vet a piece of code<br />Testing frameworks only exist to:<br />Automate this effort so the tests can be run again and again easily<br />Standardize the way tests are constructed so they are understandable to others<br />11<br />
    • 12. Assertions<br />Assertions are a tool used to reveal whether or not a piece of code is working as expected. <br />They take the form of a strong statement indicating the result. For example, if you add the numbers 2 and 3.<br /> result = 2 + 3;<br />You can assert that the result is 5. You do so as you are sure that no other answer is satisfactory and that a different answer is just plain wrong.<br />assertEquals( 5, result );<br />If this assertion is not true, meaning that result is not equal to 5, then you can conclude that the plus operator no longer works correctly in all cases. This is the basis of testing.<br />12<br />
    • 13. Test<br />A Test is just a public method of an object<br />The method is decorated by a special [Test] metadata which allows FlexUnit 4 to recognize it as test<br />[Test] <br />public function shouldSeeBlueSky():void { <br />varsky:Sky = new Sky(); <br />assertEquals( “blue”, sky.color ); <br />} <br />Tests contain one (preferable) or more assertions indicating your belief about the state of the object under test or the result of a method invoked on the object<br />Each test is discrete. It should not depend on other tests or the order the tests are run.<br />13<br />
    • 14. Test Case<br />A Test Case is a collection of related Tests in a single class<br />[Test] <br />public function shouldSeeBlueSky():void { <br />varsky:Sky = new Sky(); <br />assertEquals( “blue”, sky.color ); <br />}<br />[Test] <br />public function shouldNotSeeClouds():void { <br />varsky:Sky = new Sky(); <br />assertNull(sky.clouds); <br />} <br />14<br />
    • 15. Test Case Setup<br />As all tests in a single Test Case should be related, they often can share setup<br />The combination of the setup/environment needed to run a test is called a Test Fixture<br />FlexUnit 4 facilitates sharing test fixtures by allowing you to mark a method with Before or After metadata. This indicates the method should be run before or after every test method.<br />[Before] <br />public function andTheSkyWasCreated():void { <br />sky = new Sky(); <br />}<br />[After] <br />public function letItFall():void { <br />sky = null;<br />} <br />15<br />
    • 16. Test Case Rules<br />FlexUnit 4 offers one more way to configure the test case fixture, rules.<br />You can think of rules as a way to move things that would happen in the Before and After into a separate object. <br />This makes it easier to share this configuration code amongst multiple test cases<br />[Rule] <br />public varcreateAndDestroySky:CreateAndDestroySkyRule = new CreateAndDestroySkyRule();<br />16<br />
    • 17. Test Suite<br />A Test Suite is a collection of Test Cases and other Test Suites<br />package flexUnitTests {<br /> [Suite]<br /> [RunWith("org.flexunit.runners.Suite")]<br /> public class CurrencyConverterSuite {<br /> public var test1:ClearSkyTests;<br /> public var test2:StormySkyTests;<br /> }<br />}<br />17<br />
    • 18. Exercise 2.1 and 2.2<br />18<br />
    • 19. Matchers<br />The process of performing multiple assertions can become complicated<br />Imagine a scenario where you need to verify that 2 points are identical<br />[Test] <br />public function shouldBeTheSamePoint():void { <br />assertEquals( pt1.x, pt2.x ); <br />assertEquals( pt1.y, pt2.y ); <br />assertEquals( pt1.z, pt2.x ); <br />}<br />At some point your test cases become littered with all of the work required to do the assertions<br />To address this problem, FlexUnit works with something called matchers that allow you to move this work into a separate object<br />19<br />
    • 20. Hamcrest<br />Using matchers, the code can look more like this:<br />[Test] <br />public function shouldBeTheSamePoint():void { <br />assertThat( pt1, isSamePoint( pt2 ) ); <br />}<br />The code becomes more readable and the matching logic is contained elsewhere.<br />There is a collection of matchers that can be used with FlexUnit as well as any ActionScript project that can use specialized matching, it is called Hamcrest-as3<br />For more information: https://github.com/drewbourne/hamcrest-as3<br />20<br />
    • 21. Exercise 2.3<br />21<br />
    • 22. Exercise 3.1-3.2<br />22<br />
    • 23. Branches versus Nodes<br />We talked about branches in the abstract, but here is something more concrete.<br />Even though we extracted the PercentChangeFormatter from MarketInfo, it is still a branch node, not a leaf node<br />23<br />MarketInfo<br />PercentChangeFormatter<br />NumberFormatter<br />
    • 24. Combinatorial<br />If you aren’t careful with these sorts of dependencies, you end up with combinatorial tests. <br />This results in a lot of test duplication… technically this is because you can’t identify independent variables.<br />The PercentChangeFormatter really just modifies the String returned from the NumberFormatter a little. <br />As they exist now we really end up testing the both of these objects together… which is an integration test…<br />24<br />PercentChangeFormatter<br />NumberFormatter<br />
    • 25. Exercise 4.1<br />25<br />
    • 26. Seams and Mocks<br />The cleanest way to deal with this is to create a seam.<br />If we provide the needed NumberFormatter to the PercentChangeFormatter, we create a seam<br />What can we do with that seam?<br />We could build the PercentChangeFormatter with a substitute NumberFormatter<br />This will eliminate the combinations and allow us to test only one class (a unit)<br />There are two types of substitutes we can use… fakes and mocks<br />Fakes are just dumb objects that conform to the same interface<br />Mocks are intelligent objects that keep track of what happened to them and know how they should respond<br />For this Task we will use one of many available mocking frameworks, named Mockolate. <br />26<br />
    • 27. Exercise 4.2<br />27<br />
    • 28. Additional Resources<br />28<br />Google Testing Blog<br />http://googletesting.blogspot.com/<br />FlexUnit<br />http://flexunit.org/<br />FlexUnit Tutorials<br />http://tutorials.digitalprimates.net/flexunit.html<br />Hamcrest<br />https://github.com/drewbourne/hamcrest-as3<br />Mockolate<br />http://mockolate.org/<br />My Blog<br />http://www.digitalprimates.net/author/codeslinger/<br />
    • 29. 29<br />

    ×