PHPUnit runs only from the command line. Why? - command-line

I have the following code:
<?php
class MyTest extends PHPUnit_Framework_TestCase
{
public function testCalculate()
{
$this->assertEquals(2, 1 + 1);
}
}
?>
When I open the PHP file in the browser, I get the following error:
Fatal error: Class 'PHPUnit_Framework_TestCase' not found
However, if I use the command line it works fine:
phpunit [local_path_here]/testcase.php
Result:
.
Time: 0 seconds, Memory: 5.00Mb
OK (1 test, 1 assertion)
Why is that? How can I make it to run it in the browser as well?

You can integrate add-on for running unit tests via native web GUI:
https://github.com/NSinopoli/VisualPHPUnit

You can't run unit tests in the browser. Maybe in the future: http://sebastian-bergmann.de/archives/638-PHPUnit-3.0.html#c4983
If you want to view the code coverage run
phpunit --coverage-html=coverage testcase.php
and then open the index.html file in the coverage directory.
Otherwise, you have to run your tests from the command line.

You may have a different include path on the command line. Check to see whether you have a php-cli.ini file in addition to the normal php.ini file. The first one will be used when you run PHP from the command line. That's probably got a different include_path setting. It might include the PEAR directory, for example, if PHPUnit was installed via PEAR.

You can use eclipse also to run phpunit.
follow the link below
http://pkp.sfu.ca/wiki/index.php/Configure_Eclipse_for_PHPUnit

Related

PHPUnit is Working With Command Line, but not with Eclipse

Whenever I run a test file from eclipse it works and display result as:
. 1/1 (100%)
Time: 371 ms, Memory: 8.00MB
OK (1 test, 1 assertion)
But If I run the same test from the Eclipse Oxygen, I get the error:
PHP Fatal error: Declaration of PHPUnitLogger::addFailure(Test $test, AssertionFailedError $e, $time): void must be compatible with PHPUnit\Framework\TestListener::addFailure(PHPUnit\Framework\Test $test, PHPUnit\Framework\AssertionFailedError $e, float $time): void in /tmp/phpunit_printer/PHPUnitLogger.php(415) : eval()'d code on line 1
I downloaded phpunit-7.1.phar, and installed it globally by making it executable and moving it to /usr/local/bin and for Eclipse, I have downloaed in the Download directory and without making it executable I added it into my Eclipse Project as an External Phar but it is not working and giving the above error.
Can someone help me fixing the issue?
By the way, I am a NetBeanse User, but NetBeans is not supporting PHP7.1 that's why moving to Eclipse.
UPDATE
Here is my test file:
<?php
namespace tests\Unit\;
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
public function testSimple() {
$this->assertEquals(1, 1);
}
}
I had exactly the same problem with PHPUnit 7.5.14 and didn't find a solution.
Since I wanted to test my application running with PHP 7.1, I decided to try PHPUnit 6 and it worked at once. So PHPUnit 6.5.14 did the trick for me.
It seems Eclipse is not compatible with PHPUnit 7. My Eclipse Version is 2018-12 (4.10.0) with PDT 6.2. After switching to PHPUnit 6 other problems (that I managed to find workarounds for) were gone too. For example the generated class code overrides TestCase::__construct without calling the parent constructor. It seems to be a problem with PHPUnit 7, not so with 6.
I didn't find a compatibility chart of Eclipse PDT to support my thesis.

How can I fix tests in Ember testem with errors such as 'could not load', 'failed', 'could not find module' or 'died'?

