Code Coverage for laravel dusk

Is there a way to get code coverage when starting Laravel Dusk?

I know that it runs browser tests so that it doesn't parse the code, but is there a way to add a listener to check which code is covered? I have not seen anything on this issue now.

+11
source share
2 answers

Conceptually, you need to download all your queries using the PHP module code coverage tools.

This can be done directly with phpunit libraries or with xdebug coverage tools (which use phpunit).

From this example that I discovered, you can run coverage tools based on several _GET parameters passed using the Dusk test.

public function testBasicExample() { $this->browse(function (Browser $browser) { $browser->visit(route('test', [ 'test_name' => 'testBasicExample', 'coverage_dir' => '/app/Http' ]))->assertSee('test'); }); } 

the code that does the work consists of two parts 1. Start compiling based on the parameters:

 $test_name = $_GET['test_name']; require __DIR__ . '/../vendor/autoload.php'; $current_dir = __DIR__; $coverage = new SebastianBergmann\CodeCoverage\CodeCoverage; $filter = $coverage->filter(); $filter->addDirectoryToWhitelist( $current_dir . '/..' . ((isset($_GET['coverage_dir']) && $_GET['coverage_dir']) ? $_GET['coverage_dir'] : '/app') ); $coverage->start($test_name); 

And 2 finish the collection and conclusion:

 function end_coverage() { global $test_name; global $coverage; global $filter; global $current_dir; $coverageName = $current_dir . '/coverages/coverage-' . $test_name . '-' . microtime(true); try { $coverage->stop(); $writer = new \SebastianBergmann\CodeCoverage\Report\Html\Facade; $writer->process($coverage, $current_dir . '/../public/report/' . $test_name); $writer = new SebastianBergmann\CodeCoverage\Report\PHP(); } catch (Exception $ex) { file_put_contents($coverageName . '.ex', $ex); } } 

The final collection is called using a little tricky trick when the coverage_dumper class has only a destructor, which is called automatically when php terminates the process.

The code itself may tidy up the output paths and variables, but, based on the concept, it should work.

+4
source

Dusk uses browsers to run tests, and the browser cannot see the PHP executable code. The only way I see to cover code with Dusk is to create an option in the php artisan service that could be counted and create a coverage file.

+1
source

Source: https://habr.com/ru/post/1267217/


All Articles