4. Why?
TDD is like Source Control you don’t know how
much you need it until you start using it.
5. Benefits?
• Test first approach is Aspect Driven
• Enforces the developer to be the first consumer of
their own program
• Makes it easy to Refactor
• Simplifies Integration Testing
• Unit tests are living documentation of the system
• Encourages modular design
• Gives greater level of confidence in the code
• Reduces debugging time
6. Terminology
Assertion Test Case
true false statements that are used to verify the a set of conditions to determine the correctness of some
behavior of the unit functionalities of program
Test Suite Fixture
a set of tests that all share the same fixture all the things that must be in place in order to run a test
Code Coverage Fakes & Mocks
Dummy objects that simulates the behavior of complex,
percentage of a program tested by by a test suite
real objects
7. Phases of a Test
Set Up Exercise
setting up the test interact with the system under
fixture test
Blank State
Tear Down Verify
determine whether the
tear down the test fixture to
expected outcome has been
return to the original state
obtained
8.
9. What?
• A unit testing framework for Flex and
ActionScript.
• Similar to JUnit - the original Java unit testing
framework.
• Comes with a graphical test runner.
• Can test both Flex and AS3 projects
10. Let’s try a “Hello
World” unit test using
FlexUnit
13. 2. Create a new method
package
{
import flash.display.Sprite;
public class Calculator extends Sprite
{
public function Calculator()
{
}
public function add(x:int, y:int):int
{
return 0;
}
}
}
17. package flexUnitTests
{
import flexunit.framework.Assert;
3. Create a
public class CalculatorTest
{
test case class
[Before]
public function setUp():void
{
}
[After]
public function tearDown():void
{
}
[BeforeClass]
public static function setUpBeforeClass():void
{
}
[AfterClass]
public static function tearDownAfterClass():void
{
}
[Test]
public function testAdd():void
{
Assert.fail("Test method Not yet implemented");
}
}
}
18. package flexUnitTests
{
import flexunit.framework.Assert;
public class CalculatorTest
{
4. Create a
private var calculator:Calculator;
failing test case
[Before]
public function setUp():void
{
calculator = new Calculator();
}
[After]
public function tearDown():void
{
}
[BeforeClass]
public static function setUpBeforeClass():void
{
}
[AfterClass]
public static function tearDownAfterClass():void
{
}
[Test]
public function testAdd():void
{
Assert.assertEquals(calculator.add(2, 3), 5);
}
}
22. 6. Write the body of the method
package
{
import flash.display.Sprite;
public class Calculator extends Sprite
{
public function Calculator()
{
}
public function add(x:int, y:int):int
{
return x+y;
}
}
}
27. package flexUnitTests
{
import flexunit.framework.Assert;
public class CalculatorTest
{
Components of
private var calculator:Calculator;
a Test Case Class
[Before]
public function setUp():void
{
calculator = new Calculator();
}
[After]
public function tearDown():void
{
}
[BeforeClass]
public static function setUpBeforeClass():void
{
}
[AfterClass]
public static function tearDownAfterClass():void
{
}
[Test]
public function testAdd():void
{
Assert.assertEquals(calculator.add(2, 3), 5);
}
}
28. package flexUnitTests
{
import flexunit.framework.Assert;
public class CalculatorTest
{
Components of
private var calculator:Calculator;
a Test Case Class
[Before]
public function setUp():void
{
calculator = new Calculator();
}
[After]
public function tearDown():void
{
}
[BeforeClass]
public static function setUpBeforeClass():void
{
}
[AfterClass]
public static function tearDownAfterClass():void
{
}
[Test]
public function testAdd():void
{
Assert.assertEquals(calculator.add(2, 3), 5);
}
}
29. package flexUnitTests
{
import flexunit.framework.Assert;
public class CalculatorTest
{
Components of
private var calculator:Calculator;
a Test Case Class
[Before]
public function setUp():void
{
calculator = new Calculator();
}
[After]
public function tearDown():void
{
}
[BeforeClass]
public static function setUpBeforeClass():void
{
}
[AfterClass]
public static function tearDownAfterClass():void
{
}
[Test]
public function testAdd():void
{
Assert.assertEquals(calculator.add(2, 3), 5);
}
}
30. package flexUnitTests
{
import flexunit.framework.Assert;
public class CalculatorTest
{
Components of
private var calculator:Calculator;
a Test Case Class
[Before]
public function setUp():void
{
calculator = new Calculator();
}
[After]
public function tearDown():void
{
}
[BeforeClass]
public static function setUpBeforeClass():void
{
}
[AfterClass]
public static function tearDownAfterClass():void
{
}
[Test]
public function testAdd():void
{
Assert.assertEquals(calculator.add(2, 3), 5);
}
}
31. Test Suite Class
package flexUnitTests
{
import flexUnitTests.CalculatorTest;
[Suite]
[RunWith("org.flexunit.runners.Suite")]
public class CalculatorTestSuite
{
public var test1:flexUnitTests.CalculatorTest;
}
}
32. Myths
• TDD is a Golden Hammer
• We will not need Testers any more
• TDD eradicates bugs from the code
• TDD ensures that the code is up to the
requirement
33. Facts
• Don’t expect TDD to solve all of your
problems.
• TDD reduces the amount of QA but we will
still need Testers :)
• TDD may reduce number of bugs
• Tests might share the same blind spots as
your code because of misunderstanding of
requirements.
34. Useful Links
• FlexUnit
http://www.flexunit.org/
• FlexUnit 4.0
http://docs.flexunit.org/index.php?title=Main_Page
• A brief and beautiful overview of FlexUnit
http://www.digitalprimates.net/author/codeslinger/2009/05/03/
flexunit-4-in-360-seconds/
• A very good read
http://www.amazon.com/Working-Effectively-Legacy-Michael-
Feathers/dp/0131177052