PHPUnit code coverage report generation fails - php

I'm trying some supposedly nice features of PHPUnit, but I cannot generate a freakin' code coverage report. What I get is:
rolf#dev ~/projects/current/tests $ scripts/phpunit --configuration $PROJECTS/current/tests/conf/inc/tests.xml
[...]
Generating code coverage report, this may take a moment.
[...]
Fatal error: require_once(): Failed opening required 'lib/DataSource.php' (include_path=':::') in path/to/lib/WS/DataParser.php on line 10
However, in this very class, a specific include path is defined, and the require_once works like a charm when the application is launched.
Could it be that PHPUnit cannot solve include paths ?
Thanks in advance and long live stackoverflöw!
Rolf

If you're using the latest PHPUNIT (3.5+) it might be because Sebastion has started using an autoloader himself within the program.
You've got to add the line
spl_autoload_register('__autoload');
after your __autoload() function gets created / included / required
so for example, the file that has my autoloader is called functions. in my bootstrap I do the following:
require_once(ROOT_PATH.'/lib/utils/functions.php');
spl_autoload_register('__autoload');

Are you changing your include_path in any of your tests/code? Because it looks really odd (:::)

Related

"No tests executed" I cannot run any PHP test in NetBeans at all

I am very new to NetBeans and I just wanted to run a Code Coverage teston my code developed earlier. I do not know what I am doing wrong.
I am using NetBeans 7.3.1 with Wamp Server 2.4, installed PHPUnit and Skeleton Generator through PEAR, and set those files in NetBeans settings.
I made the project using an existing sources. Running in my browser seems OK. I can even debug with XDebug.
But when I right click on the project name and "Test", it says "No tests executed.(0.0 s)" in the Test Result window, and this in the Output window:
PHPUnit 3.7.23 by Sebastian Bergmann.
Configuration read from C:\wamp\www\test\configuration.xml
Time: 141 ms, Memory: 2.00Mb
No tests executed!
Generating code coverage report in Clover XML format ... done
I tried running test on a PHP file, it returns an information dialog box "Test file for the selected source file was not found."
I right clicked on the PHP file, and selected "Tools->Create PHPUnit tests", it returns a warning dialog box "Tests were not generated for the following files: (file name) Review the log in Output Window." but nothing was changed in the Output window.
I generated PHPUnit Bootstrap and XML Configuration, but they did not help.
I wished I can provide screenshots, but I cannot. I am trying to be specific as possible.
I appreciate any help.
I got this error too.. I also got this in the Output tab
Fatal error: Class 'package\path\Tests\TestCase' not found in project\path\package\path\Tests\ClientTest.php on line 25
Here, TestCase is the subclass of PHPUnit_Framework_TestCase and that, along with the current test file is in a different directory structure than the source files. So, in my case, it was a path issue.
Something I noted is I need to add a bootstrap file to include files in the test path and the source path to the runtime.
This bootstrap could be generated by NetBeans. See instructions here: https://netbeans.org/kb/docs/php/phpunit.html#project-specific-configurations
I of course, had another bootstrap.php (added by another developer). I only had to link the bootstrap like follows,
Good luck!
PHPUnits message:
No tests executed!
just means that no tests were found. This can have multiple reasons:
First of all, if there are no tests, no tests are executed.
But it is also possible that there are tests, but the configuration says to skip those (for example some tests are grouped into a group named slow and the configuration XML says to exclude slow from being run).
For your case this most certainly is a configuration issue. I'm not fluent with Netbeans but as you can successfully execute PHPUnit already, that part of your setup looks working.
Next step is to find out when you invoke the test-runner where it looks for tests. It might just be that the tests directory is missing or not specified. E.g. check which configuration is in configuration.xml for example and compare that with the documentation to double-check all settings.
This is not a very concrete answer and somebody who is more fluent with Netbeans might help more, however as you'er not showing further screenshots, this is my honest best bet how to go on with trouble-shooting and why.
Seems I am getting closer to the solution. I found out the problem is not in the configurations, but in the code itself.
I do not know if this is a silly problem, but seems the test only accepts PHP codes written in OOP. I tried making another file with a class and I can make test file out of it with no problem. At least I can see the test result changes from "No test executed" to "No test passed, 1 test skipped"
My codes are written for months without OOP and I know nothing about it at all. Does this mean I have to rewrite all of them again?
It will happen when you put an assertion in a constructor of your test class and this assertion fails.
Then you will see only the warning 'No tests executed!' (not very helpful) but i suppose one should not put assertions in the constructors (this is in phpunit 4.1~).
for it was because my test names were not starting with 'test'

XMLdiff for php installation

I am trying to install xml diff ; https://github.com/mmacia/XMLdiff and i have not managed yet to make it work.Whenever i run any test example,i get
Fatal error: Interface 'PHPUnit_Framework_Test' not found in
C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php on line 85
Has anyone managed to install and use the library.I am using xampp on windows.
I believe your problem has to do with PHPUnit's Autoloader.php not being included. This file sets the php spl_autoloadspl_register function which is responsible for loading in interfaces and classes like PHPUnit_Framework_Test.
According to this SO question, you have to include the autoloader file manually. Without knowing more about your set-up and how that particular library works, I would say do something like this in the appropriate file(s):
// define phpunit path
if ( ! defined('PHPUNIT_PATH')) {
// define an absolute path to your PHPUnit dir
// CHECK THIS, i'm not good with php on windows:
define('PHPUNIT_PATH','C:\xampp\php\PEAR\PHPUnit');
}
// Then include the autoloader like this:
include PHPUNIT_PATH.'Autoloader.php';
I hope this helps you or anyone else out.
Check execution flags for C:\xampp\php\PEAR\PHPUnit\Framework\Framework\Test.php
The file needs to be executable by the user who is launching tests (probably you).

