⬆️ ⬇️

FlexUnit 4.x Features (Part 1)

Translation of a post describing the capabilities of FlexUnit, the original: www.flexunit.org/?page_id=6



[25.14.2014] All documentation is now here: cwiki.apache.org/confluence/display/FLEX/FlexUnit



For each functionality, the FlexUnit version is indicated in which it is available.

')

Flex or ActionScript 3 (4.0)


FlexUnit is available for both Flex and Action Script. If you are testing a Flex project, you can find the appropriate FlexUnit for the version of Flex you are using. As for the FlexUnit build for Action Script, it will work with any Action Script 3 project.







Metadata - Test (4.0)


Tests are now tagged using a metadata tag named [Test]. Now for your tests you do not need to use special symbols (test prefix, etc.). In addition, the need to define Test and Suite classes has disappeared. Your classes should no longer inherit the framework classes. Below are some sample tests.



[Test] public function addition():void { assertEquals(12, simpleMath.add(7, 5)); } [Test] public function subtraction():void { assertEquals(9, simpleMath.subtract(12, 3)); } 




Since now test classes should not inherit a class from FlexUnit, you will notice that the assert functions that you used in the past (assertEquals, assertTrue) are now implemented as static functions of the Assert class;



Before and After (4.0)


Sometimes you need to set up a test environment (or device) for your tests. In the example above, you need to ensure the existence of a reference to simpleMath before running the tests. In previous versions of FlexUnit and Fluint, you could override the setup () and teardown () methods to achieve this goal. FlexUnit 4 includes metadata Before and After which solve a similar problem. Any method marked with the Before tag will be run before each test method. Any method interchanged with the After tag will be launched after each test method. This means that you can have a variety of methods that run before or after the test.



 [Before] public function runBeforeEveryTest():void { simpleMath = new SimpleMath(); } [Before] public function alsoRunBeforeEveryTest():void { simpleMath1 = new SimpleMath(); } [After] public function runAfterEveryTest():void { simpleMath = null; simpleMath1 = null; } 




If you use a variety of before or after methods, you can control the order of execution of the methods using the order parameter. For example, [Before (order = 1)], [Before (order = 2)].



BeforeClass and AfterClass (4.0)


The methods marked with Before and After are run before and after the start of each test method. BeforeClass and AfterClass allow you to define a static method that will be run before and after running all test methods in the test class. Just as with Before and After, you can determine the order of the execution of the BeforeClass and AfterClass methods using the order parameter.



 [BeforeClass] public static function runBeforeClass():void { //run for one time before all test cases } [AfterClass] public static function runAfterClass():void { // run for one time after all test cases } 




Exception Handling (4.0)


For the Test metadata tag, the expects parameter can also be specified. The expects parameter allows you to indicate that this test generates an exception. If the test generates an exception with the specified name, then it is considered a success, if there is no then failure. This eliminates the need to use a try block wrapper with an empty catch when writing tests.



 [Test(expects="flash.errors.IOError")] public function doIOError():void { //a test which causes an IOError }  [Test(expects="TypeError")] public function divisionWithException():void { simpleMath.divide( 11, 0 ); } 




Ignore (4.0)


The Ignore metadata tag can be added before any Test you want to ignore. You can also add a line that indicates why the test is ignored. Unlike comments, these lines will always be displayed, reminding you that you need to correct or modify the corresponding method.



 [Ignore("Not Ready to Run")] [Test] public function multiplication():void { assertEquals(15, simpleMath.multiply(3, 5)); } 




Asynchrony (4.0)


In previous versions of FlexUnit, it was very difficult to test code that has many asynchronous events and which was event driven, but was not completely asynchronous.

In Fluint, asynchronous support is better; it is possible to use asynchrony, but for each test performed, additional functionality is added to ensure that the asynchronous code is executed.

FlexUnit 4 allows the developer to determine which tests need asynchrony using the async parameter.

The async parameter provides full asynchronous support, the same as Fluint for a similar test. In addition to the async parameter, you can define a timeout for the method.



 [Before(async,timeout="250")] public function setMeUp():void { } [After(async,timeout="250")] public function allDone():void { } [Test(async,timeout="500")] public function doSomethingAsynchronous():void { //Async.proceedOnEvent( testCase, target, eventName ); //Async.failOnEvent( testCase, target, eventName ); //Async.handleEvent( testCase, target, eventName, eventHandler ); //Async.asyncHandler( testCase, eventHandler ); //Async.asyncResponder( testCase, responder ); } 

Source: https://habr.com/ru/post/150188/



All Articles