FlexUnit and unit tests

Source: Internet
Author: User
Tags arithmetic

What is unit testing and why do unit tests.

A unit is a function that implements simple functions in layman's parlance. A unit test is to test whether a function is functioning correctly with a specific set of inputs (test cases) and returns the correct output.

Unit test is the test work for correctness test of the minimum module of software. The so-called smallest module, in the context of OOP, usually refers to the method of the object.

Unit testing is a programmer's basic responsibility and basic skills, directly affecting the efficiency of the programmer and the quality of the software, but also directly affect the team's cooperation. Consider the following scenario:

A programmer submits a piece of untested code, and another programmer checkout it out, making the local code not run properly, and it took a long time to discover the problem with the untested code: it affected the work of other programmers.

A programmer's code has no unit tests, after a period of time to find the program run abnormally, after a hard investigation, found that this code is a problem, so fixed the vulnerability; After a while, found another bug, and then fix the new found bug; resulted in the last bug recurrence (the problem was not discovered at the time because it was generally simply a simple test of the bug that was just fixed): This is a vicious follow, and a bug fix causes another bug to appear and unconsciously.

The benefits of unit testing are also obvious:

Improve the efficiency of the entire team. Quality code is a contribution to the entire team.

Improve the programmer's sense of responsibility and the quality of the code. Through unit testing, to ensure that their code is reliable and complete, this is responsible for themselves, but also for the team.

Contents of unit Tests
Unit testing is mainly divided into manual static inspection and dynamic (automatic) execution of tracking inspection.

Manual static Check
The main purpose of manual static inspection is to ensure that:

Correctness, can realize the expected function.

Clarity, simple and understandable code, no ambiguity in annotations.

Normative, code format, naming and so on in line with the company's coding specifications and code style.

Efficient, code execution efficiency as high as possible.

The above inspection target priority is from the high end.

Automatic trace Check
Automatic tracking is the focus of our research. The so-called automatic tracking inspection refers to the class and method of unit test written by unit Test tool. When the test class and method are executed, the set input condition is calculated automatically and the result of the test is given.

By automatically tracking tests, we are able to write every bug we find or potential bugs we can think of in our test classes and methods. This allows the test class to be run to find out if the bug still exists, or whether the bug that has been fixed is "re-emerging".

The unit test for automatic track checking is one of XP's core philosophies. The combination of unit test and daily build technology can ensure the smooth progress of the whole development process.

flexunit Test

1. Download FlexUnit
Http://opensource.adobe.com/wiki/display/flexunit/Downloads

2. After decompression has two directories, Asdoc is the API document, bin is SWC Library



3. The new project, the following figure set, press "Finish" new project




4. Right key item Testflexunit->properties->flex build Path, click "Add SWC ..." To add FlexUnit SWC package.



5. Create a new business logic class (ActionScript Class) that needs to be tested, for example:

Test code:
Package {public class Arithmetic {/** * add * author:liuyx/public static function Add (A:number,b:number): number{retur n A + b; /** * Subtraction * author:liuyx/public static function subtract (A:number,b:number): number{return a-b;} }


6. Inherit FlexUnit TestCase class, new test business Arithmetictest class, as follows:  package {import flexunit.framework.TestCase; import Flexunit.framework.TestSuite; public class Arithmetictest extends TestCase {/** * override TestCase Constructor * Author:liuyx * date:2009-01-07/Public Function Ar Ithmetictest (methodname:string) {//Callback TestCase Constructor Super (methodname);}/** * static method, which returns TestCase * Testsuite contained in this author: Liuyx * date:2009-01-07/public static function Suite (): testsuite{var suite:testsuite = new TestSuite (); Suite.addtest ( New Arithmetictest ("Testadd")); Suite.addtest (New Arithmetictest ("Testsubtract")); return suite; /** * Testcast test method, test the addition logic of the business class * Author:liuyx * date:2009-01-07/Public Function Testadd (): void{var result:number = 0 ; result = Arithmetic.add (1,2); Asserttrue ("and for 3", result = = 3); result = Arithmetic.add (2,2); Assertequals ("And for 4", 4,result); /** * Testcast test method, test the subtraction logic of the business class * Author:liuyx * date:2009-01-07/Public Function testsubtract (): void{var Result:numbe R = 0; result = Arithmetic.subtracT (2,1); Asserttrue ("Results 1", result = = 1); Asserttrue ("result is 0", 0,arithmetic.subtract (2,2)); }}  

7. Modify Testflexunit.mxml and join FlexUnit's testrunnerbase.

<?xml version= "1.0" encoding= "Utf-8"?> <mx:application "xmlns:mx=" xmlns: flexunit= "flexunit.flexui.*" layout= "absolute" creationcomplete= "Oncreationcomplete ()" > <mx:Script> <! --[cdata[Import Flexunit.framework.TestSuite/** * Create an entire project TestSuite * Author:liuyx/Private Function Createsuite (): Te Stsuite {*/* build testsuite*/var suite:testsuite = new TestSuite (); Suite.addtest (Arithmetictest.suite ());/*todo: Add other testsuite*/return suite here; /** * Call Testrunner for test/Private Function oncreationcomplete (): void {/* testrunnerbase for Test/testrunner.test = THIS.C Reatesuite (); Testrunner.starttest (); }]]--> </mx:Script> <!--flexunit Testrunnerbase interface Components--> <flexunit:testrunnerbase id= "Testrunner" Width= "100%" height= "100%"/> </mx:Application>


Run Result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.