The Codeception extension for supporting dynamic data driven tests (CEST) using @dataprovider
annotation.
@dataprovider
annotation is now a core feature of Codeception (see PR#3737)
composer.json
and your codeception.yml
. No update required for existing tests using @dataprovider
- Codeception 2.2
- PHP 5.4
The extension can be installed using Composer
$ composer require edno/codeception-dataprovider
Be sure to enable the extension in codeception.yml
as shown in
configuration below.
Enabling DataProvider annotation in your tests is done in codeception.yml
.
extensions:
enabled:
- Codeception\Extension\DataProvider
Once installed you will be to use the @dataprovider
annotation for defining the
method to be use for fetching the test data.
Your data source must be a public static function located within your test class.
The method should return data compliant with the @example
annotation.
<?php
class ExampleDataProviderCest
{
/**
* @dataprovider __myDataSource
*/
public function testWithDataProvider(FunctionalTester $I, \Codeception\Example $example)
{
$expected = ["", "foo", "bar", "re"];
$I->assertInternalType('integer', $example[0]);
$I->assertEquals($expected[$example[0]], $example[1]);
}
public static function __myDataSource()
{
return [
[1, "foo"],
[2, "bar"],
[3, "re"]
];
}
}