Question

I just start to study Unit Testing (using FlexUnit). I still have not sure how to use them.

I created a new flex project, and created a class named Car. Then, I created a TestCase class, for Car class.

This is the default template that FlexBuilder gave to me, and I put some traces.

package flexUnitTests
{
    import flexunit.framework.Assert;

    public class CarTest
    {       
        [Before]
        public function setUp():void
        {
            trace('before test function');
        }

        [After]
        public function tearDown():void
        {
            trace('after test function');
        }

        [BeforeClass]
        public static function setUpBeforeClass():void
        {
            trace('before test class');
        }

        [AfterClass]
        public static function tearDownAfterClass():void
        {
            trace('after test class');
        }

        [Test]
        public function testCar():void
        {
            trace('the test function');

            var c:Car = new Car("gol");

            Assert.assertTrue("Is car name valid:", c.name != "gol");
        }
    }
}

I can't understand why I have so many methods to run before or after a test function. Why not just use them within a simple funcion, like:

        [Test]
        public function testCar():void
        {
            trace('before test class');
            trace('before test function');

            trace('the test function');

            var c:Car = new Car("gol");

            Assert.assertTrue("Is car name valid:", c.name != "gol");

            trace('after test function');
            trace('after test class');
        }

A second question I want to add is, these tests test each class individualy, (like shown when instancianting Car class) or a I can test my whole application once by instanciating the main class? The problem is how to simulate the whole userflow of the application (user clicks here, send a request to server there, etc, etc). I should write a whole flow one by one in a test method? :O

Was it helpful?

Solution

I can't understand why I have so many methods to run before or after a test function.

You don't have to implement all these methods. FB generated all these stubs for you, but you might as well leave them out.

That said, you can have more than one test function inside a test class. You usually test every public function of a class. You may even have multiple tests per function, using different sets of corner-case arguments.
e.g. in your example you only test the constructor, but you might have a test for:

  • car.turnLeft(32) (in degrees)
  • or see what happens when car.turnLeft(0)
  • or car.turnLeft(190) (you're getting on the terrain of car.turnRight here)
  • or car.turnLeft(-12) (negative turnLeft?)

The before/after methods can be used to write some code that you want executed before/after every test in the class, so that you don't have to repeat this in each test function.

these tests test each class individualy

It is a unit test isn't it?

Can I test my whole application once by instanciating the main class?

That's not what unit tests are for: they only test classes. There are other kinds of testing tools to test (parts of) applications:

  • behavioural test tools: in which you write scenario's that contain a typical execution sequences and assert that the outcome of such sequences is correct. I believe Cucumber can test Flex apps in this way (but not entirely sure)
  • UI test tools: in which you record scenario's of users clicking around in your application. These scenario's can then be played back to test whether the application keeps running as expected. FlexMonkey is one such solution for Flex apps, but it seems the owners have changed direction with the product.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top