PHPUnit not showing errors - php

I'm starting to use this testing framework and I have the following problem.
I created a file /tests/CalculatorTest.php. Its the code inside this file:
<?php
class CalculatorTest extends PHPUnit_Framework_TestCase {
public function testInstance()
{
new Calculator;
}
}
?>
I know that I haven't any assert inside the first test but what I'm expecting to see is something like "Class 'Calculator' not found" and it only outputs the following:
Alan:writingTests alan$ vendor/bin/phpunit tests/
PHPUnit 4.2.0 by Sebastian Bergmann.
As you can see, there isn't any error.

I have just found the error, I had to change my php.ini configuration display errors.

Related

phpunit 8 and PHPUnit_Framework_TestCase

I wrote the following test:
use PHPUnit\Framework\TestCase;
//other uses
class StatusCheckerTest extends TestCase
{
public function testEntryValidation() {
//some code
$this->assertFalse($validation_result);
}
}
I tried to run it with phpunit StatusCheckerTest.php
But when I do it, I see error:
PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in
/var/www/html/nextcloud/apps/globalstatus/tests/OCA/GlobalStatus/Tests/StatusCheckerTest.php on line 12
I used PHPUnit 8.4.3 (also tried with 6.5.5). I installed it manually.
phpunit --version
PHPUnit 8.4.3 by Sebastian Bergmann and contributors.
Why does it look for PHPUnit_Framework_TestCase? It must be deprecated.
If you want to run only a specific test class, you need to pass it with a filter option:
phpunit --filter StatusCheckerTest

PHPUnit Plugin in Netbeans 8.2 gives Fatal error: Class 'PHPUnit_Framework_TestSuite' not found when trying to run a test case

