vendredi 2 décembre 2016

Generating PHPUnit tests from array with endpoints

For an application we use configuration files in which a large number of endpoint characteristics are determined (relations, fillables, visibles, roles, etc.) We would like to loop through these files and conduct automatic tests with PHPUnit, simply to see if we receive a response, if validation errors are being triggered, if the response is in line with the files, etc.

We load the configuration and perform the tests for each endpoint configuration:

public function testConfigurationFiles()
{
    $config = resolve('App\Contracts\ConfigInterface');

    foreach ($config->resources as $resource=>$configuration) {
        foreach ($configuration->endpoints() as $method=>$rules) {
            $this->endpoint($method, $resource, $configuration);
        }
    }
}

After which we use a switch, to test each type of method differently (index, show, create, update, delete). In total this comes down to dozens of tests with hundreds of assertions.

However, if even one of these endpoints fails, the entire tests fails without showing explicit information what went wrong. Is there a way to automatically generate a "test{$resource}{$method}" method for each endpoint, so they will be handled like individual tests?

Besides these tests we also conduct units tests & e2e tests, so we are fully aware of the disadvantages of this way of testing.

Aucun commentaire:

Enregistrer un commentaire