2
votes

I have a php project with unit tests included. I use Netbeans for development and would like to have phpunit intergration in my IDE. If I run phpunit from the commandline, it is working. If I press Alt+F6 to run tests in Netbeans no tests run, I get the message:

No tests executed (Perhaps an error occurred, verify in Output window.)

The structure (it is a Zend Framework 2 module):

Foo/
  src/
    Foo/
      Bar.php
      Baz.php
  tests/
    Foo/
      BarTest.php
    bootstrap.php
    phpunit.xml
  Module.php
  autoload_register.php

Contents of BarTest.php

namespace Foo;

use PHPUnit_Framework_TestCase as TestCase;

class BarTest extends TestCase
{
    public function testIsWorking ()
    {
        $this->assertTrue(true);
    }
}

My phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="bootstrap.php">
    <testsuite name="Foo">
        <directory>./</directory>
    </testsuite>
</phpunit>

My bootstrap.php:

// Set error reporting pretty high
error_reporting(E_ALL | E_STRICT);

// Get base, application and tests path
define('BASE_PATH',  dirname(__DIR__));

// Load autoloader
require_once BASE_PATH . '/autoload_register.php';

In the Netbeans project properties I tried:

  1. To set the tests directory to Foo/tests (this is required I think)
  2. To set specifically the bootstrap file (not required I think)
  3. To set specifically the XML configuration file (not required I think)
  4. To set specifically to run all *Test files (not required I think)

How can I ensure Netbeans can execute my phpunit tests?

4
Did you try running phpunit --bootstrap bootstrap.php Foo/tests ? Because thats what IDE ultimately does - Artjom Kurapov

4 Answers

3
votes

You can simply put all of this into your PHPUnit bootstrap.php:

<?php

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
0
votes

The problem is that Netbeans expect all tests situated in one root project folder. If it's not done this way, Code Coverage and others features are not working correctly. See enhancement for more informations.

This problem can be solved creating custom testSuite:

  • Create directory "test" or "tests" in your root project folder if it's not created yet.
  • Create file TestSuite.php in this "test" folder
  • Add this TestSuite.php in your Netbeans project settings.
<?php

class TestSuite extends PHPUnit_Framework_TestSuite
{
    public static function suite()
    {
        $suite = new TestSuite();

        $file = '/Foo/tests/Foo/BarTest.php';

        echo 'Adding test: ' . $file . "\n";
        $suite->addTestFile($file);

        //add here algorithm that will search and add all test files from your /tests directory

    return $suite;

}

Netbeans 7.2 will have nice feature to run all test in separate directory.

0
votes

Though I had set error_reporting to a high level, the PHP CLI did not have display_errors turned on. This was it, so now I got my reporting back...

0
votes

NetBeans is not finding the autoloader classes. Then, in the project settings you must say what the class autoloader, bootstrap, etc.

In my case just went in: Project Properties -> Testing -> PHPUnit. I set the option to "Use Bootstrap", and told the path of the bootstrap.

My bootstrap file has the autoloader then all my test classes were executed without errors.