0
Unit and Functional Testing Your Flex Applications Mike Nimer Dir. Of Engineering nomee.com
<ul><ul><li>Isolate each part of the program and show that the individual parts are correct.  </li></ul></ul><ul><ul><li>P...
Types of Testing <ul><li>Manual </li></ul><ul><li>Humans create test scripts.  </li></ul><ul><ul><li>Step by Step instruct...
Automated Testing <ul><li>Testing as part of Development </li></ul><ul><li>As developers create code, they create tests </...
Types of Automated Testing <ul><li>Unit </li></ul><ul><li>Tests the smallest unit of code that is possible </li></ul><ul><...
Tools for Unit Testing <ul><li>Unit Testing </li></ul><ul><li>(before) </li></ul><ul><ul><li>FlexUnit  - The original Flex...
Tools for Automated Testing <ul><li>These products all use the Flex Automation API, to monitor, manipulate, and control th...
Testing Terminology <ul><li>Test Method  - A test method is the smallest unit of the testing process. A test method execut...
Understanding Assertions <ul><li>assertEquals() —Accepts two parameters and is a valid assertion if the two values are equ...
Whats new in FlexUnit 4 <ul><li>Support for all Junit 4 features </li></ul><ul><li>Support for UI testing </li></ul><ul><l...
Understanding Metadata <ul><li>FlexUnit 4 can be driven off of specific flexmetadata (similar to java annotations). This a...
Creating A Basic Unit Test <ul><li>package simpleTest.tests{ </li></ul><ul><li>import org.flexunit.Assert; </li></ul><ul><...
Creating A Basic Test Suite <ul><li>package simpleTest </li></ul><ul><li>{ </li></ul><ul><li>import flexunit.framework.Tes...
Creating A Test Runner <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> </li></ul><ul><li><mx:Applicatio...
Asynchronous Testing <ul><li>Asynchronous Tests examples </li></ul><ul><li>Server calls </li></ul><ul><li>Timers </li></ul...
Creating an Asynchronous Unit Test – Setup/Teardown [Before(async)] public function setUp():void{ timer =  new Timer( 500,...
Creating an Asynchronous Test – Test Method [Test(async)] public function testInTimePass() : void { var obj:Object= {expec...
Integration Testing <ul><li>UIComponents </li></ul><ul><li>A common goal when developing Flex applications is to test UICo...
Integration Testing (FlexUnit) <ul><li>FlexUnit Sequences </li></ul><ul><li>Sometimes a test of a UIComponent can require ...
Testing UIComponents – setup public var form:LoginForm; [Before(async,ui)] public function setUp():void  { form = new Logi...
Testing UIComponents – test method <ul><li>[Test(async,ui)] </li></ul><ul><li>public function testLogin():void { </li></ul...
Testing UIComponents – test method <ul><ul><li>var sequence:SequenceRunner = new SequenceRunner( this ); </li></ul></ul><u...
Testing UIComponents – test method <ul><ul><li>sequence.addStep(  </li></ul></ul><ul><ul><li>new SequenceEventDispatcher( ...
Testing UIComponents – test method <ul><ul><li>//sequence.addAssertHandler( handleLoginEvent, passThroughData ); </li></ul...
Integration Testing (FlexMonkey) <ul><li>Recording Scripts </li></ul><ul><li>Replaying Scripts </li></ul>
Tips <ul><li>All Software projects should be thoroughly tested!! </li></ul><ul><ul><li>Automated testing saves you money b...
Questions ?
FlexUnit 4 http://opensource.adobe.com/wiki/display/flexunit/FlexUnit Flex Monkey http://code.google.com/p/flexmonkey/ Mor...
Upcoming SlideShare
Loading in...5
×

Presentation - CFUnited 2010: A ColdFusion, Flex

867

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
867
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Presentation - CFUnited 2010: A ColdFusion, Flex "

  1. 1. Unit and Functional Testing Your Flex Applications Mike Nimer Dir. Of Engineering nomee.com
  2. 2. <ul><ul><li>Isolate each part of the program and show that the individual parts are correct. </li></ul></ul><ul><ul><li>Provide a strict, written contract that the piece of code must satisfy. </li></ul></ul><ul><ul><li>Find problems early in the development cycle. </li></ul></ul><ul><ul><li>Unit testing allows the programmer to re-factor code at a later date, and make sure the module still works correctly </li></ul></ul><ul><ul><li>--wikipedia </li></ul></ul>The goal(s) of testing
  3. 3. Types of Testing <ul><li>Manual </li></ul><ul><li>Humans create test scripts. </li></ul><ul><ul><li>Step by Step instructions (word doc). </li></ul></ul><ul><li>Humans execute those tests. </li></ul><ul><li>Low cost to begin. High cost over the life of a project </li></ul><ul><li>Testing generally happens less often </li></ul><ul><li>Automated </li></ul><ul><li>Humans create test methods. Machines execute those tests. </li></ul><ul><li>Higher cost to develop. Much lower cost over the life of a project </li></ul><ul><li>Testing can happen continually </li></ul><ul><ul><li>On every check-in </li></ul></ul>
  4. 4. Automated Testing <ul><li>Testing as part of Development </li></ul><ul><li>As developers create code, they create tests </li></ul><ul><li>Test Driven Development – tests first, code 2nd </li></ul><ul><li>Tests are continually executed by developer to ensure new code does not break existing features </li></ul><ul><li>Automated build/test environments </li></ul><ul><li>Part of “Continuous Integration” </li></ul><ul><li>Code checked out and built automatically </li></ul><ul><li>Code continually tested with your tests and reports generated </li></ul><ul><li>Automated QA ensuring a last minute check in does not break a release </li></ul>
  5. 5. Types of Automated Testing <ul><li>Unit </li></ul><ul><li>Tests the smallest unit of code that is possible </li></ul><ul><li>Usually tests a single method or function </li></ul><ul><li>Multiple tests for every path through a method </li></ul><ul><li>Usually created by Developer </li></ul><ul><li>Integration / functional </li></ul><ul><li>Tests a combination of units </li></ul><ul><li>Can be used to test UI Components </li></ul><ul><li>Confirms that individual pieces of code work together </li></ul><ul><li>Created by Developer or QA </li></ul>
  6. 6. Tools for Unit Testing <ul><li>Unit Testing </li></ul><ul><li>(before) </li></ul><ul><ul><li>FlexUnit - The original Flex/ActionScript 3.0 unit testing framework. </li></ul></ul><ul><ul><li>Fluint –Written for better asynchronous support and support for testing UI components. </li></ul></ul><ul><li>(today) </li></ul><ul><ul><li>FlexUnit 4 – A merge of FlexUnit 3 and Fluint. Providing better asyncronous testing support, support for UIComponent testing and IDE integration. </li></ul></ul>
  7. 7. Tools for Automated Testing <ul><li>These products all use the Flex Automation API, to monitor, manipulate, and control the Flex application. </li></ul><ul><li>FlexMonkey </li></ul><ul><li>TestComplete </li></ul><ul><li>Selenium (with SeleniumFlex project) </li></ul><ul><li>Mercury QuickTest Pro 9.1 (QTP) </li></ul><ul><li>Borland SilkTest </li></ul><ul><li>IBM Rational Functional Tester </li></ul>
  8. 8. Testing Terminology <ul><li>Test Method - A test method is the smallest unit of the testing process. A test method executes code and checks the outcome. </li></ul><ul><li>At the end of the test method the developer generally makes an “ assertion” , stating the expected outcome of this executed code. </li></ul><ul><li>Test Case - A test case is a collection of test methods that share a common test environment, also referred to as a test fixture. </li></ul><ul><li>Test Suite - A test suite is a collection of test cases. </li></ul><ul><li>Test Runner - A test runner is an application that executes the test methods in your suites and cases. </li></ul>
  9. 9. Understanding Assertions <ul><li>assertEquals() —Accepts two parameters and is a valid assertion if the two values are equal (==). </li></ul><ul><li>assertStrictlyEquals() —Accepts two parameters and is a valid assertion if the two values are strictly equal (===). </li></ul><ul><li>assertTrue() —Accepts a single parameter and is a valid assertion if the value is true. </li></ul><ul><li>assertFalse() —Accepts a single parameter and is a valid assertion if the value is false. </li></ul><ul><li>assertNull() —Accepts a single parameter and is a valid assertion if the value is null. </li></ul><ul><li>assertNotNull() —Accepts a single parameter and is a valid assertion if the value is not null. </li></ul><ul><li>fail() — Marks the test failed. </li></ul>
  10. 10. Whats new in FlexUnit 4 <ul><li>Support for all Junit 4 features </li></ul><ul><li>Support for UI testing </li></ul><ul><li>Metadata driven </li></ul><ul><li>Exception handling </li></ul><ul><ul><ul><li>New attribute of [Test] Metadata allows you to define expected exceptions (instead of using try/catch) </li></ul></ul></ul><ul><ul><ul><li>[Test(expects=“TypeError”)] </li></ul></ul></ul><ul><li>Async test support </li></ul><ul><li>Support for multiple Runners </li></ul><ul><li>Hamcrest support </li></ul><ul><ul><ul><li>Uses the Hamecrest AS3 project for a richer set of Asserts. </li></ul></ul></ul><ul><li>Theories and Assumptions </li></ul><ul><li>IDE Support </li></ul><ul><li>Beta 1 now available. </li></ul>
  11. 11. Understanding Metadata <ul><li>FlexUnit 4 can be driven off of specific flexmetadata (similar to java annotations). This allows you to define tests as you see fit, with your own naming conventions or in existing code. </li></ul><ul><li>Before metadata you had to name your test methods a certain way, and your tests had to extend specific classes. </li></ul><ul><li>[Test] </li></ul><ul><li>[Suite] </li></ul><ul><li>[Before] </li></ul><ul><li>[After] </li></ul><ul><li>[BeforeClass] </li></ul><ul><li>[AfterClass] </li></ul><ul><li>[Ignore] </li></ul><ul><li>[DataPoints] </li></ul><ul><li>[Theory] </li></ul><ul><li>[RunWith] </li></ul>
  12. 12. Creating A Basic Unit Test <ul><li>package simpleTest.tests{ </li></ul><ul><li>import org.flexunit.Assert; </li></ul><ul><li>public class SimpleTestCase{ </li></ul><ul><li> [Test] </li></ul><ul><li> public function testMath():void { </li></ul><ul><li>var x:int = 5 + 3; </li></ul><ul><li>Assert.assertEquals( 8, x ) </li></ul><ul><li> } </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  13. 13. Creating A Basic Test Suite <ul><li>package simpleTest </li></ul><ul><li>{ </li></ul><ul><li>import flexunit.framework.TestSuite; </li></ul><ul><li>import simpleTest.tests.SimpleTestCase; </li></ul><ul><li>[Suite] </li></ul><ul><li>[RunWith(&quot;org.flexunit.runners.Suite&quot;)] </li></ul><ul><li>public class SimpleTestSuite </li></ul><ul><li>{ </li></ul><ul><li> // define a var for each test case. </li></ul><ul><li> public var simpleTestCase:SimpleTestCase; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  14. 14. Creating A Test Runner <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> </li></ul><ul><li><mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot; </li></ul><ul><li>creationComplete=&quot;runMe()&quot; </li></ul><ul><li>xmlns:flexUnitUIRunner=&quot;http://www.adobe.com/2009/flexUnitUIRunner&quot; > </li></ul><ul><li><mx:Script><![CDATA[ </li></ul><ul><ul><li>import simpleTest.SimpleTestSuite; </li></ul></ul><ul><ul><li>import org.flexunit.listeners.UIListener; </li></ul></ul><ul><ul><li>import org.flexunit.runner.FlexUnitCore; </li></ul></ul><ul><ul><li>private var core:FlexUnitCore; </li></ul></ul><ul><ul><li>public function runMe():void { </li></ul></ul><ul><ul><li>core = new FlexUnitCore(); </li></ul></ul><ul><ul><li>//Listener for the UI, optional </li></ul></ul><ul><ul><li>core.addListener( new UIListener( uiListener )); </li></ul></ul><ul><ul><li>core.run( SimpleTestSuite ); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>]]></mx:Script> </li></ul><ul><li>< flexUnitUIRunner:TestRunnerBase </li></ul><ul><li>id=&quot;uiListener&quot; </li></ul><ul><li>width=&quot;100%&quot; height=&quot;100%&quot; /> </li></ul><ul><li></mx:Application> </li></ul>
  15. 15. Asynchronous Testing <ul><li>Asynchronous Tests examples </li></ul><ul><li>Server calls </li></ul><ul><li>Timers </li></ul><ul><li>AIR Sqlite </li></ul><ul><li>AIR File Access </li></ul>
  16. 16. Creating an Asynchronous Unit Test – Setup/Teardown [Before(async)] public function setUp():void{ timer = new Timer( 500, 3 ); } [After(async)] public function tearDown():void { if ( timer ) { timer.stop(); } timer = null; }
  17. 17. Creating an Asynchronous Test – Test Method [Test(async)] public function testInTimePass() : void { var obj:Object= {expected:3} timer.delay = 100; timer.addEventListener(TimerEvent.TIMER_COMPLETE, Async.asyncHandler( this, completehandler, 500, obj, null) , false, 0, true ); timer.start(); } protected function completehandler( event:Event, data_:Object):void{ if( event.type == &quot;timerComplete” && ){ Assert.assertEquals(Timer(event.target ).currentCount, data_.expected ); } else{ Assert.fail( &quot;wrong event type&quot;); } }
  18. 18. Integration Testing <ul><li>UIComponents </li></ul><ul><li>A common goal when developing Flex applications is to test UIComponents in a repeatable way </li></ul><ul><li>UIComponents are internally asynchronous </li></ul>
  19. 19. Integration Testing (FlexUnit) <ul><li>FlexUnit Sequences </li></ul><ul><li>Sometimes a test of a UIComponent can require many steps to setup. </li></ul><ul><li>For example, you may need to create a Login Form, populate it and then click a button before you can decide if everything worked </li></ul><ul><li>Sequences are a shorthand to simplify that work into a readable set of steps </li></ul>
  20. 20. Testing UIComponents – setup public var form:LoginForm; [Before(async,ui)] public function setUp():void { form = new LoginForm(); Async.proceedOnEvent( this, form, FlexEvent.CREATION_COMPLETE, 200 ); UIImpersonator.addChild( form ); } [After(async,ui)] public function tearDown():void { UIImpersonator.removeChild( form ); form = null; }
  21. 21. Testing UIComponents – test method <ul><li>[Test(async,ui)] </li></ul><ul><li>public function testLogin():void { </li></ul><ul><ul><li>var passThroughData:Object = new Object(); </li></ul></ul><ul><ul><li>passThroughData.username = 'myuser1'; </li></ul></ul><ul><ul><li>passThroughData.password = 'somepsswd'; </li></ul></ul><ul><ul><li>var sequence:SequenceRunner = new SequenceRunner( this ); </li></ul></ul><ul><ul><li>sequence.addStep( new SequenceSetter( form.usernameTI, {text:passThroughData.username} ) ); </li></ul></ul><ul><ul><li>sequence.addStep( new SequenceWaiter( form.usernameTI, FlexEvent.VALUE_COMMIT, 100 ) ); </li></ul></ul><ul><ul><li>sequence.addStep( new SequenceSetter( form.passwordTI, {text:passThroughData.password} ) ); </li></ul></ul><ul><ul><li>sequence.addStep( new SequenceWaiter( form.passwordTI, FlexEvent.VALUE_COMMIT, 100 ) ); </li></ul></ul><ul><ul><li>sequence.addStep( new SequenceEventDispatcher( form.loginBtn, new MouseEvent( 'click', true, false ) ) ); </li></ul></ul><ul><ul><li>sequence.addStep( new SequenceWaiter( form, 'loginRequested', 100 ) ); </li></ul></ul><ul><ul><li>sequence.addAssertHandler( handleLoginEvent, passThroughData ); </li></ul></ul><ul><ul><li>sequence.run(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>protected function handleLoginEvent( event:TextEvent, passThroughData:Object ):void { </li></ul><ul><ul><li>Assert.assertEquals( passThroughData.password, event.text ); </li></ul></ul><ul><li>} </li></ul>
  22. 22. Testing UIComponents – test method <ul><ul><li>var sequence:SequenceRunner = new SequenceRunner( this ); </li></ul></ul><ul><ul><li>sequence.addStep( new SequenceSetter( form.usernameTI, {text:passThroughData.username} ) ); </li></ul></ul><ul><ul><li>sequence.addStep( new SequenceWaiter( form.usernameTI, FlexEvent.VALUE_COMMIT, 100 ) ); </li></ul></ul><ul><ul><li>sequence.addStep( new SequenceSetter( form.passwordTI, {text:passThroughData.password} ) ); </li></ul></ul><ul><ul><li>sequence.addStep( new SequenceWaiter( form.passwordTI, FlexEvent.VALUE_COMMIT, 100 ) ); </li></ul></ul><ul><li>Create a new Sequence </li></ul><ul><li>Set a property on a UIComponent </li></ul><ul><li>Wait for the “valueCommit” event to fire, so you know the property was set correctly. </li></ul>
  23. 23. Testing UIComponents – test method <ul><ul><li>sequence.addStep( </li></ul></ul><ul><ul><li>new SequenceEventDispatcher( form.loginBtn, new MouseEvent( 'click', true, false ) ) ); </li></ul></ul><ul><ul><li>sequence.addStep( new SequenceWaiter( form, 'loginRequested', 100 ) ); </li></ul></ul><ul><ul><li>sequence.addAssertHandler( handleLoginEvent, passThroughData ); </li></ul></ul><ul><ul><li>sequence.run(); </li></ul></ul><ul><li>Add a new step that simulates a user clicking on the “login” button. </li></ul><ul><li>Wait for the “loginRequested” event. </li></ul><ul><li>Add an “assertHandler” to check the login when it is completed. </li></ul><ul><li>Run the test. </li></ul>
  24. 24. Testing UIComponents – test method <ul><ul><li>//sequence.addAssertHandler( handleLoginEvent, passThroughData ); </li></ul></ul><ul><li>protected function handleLoginEvent( </li></ul><ul><li>event:TextEvent, passThroughData:Object ):void { </li></ul><ul><li>Assert.assertEquals( passThroughData.password, event.text ); </li></ul><ul><li>} </li></ul><ul><li>Use the assertHandler method to test the results of the login action (server call) </li></ul><ul><li>If the right data exists, success. </li></ul>
  25. 25. Integration Testing (FlexMonkey) <ul><li>Recording Scripts </li></ul><ul><li>Replaying Scripts </li></ul>
  26. 26. Tips <ul><li>All Software projects should be thoroughly tested!! </li></ul><ul><ul><li>Automated testing saves you money by preventing bugs form returning again and again. </li></ul></ul><ul><li>A getting started rule of thumb for writing a new test case </li></ul><ul><ul><ul><li>Write one test that passes the “usual” case </li></ul></ul></ul><ul><ul><ul><li>Write one test that passes by testing the extremes </li></ul></ul></ul><ul><ul><ul><li>Write one test that is supposed to fail </li></ul></ul></ul><ul><li>Continue building your test cases, by writing a test case for every bug that is submitted – before you close the bug as fixed. </li></ul><ul><li>Tests cases should “clean up” after themselves </li></ul><ul><li>Any developer should be able to run the tests without custom setup – if possible. </li></ul>
  27. 27. Questions ?
  28. 28. FlexUnit 4 http://opensource.adobe.com/wiki/display/flexunit/FlexUnit Flex Monkey http://code.google.com/p/flexmonkey/ More on Continuous Integration http://www.martinfowler.com/articles/continuousIntegration.html
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×