apache-flexflexunit4

flexunit: Parametrized tests


I am trying to run a parametrized tests... Was trying to implement it like it explained here: http://docs.flexunit.org/index.php?title=Parameterized_Test_Styles

Here is what my test case looking

import org.flexunit.runners.Parameterized;

[RunWith("org.flexunit.runners.Parameterized")]
public class ArrayBasedStackTests
{       
    [Paremeters]
    public static var stackProvider:Array = [new ArrayBasedStack(), new LinkedListBasedStack()] ;

    private var _stack:IStack;

    public function ArrayBasedStackTests(param:IStack)
    {
        _stack = param;
    }


    [Before]
    public function setUp():void
    {
    }

    [After]
    public function tearDown():void
    {
    }

    [Test ( description = "Checks isEmpty method of the stack. For empty stack", dataProvider="stackProvider" )]
    public function isEmptyStackPositiveTest():void
    {
        var stack:IStack = _stack;
        assertEquals( true, stack.isEmpty() );
    }

But this code throws following initializing Error:

Error: Custom runner class org.flexunit.runners.Parameterized should be linked into project and implement IRunner. Further it needs to have a constructor which either just accepts the class, or the class and a builder.

Need help to fix it

UPDATE

I've updated the code so it looks like this

    [RunWith("org.flexunit.runners.Parameterized")]
public class ArrayBasedStackTests
{       
    private var foo:Parameterized;

    [Parameters]
    public static function stacks():Array
    {
        return  [ [new ArrayBasedStack()], [new LinkedListBasedStack()] ] ;
    }

    [Before]
    public function setUp():void
    {
    }

    [After]
    public function tearDown():void
    {
    }

    [Test ( description = "Checks isEmpty method of the stack. For empty stack", dataProvider="stacks")]
    public function isEmptyStackPositiveTest(stack:IStack):void
    {
        assertEquals( true, _stack.isEmpty() );
    }

It works. But the result is a bit strange. I have 4 test executed instead of 2. (I have 2 items in data provider, so cant get why do I have 4 tests).

Output

http://screencast.com/t/G8DHbcjDUkJ


Solution

  • The [Parameters] meta-data specifies that the parameters are passed to the constructor of the test - so the test class is called for each parameter. You also have the dataProvider set for the specific test method, so the test method is also called once for each parameter. Two calls for the test, and two calls to the method, ends up running four tests.

    The solution is to either use [Parameters] meta-tag which specifies the data to use for the whole test class, or use the dataProvider for each test method, but not both with the same data at the same time.