When I try to run PHPUnit in Netbeans I face this error:
Fatal error: Class 'PHPUnit_Framework_TestSuite' not found in C:\Users\julian\AppData\Roaming\NetBeans\8.2\phpunit\NetBeansSuite.php on line 63
Done.
This happens both in Netbeans and CLI.
I started debugging this problem by navigating to this directory: C:\Users\julian\AppData\Roaming\NetBeans\8.2\phpunit\.
That directory contained one file: NetBeansSuite.php. I opened it to look for clues, and saw this line:
class NetBeansSuite extends PHPUnit_Framework_TestSuite {
What I didn't see is any concrete PHPUnit_Framework_TestSuite class.
What's next is that the NetBeansSuite.php file doesn't have any include or require language constructs that may include the PHPUnit_Framework_TestSuite class.
So, in order to fix the fatal error problem I should include the PHPUnit_Framework_TestSuite in NetbeansSuite.php.
This is a problem because I'm not the author of NetbeansSuite.php.
On top of that the author of NetbeansSuite.php wrote this in the comment section:
<b>WARNING: User changes to this file should be avoided.</b>
I read further in the comments:
* Copyright 2010 Oracle and/or its affiliates. All rights reserved.
I guess the NetbeansSuite.php file is outdated.
Searching on the net brought me to this stackoverflow question:
Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...?
They claim that using a namespaced version of PHPUnit_Framework_TestCase should fix the problem. So I did what they wrote.
I stubbornly changed NetBeansSuite.php.
Old code:
class NetBeansSuite extends PHPUnit_Framework_TestSuite {
New code:
require_once 'PHPUnit/Autoload.php';
use PHPUnit\Framework\TestCase;
class NetBeansSuite extends TestCase {
I tried to run the test case again, and this was the unfortunately result:
Fatal error: Declaration of CalculatorTest::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void in C:\wamp64\www\test\CalculatorTest.php on line 97
In other words, it gave me a new problem.
My system:
Windows 10
WAMP
php 7.2.14
PHPUnit 8.0.6
Netbeans version 8.2 (Netbeans PHPUnit plugin installed through Tools > Plugins. Version: 0.26.2)
My question is: does anybody know how the NetBeansSuite.php file should be like with the system described above?
The setUp method in my test case:
protected function setUp()
{
$this->object = new Calculator;
}
Update: The Skeleton Generator project is abandoned.
See link: https://github.com/sebastianbergmann/phpunit-skeleton-generator
So in order to fix the Declaration of CalculatorTest::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp() error the corresponding return type declaration should be used in the test case.
// I added : void
protected function setUp(): void
{
$this->object = new Calculator;
}
// : void needs to be added in the tearDown method as well
protected function tearDown(): void
Unfortunately, this gave me new problems:
Warning: require_once(PHPUnit/Autoload.php): failed to open stream: No such file or directory in C:\wamp64\www\test\CalculatorTest.php on line 4
Fatal error: require_once(): Failed opening required 'PHPUnit/Autoload.php' (include_path='.;C:\php\pear') in C:\wamp64\www\test\CalculatorTest.php on line 4
I solved this by manually installing PEAR and creating a new "PHPUnit" directory in C:\php\PEAR. Then I created a new Autoload.php file. I filled the content of Autoload.php with a PSR-4 example file found at: https://www.php-fig.org/psr/psr-4/.
This solved the Autoload problem, but I faced a new problem during the execution of a test case.
C:\wamp64\www\test>phpunit CalculatorTest.php
PHPUnit 8.0.6 by Sebastian Bergmann and contributors.
Time: 170 ms, Memory: 10.00 MB
No tests executed!
It shows No tests executed! but I have 5 tests.
I'll make a new question for this.
Here it is: PHPUnit 8 installed on Windows through PHAR shows No tests are executed
Initially indeed you should update your namespaces since it has been changed in more recent versions of PHPUnit.
The error however indicates that your CalculatorTest::setUp() method is not compatible with the PHPUnit version. Could you maybe post that method here?
For the same issue, I directly changed the file "NetBeansSuite.php" (C:\Users\USERNAME\AppData\Roaming\NetBeans\8.2\phpunit) as following :
class NetBeansSuite extends \PHPUnit\Framework\TestSuite {
After that, CLI and Netbeans was working.

PHPUnit Error: Call to undefined method Controller::request()

On running this test, I get Error: Call to undefined method OrderControllerTest::request()
<?php
use PHPUnit\Framework\TestCase;
class OrderControllerTest extends TestCase
{
public function testupload() {
$a='foo';
$output = $this->request('POST',['Order', 'upload',$a] );
}
}
PHPUNIT Version: 7.2.4. Appreciate any help
For the kenjis/ci-phpunit-test package if you run phpunit from the application/tests folder so it picks up on the included phpunit.xml and included TestCase Class then they should run.
cd application/tests
then, if you are using the composer installed version of phpunit in your project:
../../vendor/bin/phpunit
or if you're using the globally (apt etc.) installed version simply:
phpunit
see http://blog.a-way-out.net/blog/2015/06/12/codeigniter3-phpunit/#how-to-run-tests
The package kenjis/ci-phpunit-test extends the standard PHPUnit package so what you need to do is to extend the CIPHPUnitTestCase instead like in example below.
<?php
class OrderControllerTest extends CIPHPUnitTestCase
{
public function testupload() {
$a='foo';
$output = $this->request('POST',['Order', 'upload',$a] );
}
}
You may need to configure your IDE so that it can find CIPHPUnitTestCase class.

PHPUnit: Class <ClassName> could not be found in <ClassNameTest>.php

I'm at my wit's end. I must have read every SO question on the same topic, but no joy.
I can't get phpUnit working properly. I've successfully installed phpUnit and it's dependencies using PEAR. I've also modified my php.ini file and added the path to phpUnit to the include path: (".:/php/includes:usr/lib/php/pear").
To test phpunit is working, I've copied this simple class, so MyClassTest.php is as follows:
class MyClassTest extends PHPUnit_Framework_TestCase
{
public function testCalculate()
{
$this->assertEquals(2, 1 + 1);
}
}
Running "phpunit MyClassTest" produces the following output: (running "phpunit MyTestClass MyTestClass.php" produces the same result);
class MyClassTest extends PHPUnit_Framework_TestCase
{
public function testCalculate()
{
$this->assertEquals(2, 1 + 1);
}
}
PHPUnit 3.7.13 by Sebastian Bergmann.
Class 'MyClassTest' could not be found in 'MyClassTest.php'.
I can't think what's wrong. I've tried uninstalling and reinstalling phpunit/PHPUnit, but no joy. Can you identify what's wrong? If you need any more info, let me know and I'll edit this post. Thanks in advance.
PHP 5.3.15
PHPUnit 3.7.13
OSX 10.8.2
Your source code gets printed to the console, so it seems like you forgot <?php at the beginning.

Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...?

Why I'm getting this PHP error?
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...
For those arriving here after updating phpunit to version 6 or greater released on 2017-02-03 (e.g. with composer), you may be getting this error because phpunit code is now namespaced (check changelog).
You will need to refactor things like \PHPUnit_Framework_TestCase to \PHPUnit\Framework\TestCase
The PHPUnit documentation says used to say to include/require PHPUnit/Framework.php, as follows:
require_once ('PHPUnit/Framework/TestCase.php');
UPDATE
As of PHPUnit 3.5, there is a built-in autoloader class that will handle this for you:
require_once 'PHPUnit/Autoload.php';
Thanks to Phoenix for pointing this out!
For higher version of phpunit such as 6.4
You must use the namespace PHPUnit\Framework\TestCase
use TestCase instead PHPUnit_Framework_TestCase
// use the following namespace
use PHPUnit\Framework\TestCase;
// extend using TestCase instead PHPUnit_Framework_TestCase
class SampleTest extends TestCase {
}
I was running PHPUnit tests on PHP5, and then, I needed to support PHP7 as well. This is what I did:
In composer.json:
"phpunit/phpunit": "~4.8|~5.7"
In my PHPUnit bootstrap file (in my case, /tests/bootstrap.php):
// PHPUnit 6 introduced a breaking change that
// removed PHPUnit_Framework_TestCase as a base class,
// and replaced it with \PHPUnit\Framework\TestCase
if (!class_exists('\PHPUnit_Framework_TestCase') && class_exists('\PHPUnit\Framework\TestCase'))
class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
In other words, this will work for tests written originally for PHPUnit 4 or 5, but then needed to work on PHPUnit 6 as well.
You may get this error because you namespaced the file. If so you will need to specify that PHPUnit_Framework_TestCase is in the global namespace by preceding it with a backslash:
namespace AcmeInc\MyApplication\Tests
class StackTest extends \PHPUnit_Framework_TestCase {}
I submitted a crude PR to start conversation for correcting the documentation.
You can simply install PHPUnit to run commands (https://github.com/sebastianbergmann/phpunit/#php-archive-phar):
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit
Run single test
And then run PHPunit test:
phpunit test.php
Content of test file is following:
<?php
class StackTest extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
}
public function testSave()
{
}
}
Run test suite
Configuration of test suite: demosuite.xml. demo is directory containing all tests. Test files must be named as *_test.php (suffix).
<testsuites>
<testsuite name="DemoTestSuite">
<directory suffix="test.php">demo</directory>
</testsuite>
</testsuites>
Test suite runs with following commands:
phpunit -c demosuite.xml --testsuite DemoTestSuite
Assumption:
Phpunit (3.7) is available in the console environment.
Action:
Enter the following command in the console:
SHELL> phpunit "{{PATH TO THE FILE}}"
Comments:
You do not need to include anything in the new versions of PHPUnit unless you do not want to run in the console. For example, running tests in the browser.
I use ZF2 and work for me when replaced 'PHPUnit_Framework_TestCase' to '\PHPUnit\Framework\TestCase'
I got it working with
include("vendor/autoload.php");
at the top of my test function.
If you have Centos or other Linux distribution you have to install phpunit package, I did that with yum install phpunit and it worked. Maybe you can have to add a repository, but I think it has to work smooth with the default ones (I have CentOS 7)
It may well be that you're running WordPress core tests, and have recently upgraded your PhpUnit to version 6. If that's the case, then the recent change to namespacing in PhpUnit will have broken your code.
Fortunately, there's a patch to the core tests at https://core.trac.wordpress.org/changeset/40547 which will work around the problem. It also includes changes to travis.yml, which you may not have in your setup; if that's the case then you'll need to edit the .diff file to ignore the Travis patch.
Download the "Unified Diff" patch from the bottom of https://core.trac.wordpress.org/changeset/40547
Edit the patch file to remove the Travis part of the patch if you don't need that. Delete from the top of the file to just above this line:
Index: /branches/4.7/tests/phpunit/includes/bootstrap.php
Save the diff in the directory above your /includes/ directory - in my case this was the Wordpress directory itself
Use the Unix patch tool to patch the files. You'll also need to strip the first few slashes to move from an absolute to a relative directory structure. As you can see from point 3 above, there are five slashes before the include directory, which a -p5 flag will get rid of for you.
$ cd [WORDPRESS DIRECTORY]
$ patch -p5 < changeset_40547.diff
After I did this my tests ran correctly again.
NOTICE: Command php bin/console generate:doctrine:crud also create TestController in src/Tests so it can throw error when you tried to start server if you don't have UnitTests. Remove the file fix it!
For me, it was because I ran
$ phpunit .
instead of
$ phpunit
when I already had a configured phpunit.xml file in the working directory.
I am using php 5.6 on window 10 with zend 1.12 version for me adding
require_once 'PHPUnit/Autoload.php';
before
abstract class Zend_Test_PHPUnit_ControllerTestCase extends
PHPUnit_Framework_TestCase
worked. We need to add this above statement in ControllerTestCase.php file

Categories