Dusk: Code Coverage

Created on 5 May 2017  路  15Comments  路  Source: laravel/dusk

Does Laravel Dusk produce working code coverage? Because although I added node to phpunit.dusk.xml file, and got logs, those logs lack any coverage data even for the controllers being tested.

enhancement

Most helpful comment

The article was more of mental note to myself with different approaches. I came up with following solution which seems to suite the Laravel framework without too many clumsy hacks.

Add following to any testing specific service providers (DuskServiceProvider.php could have a flexible variant of this I suppose)

    public function register()
    {
        if ($this->app->environment('production')) {
            throw new Exception('It is unsafe to run Dusk in production.');
        }
        if (!$this->app->runningInConsole()) {
            if ($this->app->environment('testing')) {
                try {
                    $this->triggerCoverage();
                } catch (Exception $e) {
                    Log::info("Dusk coverage: " . $e->getMessage());
                }
            }
        }
    }

    private function triggerCoverage() {
        $coverage = new \SebastianBergmann\CodeCoverage\CodeCoverage();
        $coverage->filter()
            ->addDirectoryToWhitelist(app_path());
        $name = microtime(true) . '.' . str_random(8);
        $coverage->start($name);
        $this->app->terminating(function () use ($coverage, $name) {
            $coverage->stop();
            $writer = new \SebastianBergmann\CodeCoverage\Report\PHP();
            $dir = base_path() . "/build/dusk";
            if (!file_exists($dir)) mkdir($dir, 0755, true);
            $writer->process($coverage, "{$dir}/{$name}.php.cov");
        });
    }

You will need to merge the PHP coverage data from build/dusk using the https://github.com/sebastianbergmann/phpcov after all tests are run.

All 15 comments

I would also very2 like if this feature availabe on the dusk. It will be a pleasure.

Because it's a different process, I don't think Dusk would be offering code coverage. The code being tested is not under PHPUnit's process.

Probably can add new simple report for how many test fail and success and a list of success test and fail test , just a simple one . Just an idea.

My idea is to add phpunit code coverage options into php artisan serve. So when any route is visited or requested, artisan serve will try to scrutinize the source code that is executed. I am not sure if that is possible.
Example: php artisan serve --coverage-text

Any updates on this?

This is well explained resource on the subject http://tarunlalwani.com/post/php-code-coverage-web-selenium/

The article above worked for me, its a clumsy solution but works!!

The article was more of mental note to myself with different approaches. I came up with following solution which seems to suite the Laravel framework without too many clumsy hacks.

Add following to any testing specific service providers (DuskServiceProvider.php could have a flexible variant of this I suppose)

    public function register()
    {
        if ($this->app->environment('production')) {
            throw new Exception('It is unsafe to run Dusk in production.');
        }
        if (!$this->app->runningInConsole()) {
            if ($this->app->environment('testing')) {
                try {
                    $this->triggerCoverage();
                } catch (Exception $e) {
                    Log::info("Dusk coverage: " . $e->getMessage());
                }
            }
        }
    }

    private function triggerCoverage() {
        $coverage = new \SebastianBergmann\CodeCoverage\CodeCoverage();
        $coverage->filter()
            ->addDirectoryToWhitelist(app_path());
        $name = microtime(true) . '.' . str_random(8);
        $coverage->start($name);
        $this->app->terminating(function () use ($coverage, $name) {
            $coverage->stop();
            $writer = new \SebastianBergmann\CodeCoverage\Report\PHP();
            $dir = base_path() . "/build/dusk";
            if (!file_exists($dir)) mkdir($dir, 0755, true);
            $writer->process($coverage, "{$dir}/{$name}.php.cov");
        });
    }

You will need to merge the PHP coverage data from build/dusk using the https://github.com/sebastianbergmann/phpcov after all tests are run.

Is there no better way ?

The crux of the complexity is that -- https://github.com/facebook/php-webdriver used by dusk is controlling the browser -- meaning, we have dusk running in separate console process, browser a separate process and the laravel backend server a separate process. We are interested in coverage data generated by the laravel backend server [ + laravel console process if you are running non-browser tests using dusk as we do ].

I could change https://github.com/laravel/dusk/issues/258#issuecomment-382381639 into a pull request such that

  1. Coverage data is collected and dumped when coverage is enabled
  2. Provide some console command for merging and generating the report (or do that automatically at the end of dusk)

It's not a trivial change though (for me at least :) ). I don't see any other approach.

Alternative is not using dusk at all and going back to https://github.com/laravel/browser-kit-testing

Hello

I am not using artisan dusk commend I made my own implement ion of DuskController that will run the server as part of the test. This way I can run phpunit and it will work on any Dev machine and in CI. This way I can collect the xdebug information better somehow? I do restart the server between each server ( as I could not find way to start at start and kill at the end)

In principle, assuming you are doing something that php artisan serve does along with what php artisan dusk does within a single php process you should be good collecting the coverage data for that process itself. It should not need any more hacks. That could be a good idea.

Although I am not sure how that would work as php artisan serve is blocking (unless we use threads).

No plans on supporting this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hackel picture hackel  路  6Comments

rauldeheer picture rauldeheer  路  4Comments

guirociozanini picture guirociozanini  路  6Comments

godruoyi picture godruoyi  路  6Comments

antonioribeiro picture antonioribeiro  路  5Comments