Closed vitorhorta closed 3 years ago
+1
+1
+1
+1
So many people are interested. Could one of you reproduce and fix the issue?
+1
Please provide instructions how to reproduce this issue and I will reopen this ticket.
Just try to make an api test and check the code coverage
Which version of codeception are you using?
2.2.0
@samdark This is a Yii issue, could you test it?
@rogoit have you enabled code coverage and whitelist (not white_list) in codeception.yml?
Yes i also try remote true and false on a vagrant. Try to run it from mac and vagrant. Xdebug settings are running fine on development.
I had the REST module enabled and got 0/46 methods covered. A removed REST module and executed codeception build afterwards it gave me code coverage.
In one scenario $this->getTestResultObject()->getCodeCoverage()
in codeception/codeception/src/Codeception/Test/Feature/CodeCoverage.php:16
returns null and in the other a SebastianBergmann\CodeCoverage\CodeCoverage
class.
@valorize What module does REST module use for connecting to API in your test suite?
- PhpBrowser:
url: http://mview.example.local/
- REST:
depends: PhpBrowser
url: http://mview.example.local
In one scenario \Codeception\Coverage\SuiteSubscriber::applyFilter:84 is called in the other not.
I will try to reproduce it.
I think I found the flaw. Without REST+PhpBrowser is the \Codeception\Coverage\Subscriber\Local
module active (see \Codeception\Coverage\Subscriber\Local::beforeSuite:34
) if so the applyFilter method directly after will $result->setCodeCoverage($this->coverage);
.
If REST is activated Local is not an active module, but \Codeception\Coverage\Subscriber\LocalServer or RemoteServer
are. But these do not execute $result->setCodeCoverage($this->coverage);
.
So I guress the fix would be: add $this->applyFilter($e->getResult());
to LocalServer::beforeSuite
Please make that change and check if it fixes your issue.
The change activates successfully the code coverage, but the report is still empty. I also had to adapt the LocalServer::afterSuite to the behaviour of Local::afterSuite. Soo I had to add: $this->mergeToPrint($e->getResult()->getCodeCoverage());
in LocalServer. It know reports just fine.
Sidequestion: When is codeception.yml used and when api.suite.yml? Is this overwritten when I supply the run command with the -c
param?
Edit: setAddUncoveredFilesFromWhitelist is never been called making the coverage: show_uncovered: false
option / setting kind of useless. Therefore I will add self::$coverage->setAddUncoveredFilesFromWhitelist($this->settings['show_uncovered']);
in the first line of \Codeception\Coverage\Subscriber\Printer::printConsole
, but I guess that's not the best place for that.
So basically we shouldn't consider only remote coverage when activating the REST module. So LocalServer and RemoteServer should integrate all functionality of \Codeception\Coverage\Subscriber\Local
.
Code coverage would be really awesome for API only applications.
@Naktibalda @valorize any updates on this? btw it looks like Yii label can be removed since the issue is not framework specific
I don't use PhpBrowser in my tests, but I use REST a lot, so I tried to generate code coverage report for one of my applications and I got empty report.
In order to get non-empty report I had to whitelist application code with this configuration:
coverage:
enabled: true
include:
- 'application/*'
@arustamov I do not maintain this project. I stepped through all relevant code of codeception and updated the code as described in the upper comments. For me the code worked quite well then.
@valorize Would you mind providing a PR for your changes? Would be really awesome to have code coverage working for API/REST apps.
@davidjeddy I'm sorry but I won't be able to. Timewise and because I left the company where I used codeception and where I adapted the code.
@valorize NNNNnnooooooo :(. Man, was really hoping for a fix :(
@davidjeddy the fix is described in https://github.com/Codeception/Codeception/issues/2220#issuecomment-305147001 and https://github.com/Codeception/Codeception/issues/2220#issuecomment-305151819 It is not to difficult to go into these classes with xdebug and check which parts need to be adapted once one knows where to look.
Hello @valorize, I've applied the changes in LocalServer as you told, however my coverage test just grab the data related to the /tests folder and none from the Controllers.
Is really necessary to downgrade the PhpUnit and Codeception versions? Because when I do it, all my tests crash.
Do you have any idea what can be happening?
Note: I'm testing a REST in another server.
Thanks in advance!
@davidjeddy here you are the changes @valorize mentioned above.
BEFORESUITE `public function beforeSuite(SuiteEvent $e) { $this->module = $this->getServerConnectionModule($e->getSuite()->getModules()); $this->applySettings($e->getSettings()); $this->applyFilter($e->getResult()); if (!$this->isEnabled()) { return; }
$this->suiteName = $e->getSuite()->getBaseName();
if ($this->settings['remote_config']) {
$this->addC3AccessHeader(self::COVERAGE_HEADER_CONFIG, $this->settings['remote_config']);
}
$knock = $this->c3Request('clear');
if ($knock === false) {
throw new RemoteException(
'
CodeCoverage Error.
Check the file "c3.php" is included in your application.
We tried to access "/c3/report/clear" but this URI was not accessible.
You can review actual error messages in c3tmp dir.
'
);
}
}`
AFTERSUITE `public function afterSuite(SuiteEvent $e) { if (!$this->isEnabled()) { return; } $this->mergeToPrint($e->getResult()->getCodeCoverage()); $coverageFile = Configuration::outputDir() . 'c3tmp/codecoverage.serialized';
$retries = 5;
while (!file_exists($coverageFile) && --$retries >= 0) {
usleep(0.5 * 1000000); // 0.5 sec
}
if (!file_exists($coverageFile)) {
if (file_exists(Configuration::outputDir() . 'c3tmp/error.txt')) {
throw new \RuntimeException(file_get_contents(Configuration::outputDir() . 'c3tmp/error.txt'));
}
return;
}
$contents = file_get_contents($coverageFile);
$coverage = @unserialize($contents);
if ($coverage === false) {
return;
}
$this->mergeToPrint($coverage);
}`
Somehow in RemoteServer::retrieveAndPrintHtml, I'm getting the following error that prevents from getting the coverage from Controllers:
[UnexpectedValueException]
phar error: "/tmp/C3syXhG1.tar" is a corrupted tar file (checksum mismatch of file <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 3.2 Final//EN>Index of /api//c3/re")
protected function retrieveAndPrintHtml($suite)
`protected function retrieveAndPrintHtml($suite) { $tempFile = tempnam(sys_get_temp_dir(), 'C3') . '.tar'; file_put_contents($tempFile, $this->c3Request('html'));
$destDir = Configuration::outputDir() . $suite . '.remote.coverage';
if (is_dir($destDir)) {
FileSystem::doEmptyDir($destDir);
} else {
mkdir($destDir, 0777, true);
}
$phar = new \PharData($tempFile);
$phar->extractTo($destDir);
unlink($tempFile);
}`
Exception trace: () at /var/www/.../codeception/base/src/Codeception/Coverage/Subscriber/RemoteServer.php:52 PharData->__construct() at /var/www/.../codeception/base/src/Codeception/Coverage/Subscriber/RemoteServer.php:52
Line 52 refers to this code: $phar = new \PharData($tempFile);
We need something like the PhPStorm debug session key i think
Am 26.11.2017 20:24 schrieb "Igor Augusto Brandão" <notifications@github.com
:
Somehow in RemoteServer::retrieveAndPrintHtml, I'm getting the following error that prevents from getting the coverage from Controllers:
[UnexpectedValueException] phar error: "/tmp/C3syXhG1.tar" is a corrupted tar file (checksum mismatch of file
protected function retrieveAndPrintHtml($suite)
`protected function retrieveAndPrintHtml($suite) { $tempFile = tempnam(sys_get_temp_dir(), 'C3') . '.tar'; file_put_contents($tempFile, $this->c3Request('html'));
$destDir = Configuration::outputDir() . $suite . '.remote.coverage';
if (is_dir($destDir)) {
FileSystem::doEmptyDir($destDir);
} else {
mkdir($destDir, 0777, true);
}
$phar = new \PharData($tempFile);
$phar->extractTo($destDir);
unlink($tempFile);
}`
Exception trace: () at /var/www/.../codeception/base/ src/Codeception/Coverage/Subscriber/RemoteServer.php:52 PharData->__construct() at /var/www/.../codeception/base/ src/Codeception/Coverage/Subscriber/RemoteServer.php:52
Line 52 refers to this code: $phar = new \PharData($tempFile);
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Codeception/Codeception/issues/2220#issuecomment-347031760, or mute the thread https://github.com/notifications/unsubscribe-auth/AC9S_3nh2laS2Rpl1uKZba0oApRTSaz3ks5s6brygaJpZM4FhN7y .
+1
Still 0% +1 from 2018. I'm using REST module within the Yii2 module but without the PhpBrowser - all the api calls are being performed locally. Got zero coverage on all the methods.
@russianlagman are you using php-debug as your coverage driver? I'm getting coverage just fine.
@SamMousa codecept --coverage just won't work if the coverage driver is missing.
I know, but there are multiple drivers; xdebug and phpdbg
@SamMousa How should we be defining phpdbg as the driver to use?
Google it, it depends on your environment; it's not hard
@SamMousa in any case I was using xdebug.
@russianlagman How did you set it to use phpdbg. I can't find any docs on Codeception about drivers. I've tried building it via phpunit directly but no luck :|
Closing this, it's old and about xdebug which you shouldn't use for code coverage.
Hi,
I'm having the same problem that was reported on Codeception/Codeception#1622 but that solution didn't work for me. I'm using Yii2 basic template and i'm trying to run Api tests that uses the REST module. The tests passes and the html files are created but I get 0 reports from coverage.
As mentioned on Codeception/Codeception#1622 I tried to downgrade the PHPUnit and Codeception version but it didn't work. Is there any other way to fix this issue? If not, what version should I use for both packages?
Thanks in advance