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
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)car.turnLeft(0)
car.turnLeft(190)
(you're getting on the terrain ofcar.turnRight
here)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.
It is a unit test isn't it?
That's not what unit tests are for: they only test classes. There are other kinds of testing tools to test (parts of) applications: