Phpunit: Erreur de nombre d'arguments du fournisseur de données

Créé le 19 déc. 2016  ·  4Commentaires  ·  Source: sebastianbergmann/phpunit

J'utilise la dernière version de PHPUnit (5.7.4). Le problème est que lorsque je lance le test, il passe. Mais si je l'exécute à nouveau, il échoue avec l'erreur suivante :

ArgumentCountError: Too few arguments to function ValidationTest::testValidateType(), 0 passed and at least 3 expected

Si j'apporte des modifications à la fonction du fournisseur de données (c'est-à-dire que je modifie les données à renvoyer, le nom ou le type de la fonction du fournisseur, etc.) et que je réexécute, cela passe pour une fois et échoue avec l'erreur ci-dessus pour tous les tests consécutifs.

Pas sûr, mais PHPUnit utilise-t-il un mécanisme de mise en cache pour mettre en cache les données du fournisseur ? Si oui, existe-t-il un moyen de le nettoyer (peut-être en utilisant setUp ou tearDown ) ?


Le code suivant fonctionne une fois (passe une fois) :

/**
 * <strong i="15">@covers</strong> Validation
 * <strong i="16">@coversDefaultClass</strong> Validation
 */
class ValidationTest extends TestCase {

    protected $validation;


    protected function setUp() {
        $this->validation = new Validation();
    }


    /**
     * <strong i="17">@covers</strong> ::validateType
     * <strong i="18">@dataProvider</strong> validateTypeProdiver
     */
    public function testValidateType($assertion, $argument, $type) {
        $result = $this->validation->validateType($argument, $type);

        switch ($assertion) {
            case 'True':
                $this->assertTrue($result);
                break;
        }
    }


    public function validateTypeProdiver() {
        return [
            ['True', 'file.txt', 'str']
       ];
    }
}

Le code suivant fonctionne toujours (passe à chaque fois):

/**
 * <strong i="24">@covers</strong> Validation
 * <strong i="25">@coversDefaultClass</strong> Validation
 */
class ValidationTest extends TestCase {

    protected $validation;


    protected function setUp() {
        $this->validation = new Validation();
    }


    /**
     * <strong i="26">@covers</strong> ::validateType
     */
    public function testValidateType() {
        foreach ($this->validateTypeProdiver() as $args) {
            $result = call_user_func_array([$this->validation, 'validateType'], array_slice($args, 1));

            switch ($args[0]) {
                case 'True':
                    $this->assertTrue($result);
                    break;
            }
        }
    }


    public function validateTypeProdiver() {
        return [
            ['True', 'file.txt', 'str']
       ];
    }
}

Commentaire le plus utile

Si quelqu'un a rencontré ce problème à un moment donné.
j'ai fait ce test

class Test extends TestCase {
    protected $tested;

    public function __construct()
    {
        parent::__construct();
        $this->tested = new TestedClass();
    }

    public function provider(): array
    {
        return array(
            array(0, 1, 1),
            array(1, 2, 3),
        );
    }

    /**
     * <strong i="7">@dataProvider</strong> provider
     */
    public function testSum(int $first, int $second, int $expected)
    {
        $this->assertEquals($expected, $this->tested->sum($first + $second));
    }
}

J'ai changé __construct en setUp et cela fonctionne.

Tous les 4 commentaires

Je ne suis pas en mesure de reproduire le problème avec les informations que vous avez fournies. Voici ce que j'ai fait :

$ composer require phpunit/phpunit:5.7.4
$ vendor/bin/phpunit && vendor/bin/phpunit

$ vendor/bin/phpunit && vendor/bin/phpunit 
PHPUnit 5.7.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.17-2+deb.sury.org~trusty+1
Configuration: /.../phpunit.xml

.                                                                   1 / 1 (100%)

Time: 44 ms, Memory: 4.00MB

OK (1 test, 1 assertion)
PHPUnit 5.7.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.17-2+deb.sury.org~trusty+1
Configuration: /.../phpunit.xml

.                                                                   1 / 1 (100%)

Time: 32 ms, Memory: 4.00MB

OK (1 test, 1 assertion)

Voici la classe de test que j'ai utilisée :

use PHPUnit\Framework\TestCase;

class TestMeTest extends TestCase
{

    protected $validation;

    protected function setUp()
    {

        $this->validation = new Validation();
    }

    /**
     * <strong i="9">@covers</strong> ::validateType
     * <strong i="10">@dataProvider</strong> validateTypeProdiver
     */
    public function testValidateType($assertion, $argument, $type)
    {

        $result = $this->validation->validateType($argument, $type);

        switch ($assertion) {
            case 'True':
                $this->assertTrue($result);
                break;
        }
    }

    public function validateTypeProdiver()
    {

        return [
            ['True', 'file.txt', 'str']
        ];
    }
}

Et la classe Validation :

class Validation
{
    public function validateType() {
        return true;
    }
}

PHPUnit ne met pas en cache les fournisseurs de données de manière persistante.

Je ne peux pas non plus reproduire cela.

Si quelqu'un a rencontré ce problème à un moment donné.
j'ai fait ce test

class Test extends TestCase {
    protected $tested;

    public function __construct()
    {
        parent::__construct();
        $this->tested = new TestedClass();
    }

    public function provider(): array
    {
        return array(
            array(0, 1, 1),
            array(1, 2, 3),
        );
    }

    /**
     * <strong i="7">@dataProvider</strong> provider
     */
    public function testSum(int $first, int $second, int $expected)
    {
        $this->assertEquals($expected, $this->tested->sum($first + $second));
    }
}

J'ai changé __construct en setUp et cela fonctionne.

La solution de @dmirogin a fait ma journée, phpunit 8.5

Cette page vous a été utile?
0 / 5 - 0 notes