PHPUnit failed opening required file

I've browsed through similar problems on SO, but to no avail. I'm running PHP 5.3.6 and phpunit version 3.6.10. When attempting to execute a simple test:
require_once 'PHPUnit/Framework.php';
class UserTest extends PHPUnit_Framework_TestCase {
}
I receive the following error:
PHP Fatal error: require_once(): Failed opening required 'PHPUnit/Framework.php'
(include_path='.:/Users/username/pear/share/pear:/usr/lib/php/pear/:/Users/username/pear/share/pear/PHPUnit') in ...
When reinstalling PHPUnit, I'm not sure if the install location was duplicated, but it appears that when running which phpunit, the path is: /usr/bin/phpunit. However, it appears to also be installed in /Users/user/pear/bin/phpunit.
I've tried updating all channels and reinstalling PEAR and PHPUnit, but the problem still exists. I'm running on OSX Lion. Any help would be greatly appreciated.
Just remove the line
require_once 'PHPUnit/Framework.php';
and everything should work.
You don't need to include/require anything PHPUnit related since (at least) PHPUnit 3.6 any more and you can't include that file because it doesn't exist any more in the distribution.
The phpunit runner will take care of bootstrapping everything that is needed by PHPUnit :)
As others pointed out, Framework.php is not required anymore.
But in any case if you already have too many test files written and having the include statement, then fixing them going to be a cumbersome task. Which was the case I had to face.
If a quick workaround is needed, create an empty Framework.php file. That will resolve the problem.
Create an empty file named Framework.php under your PHPUnit directory. (eg: at: /usr/share/php/PHPUnit/Framework.php).
sudo touch /usr/share/php/PHPUnit/Framework.php

Can't require mage.php from an interactive shell

I would like to try some code snippets (about to make a script) which uses Magento's models and classes.
The problem is that I get the following error:
fdr#fderose-gtrade:/var/www/globaltrade$
fdr#fderose-gtrade:/var/www/globaltrade$ php -a
Interactive shell
php > require './app/Mage.php';
Fatal error: Class 'Mage' not found in /var/www/globaltrade/app/Mage.php on line 31
Line 31 of Mage.php is the following:
Mage::register('original_include_path', get_include_path());
Does anybody have an idea about what could be the cause? Thank you!
According to php.net
Autoloading is not available if using
PHP in CLI interactive mode.
see http://php.net/manual/en/features.commandline.interactive.php for more info (its a note towards the bottom of the description)
At first glance it seems that your issue stems from autoload. When you include your Mage.php file it appears that it then tries to run a php autoload and use the Mage class, but is failing in doing so. It's possible that the way that their autoload is functioning, the paths may not be correct when run from an interactive shell.

Why is PHPUnit in Hudson CI using my localhost path AND my actual filepath?

EDIT 3: Solved. See below.
EDIT 2: I think Chadwick's on the right track with his comment. Hudson/PHPUnit is taking the localhost (the Hudson workspace) AND my local file structure and using both to run the unit tests. So it's redeclaring everything that was already declared. Why is this happening and how can I change it?
I've since reported this issue on Hudson's JIRA Server. If I get a resolution there, I'll post here. Otherwise, any help would be appreciated since my builds are going nowhere now.
My build keeps failing and I can't for the life of me understand why. Here's what I get back.
phpunit:
[exec] PHP Fatal error: Cannot redeclare generate_options() (previously declared in
<http://localhost:8080/job/Goals/ws/Goals/includes/functions/registration_fns.php>:5) in /Users
/joshsmith/Sites/Goals/Goals/includes/functions/registration_fns.php on line 32
But this particular function starts at line 5 and ends at line 32! So what in the world is going on here?
And just so you know PHPUnit works on its own outside of Hudson. Here's my terminal output from a successful test run:
Macintosh:goals joshsmith$ phpunit alltests.class
PHPUnit 3.4.14 by Sebastian Bergmann.
.............................
Time: 14 seconds, Memory: 9.75Mb
OK (29 tests, 67 assertions)
Can anyone help me figure this craziness out?
EDIT: Under Chadwick's suggestion, I tried renaming the function in case it were trying to redeclare a function internal to Hudson. This didn't work, and is clearly some other obscure issue.
This has been solved. It was an issue where I had hardcoded the paths in a single initializer file. As a result, they were being redeclared by PHPUnit.
It sounds like generate_options() is declared both in your registration_fns.php file and in some file by Hudson. PHP function names are global, so this is an inherent risk of combining code from a third party.
One way around it is to rename one of the functions (and of course all calls to it) - you can modify the Hudson code where it is declared, or modify your own, but personally I recommend changing your own. Should you upgrade Hudson in the future, you'd likely have to modify it again.
Since PHP 5.3, you also have the option of using namespaces.

Categories