I managed to get a couple of EAK/grunt based Ember apps upgraded to 1.11 with HTMLBars, and then got them migrated to Ember CLI/Brocolli. The unit tests were setup for karma test runner so I'm looking at how to get those running in the CLI projects now, but I didn't write the tests and really have no experience with unit testing javascript modules.
Searching around the iNet, I can see that others have also used karma becasue of its coverage output and are trying to get it to work with Ember CLI, but that Ember Core isn't supporting it, though they say anyone should be able to get it set up with a custom addon. I'm also trying to use the 'testem' runner to see what sticks with that.
The Ember site does have an 'automating tests with runners' page for v1.10, with sections on 'testem' and 'karma', but it doesn't appear for v1.11 so I can't tell from that site what is or isn't relevant. But it seems like I should be able to work out a solution for the karma test runner, so I added the old devDependencies to the project package.json:
"karma": "^0.12.31",
"karma-chai": "~0.1.0",
"karma-chrome-launcher": "~0.1.2",
"karma-coverage": "~0.2.1",
"karma-firefox-launcher": "~0.1.3",
"karma-junit-reporter": "~0.2.1",
"karma-mocha": "~0.1.3",
"karma-phantomjs-launcher": "~0.1.2",
"karma-sinon-chai": "~0.1.5"
I also dropped the old 'karma.conf.js' (along with a few other karma confs) in the project and updated the paths inside (from 'vendor' to 'bower_components'). I did find a 'ember-cli-karma' node mode and installed it, but it seems to just have a 'package.json'. It has no docs and seems like just a stubbed out starter project with no implementation. I also installed 'karma', 'karma-cli' and 'testem' node modules.
The testem docs say to add you src and test files to 'testem.json', but with out examples I don't know what that means; a list of every src and test file? With what path; relative, absolute? Forward slashes, backslashes? preceded with / or ./ or ../? I just left them out because I think the system just finds the src and tests by convention.
When I run 'karma init' I get:
readline.js:529
this.line = this.line.slice(this.cursor);
^
TypeError: Cannot read property 'slice' of undefined
When I run 'testem' I get:
TEST'EM 'SCRIPTS!
Open the URL below in a browser to connect.
http://localhost:7357/aN;0faN;NaNf
...then the project's '../tests/index.html' loads in a browser, but is not able to 'find' any of the asset files (css, js) so nothing executes or renders correctly. I just see template expressions ({{content-for 'head'}}, etc).
When I run 'ember test' I get:
Building...BuildingBuilding.Building..Building...Built project successfully.
1..0
# tests 0
# pass 0
# fail 0
# ok
No tests were run, please check whether any errors occurred in the page (ember test --server) and ensure that you have a test launcher (e.g. PhantomJS) enabled.
When I run 'ember test --server' I get:
The test index.html loaded in a browser with a test report. When I uncheck 'hide passed tests' the report indicates '29 passed, 28 failed'. It has 11 sections where a particular test may have 3 problems such as 'could not load', 'failed', 'could not find module', 'attempting to register an unknown factory' or 'died'.
With this, I'm obviously running testem and not karma, so may as well work on getting testem working and figure out karma later. If there were more examples and migration troubleshooting docs I might have a systematic way to work through some of these problems.
I ran into "No tests were run,..." problem recently after a node upgrade. I fixed it with a:
npm install -g phantomjs
This provides some additional options as well:
https://github.com/ember-cli/ember-cli/issues/3969
I had the Cannot read property 'slice' of undefined error on MS Windows, running via MSys2. I have solved it by using karma init from an ordinary cmd prompt.

Why would Eclipse not be able to include a file when running a PHPUnit test?

I have the following class and unit test in a PHP project in Eclipse:
I know my unit test works as I can run it at the command line:
Now I want to run this test from Eclipse. I set up PHP Unit in Eclipse like this:
However, when I run the PHPUnit tests:
It tells me that it can't include the class file:
/usr/bin/php -c /var/folders/UA/UAv38snBHd0QMgEPMCmM9U+++TM/-Tmp-/zend_debug/session4910937990995915704.tmp -d asp_tags=off /Applications/eclipse/plugins/org.phpsrc.eclipse.pti.tools.phpunit_0.5.0.R20101103000000/php/tools/phpunit.php --log-junit /var/folders/UA/UAv38snBHd0QMgEPMCmM9U+++TM/-Tmp-/pti_phpunit/phpunit.xml /Volumes/data/domains/et/extjslayout/phpunittest/tests
PHP Warning: include_once(../Product.php): failed to open stream: No such file or directory in /Volumes/data/domains/et/extjslayout/phpunittest/tests/ProductTest.php on line 3
PHP Warning: include_once(): Failed opening '../Product.php' for inclusion (include_path='/usr/local/PEAR') in /Volumes/data/domains/et/extjslayout/phpunittest/tests/ProductTest.php on line 3
PHP Fatal error: Class 'Product' not found in /Volumes/data/domains/et/extjslayout/phpunittest/tests/ProductTest.php on line 9
Why would PHPUnit be able to find the class when run from the command line but not when run from Eclipse?
When you start something from the command line, the "current directory" has a well-defined meaning: It's the directory where you started the command.
In Eclipse, what is the "current directory"? It's probably the directory from which you started Eclipse or maybe the folder in which Eclipse is installed.
I haven't used PHP in Eclipse before but for other languages, I can set the current directory in the launch config somewhere. If that doesn't work, define a variable which points to your project and then use absolute paths (using that variable as a starting point).
Have same problem. Found only solution by creating tests with internal PHPUnit wizard like at this screenshot:
Source: HowTo create a Test Case Class from a PHP Class
But following investigate show that your test case file should contain reference to tested code for example like this: require_once 'C:\Apache2\htdocs\jobeet\src\Ibw\JobeetBundle\Utils\Jobeet.php';
Other experiments with plugin config not bringing luck. So in my opinion PHPUnit from PHP Tools not well developed plugin. Consider using MakeGood plugin as better alternative.

