Unit Testing with Flex Unit By Shashank Kulkarni
Unit Testing with Flex. Unit By Shashank Kulkarni
Some Definitions n Code coverage : Code coverage is a measure used in software testing. It describes the degree to which the source code of a program has been tested. It is a form of testing that inspects the code directly and is therefore a form of white box testing (Testing done only on internal specification). n Test harness or automated test framework : It is a collection of software and test data configured to test a program unit by running it under varying conditions and monitoring its behavior and outputs. The typical objectives of a test harness are to: Ø Automate the testing process. Ø Execute test suites of test cases. Ø Generate associated test reports. A test harness typically provides the following benefits: Ø Increased productivity due to automation of the testing process. Ø Increased probability that regression testing will occur. Ø Increased quality of software components and application. n Regression : Regressions occur whenever software functionality that was previously working correctly stops working as intended. Typically regressions occur as an unintended consequence of program changes.
n Test Case : A test case in software engineering is a set of conditions or variables under which a tester will determine whether a application meets specifications. Test cases are often referred to as test scripts, particularly when written. Written test cases are usually collected into test suites. n Test Oracle : The mechanism for determining whether a software program or system has passed or failed a test. In some settings an oracle could be a use case or Requirement. It may take many test cases to determine that a software program or system is functioning correctly. n Mock objects : mock objects are simulated objects that mimic the behavior of real objects in controlled ways. In a unit test, mock objects can simulate the behavior of complex, real (non-mock) objects and are therefore useful when a real object is impractical or impossible to incorporate into a unit test. n Feature creep : It is the proliferation of features in a product where extra features go beyond the basic function of the product and so can result in over-complication rather than simple, elegant design.
Unit testing What is Unit and Unit testing? Unit testing is a method of testing that verifies the individual units of source code are working properly. A unit is the smallest testable part of an application. In Flex, this means a function, or more appropriately a method as Flex/Action. Script is an object oriented language. Unlike many other forms of software testing, unit tests are usually completed by the developer.
Reasons for Unit Testing n n n n Tests Reduce Bugs in New Features Tests Are Good Documentation Tests Reduce the Cost of Change Tests Improve Design Tests Allow Refactoring Tests Defend Against Other Programmers to make unwanted changes Testing Forces You to Slow Down and Think which gives clean and simple design Tests Reduce Fear of worrying about breaking existing code as you add new features
Flex. Unit “Flex. Unit is an open source framework created by Adobe for unit testing in Flex” The Flex. Unit Framework allows you to create test cases and synchronous tests and evaluate test suites in a test harness application that provides a visual display of all the tests in that test suite. The visual display has a very clear Representation for n Number of Tests Run, Time Taken to Run Tests, Average assertions made per Test, Number of Errors, Failures and so on n It has Filter Option for results and Search functionality n Table View representation where we can check Result, Assrtion , expected and Actual We can Handle Events and Test Visual Components with Flex. Unit How to include Flex. Unit ? Flex. Unit is available for download on Google Code at http: //code. google. com/p/as 3 flexunitlib/. After you extract the file from the zip, you will find a flexunit. swc in the bin directory. You need to add this library to your project.
Important API Test. Runner. Base() : Test. Runner. Base is the default graphical test runner included with the Flex. Unit Framework. Property : test ( To Add the Suite ) , start. Test and Event : Test. Complete. Event n Test. Suite : Test. Suite to hold the collection of tests to be run. Property : add. Test , test. Count(): get. Test. Method. Names(): n n Test. Case –It defines the fixture in which to run multiple tests it inherits Assert class which provides all types of assertions(Assuptions)
Assertion Types n n n n n assert. Equals - Asserts that 2 values are equal. assert. False - Asserts that a condition is false The first argument can be the message when the assertion fails. assert. Match - Asserts that a string matches a regexp. assert. No. Match - Asserts that a string doesn't match a regexp. assert. Not. Null - Asserts that an object is not null. assert. Not. Undefined - Asserts that an object is not undefined. assert. Null - Asserts that an object is null. assert. Strictly. Equals - Asserts that two objects are strickly identical The first argument can be the message when the assertion fails Assert assert. True - Asserts that a condition is true The first argument can be the message when the assertion fails Assert assert. Undefined - Asserts that an object is undefined
Steps in Writing the Test. Cases • • Write a single test Implement just enough code to get the test to compile Run the test and see it fail Implement just enough code to get the test to pass Run the test and see it pass Refactor for clarity Repeat
Unit Testing Example Requirement is We have to make functionality of checking an opening Balance for Account. If Account is of Type saving , then the Opening balance will be 1000 and if it is of type Current Account then opening balance is 5000. Flex. Unit is available for download on Google Code at http: //code. google. com/p/as 3 flexunitlib/. After you extract the file from the zip, you will find a flexunit. swc in the bin directory. Now lets start a TDD process : ) n n Create a project say TDD flexunit. swc add this library to your project In src Create Two Folders as test. Cases and utils Our Application level file will be TDD. mxml
Step 1 : In TDD. mxml Create an Object of Test. Runner. Base Class Give it name as "test. Runner" Code will look like this <flexunit: Test. Runner. Base id="test. Runner" width="100%" height="100%" /> Write Two Function as //Assigns a test. Suite to the Test. Runner. Base which we will start executing //Called from Creation. Complete Event of Application private function on. Creation. Complete(): void { test. Runner. test = create. Suite(); test. Runner. start. Test(); } private function create. Suite(): Test. Suite { var test. Suite: Test. Suite = new Test. Suite(); return test. Suite; }
Step 2 : Create a class Account. Manager. Test Extends Test. Case within the package test. Cases Write a method “test. Opening. Balance” where We will write a test cases for /** * Account types are * s - Saving Account with min. Balance = 500; * c - Current Account with min. Balance = 1000; * */ public function test. Opening. Balance(): void { }
Step 3 : Create a class Account. Manager. Write a two methods open. Account() - It will create a account with given type with required respective opening balance. get. Min. Balance() - It will return the balance of the particular Instance which we will test in our test case method test. Opening. Balance method of Account. Manager. Test. The Code should look like : private var balance: int = 0; private var _type: String; public function open. Account(type: String): void { _type = type. to. Lower. Case(); } public function get. Min. Balance(): Number{ return balance; }
Step 4 : Add the Tests to the test. Opening. Balance method of Account. Manager. Test class as public function test. Opening. Balance(): void { var validatedeposite: Account. Manager = new Account. Manager(); validatedeposite. open. Account("S"); assert. Equals("Balance after Opening Saving Account is 1000", 1000, validatedeposite. get. Min. Balance()); validatedeposite. open. Account("C"); assert. Equals("Balance after Opening Current Account is 5000", 5000, validatedeposite. get. Min. Balance()); } Now Run The Project see that the Test Case Fails
Step 5 : Now implement just enough code so that the test. Case Passes. For this rewrite the method open. Account() in Account. Manager class so that the test. Case passes. The Code looks like this, public function open. Account(type: String): void { _type = type. to. Lower. Case(); if(_type=='s') { balance = 1000; }else if(_type=='c') { balance = 5000; } } Run the Project again Test Case Passes
Presented By Shashank kulkarni
- Slides: 18