GWT Junit - error='no compilation unit for that type was seen'

I'm trying to run a GWT unit test in a sample app. I ran
cmd /c /java/gwt-windows-1.6.4/webAppCreator.cmd -out gwttasks com.gwttasks.GwtTasks
Copied in junit-4.5.jar into a lib directory, and added that to the classpath.
Ran:
cmd /c /java/gwt-windows-1.6.4/junitCreator.cmd -junit lib/junit-4.5.jar -module com.gwttasks.GwtTasks -eclipse GwtTasks com.gwt
tasks.unit.GwtJunit
When I try to run any of the generated cmd file (such as GwtJunit-hosted.cmd) or any of the launch files, I get the following error. All the web pages I've seen say to add the test source to the classpath, but it's already there, so that's not the problem. Anyone else seen this?
com.google.gwt.junit.JUnitFatalLaunchException: The test class 'com.gwttasks.unit.GwtJunit' was not found in module 'com.gwttasks.GwtTasks'; no compilation unit for that type was seen
at com.google.gwt.junit.JUnitShell.checkTestClassInCurrentModule(JUnitShell.java:390)
at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:626)
...
The answer could be found here :
http://raibledesigns.com/rd/entry/testing_gwt_applications
In netbeans I added the src/java and test to the class path and debugging worked!!!!!
THANKS
I just want to add that I had the same problem, because I did the (very silly) mistake to not put the GWTTestCase class into the "client" directory, but into another one. No wonder it wasn't found ;)

Debug some PhpUnit tests in Eclipse

I use Eclipse PDT for PHP.
I can run my PhpUnit tests : works fine.
But I can not debug my unit tests.
Has someby already done this ?
Can somebody help doing this ?
Thanx,
Messaoud
An example is more worth than 1000 words :
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php';
class MyTestCase extends PHPUnit_Framework_TestCase {
protected function setUp() {
parent::setUp ();
}
function testSimple() {
echo "horray !";
}
protected function tearDown() {
parent::tearDown();
}
static function main() {
$suite = new PHPUnit_Framework_TestSuite( __CLASS__);
PHPUnit_TextUI_TestRunner::run( $suite);
}
}
if (!defined('PHPUnit_MAIN_METHOD')) {
MyTestCase::main();
}
the key thing is :
provide a main method in your testcase
test if the test is executed directly (via php MyTestCase.php) or by phpunit itself. if executed directly - just start the testrunner.
know you can debug your testcase.
We can solve this issue with our Eclipse plugin MakeGood.
MakeGood provides a simple way to debug your tests. You only run a test in Debug Mode.
For more information, see the user guide.
For others who are wondering if there are simple instructions for configuring Eclipse/Aptana with phpunit, here's a website I have found:
http://pkp.sfu.ca/wiki/index.php/Configure_Eclipse_for_PHPUnit
What you have to do basically is:
Make sure your PEAR libraries are in your project's include path. Right click on the project in the navigator window and click Properties. You'll see there's a section for PHP Include Path (or PHP Build Path in Aptana for my version), open that and add your PEAR libraries to your include/build path so that Eclipse knows about phpunit.
Create a debug configuration which runs the phpunit.php file (you might need to add the .php extension to the file if it is running with a shebang, as the case is in Mac OS X).
So with phpunit.php file as the "Start Action" script, set "PHP Script Arguments" so that the PHPUnit testing file you are interested with is run by phpunit.php. Add any other command line arguments, to suit you. eg. --verbose is a good option. You can also use variables like ${resource_loc} to have Eclipse replace it with the current file for example.
Run your debug configuration and enjoy debugging!
You do not need to modify your test files or anything, they'll work out of the box.
I finally run debugging parallel to command line in eclipse 3.4. Debugging i run as "PHP web page", my minimal code
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php';
class XTest extends PHPUnit_Framework_TestCase{
public function testX(){
//...
}
}
if (!defined('PHPUnit_MAIN_METHOD')) {
header('Content-type:text/plain; charset=utf-8');
PHPUnit_TextUI_TestRunner::run( new PHPUnit_Framework_TestSuite( 'XTest'));
}
I have confirmed by setting a breakpoint in my setUp() method inside my unit test by following the instructions here:
How to Debug Your PHP Unit Tests in Eclipse
It involves copying the /usr/bin/phpunit file to your project (so it's accessible through eclipse's GUIs), and add the .php extension to it. From there, goto your debug configs and set the PHP-File to that phpunit.php file.
The next important step worked great for me, because I'm using Yii which provided me with a bootstrap.php file. Put something like this in your args:
--bootstrap=${workspace_loc}/my-project/trunk/protected/tests/bootstrap.php ${workspace_loc}/my-project/trunk/protected/tests/unit/SomeClassToTest.php