2

I am trying to create my first unit test. I use Zend Studio, and I have added the phpUnit library to my project by going to:

Project -> Properties -> Add Library

When I run it as PHP Unit Test I get the following error:

Unable to run a PHPUnit session. Only PHPUNIT classes can be run as PHPUnit tests. Reason: Not tests found in IndexControllerTest.php

IndexControllerTest.php:

<?php
require_once 'application\controllers\IndexController.php';
require_once 'PHPUnit\Framework\TestCase.php';
/**
 * IndexController test case.
 */
class IndexControllerTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var IndexController
     */
    private $IndexController;
    /**
     * Prepares the environment before running a test.
     */
    protected function setUp ()
    {
        parent::setUp();
        // TODO Auto-generated IndexControllerTest::setUp()
        $this->IndexController = new IndexController(/* parameters */);
    }
    /**
     * Cleans up the environment after running a test.
     */
    protected function tearDown ()
    {
        // TODO Auto-generated IndexControllerTest::tearDown()
        $this->IndexController = null;
        parent::tearDown();
    }
    /**
     * Constructs the test case.
     */
    public function __construct ()
    {
        // TODO Auto-generated constructor
    }
    /**
     * Tests IndexController->init()
     */
    public function testInit ()
    {
        // TODO Auto-generated IndexControllerTest->testInit()
        $this->markTestIncomplete("init test not implemented");
        $this->IndexController->init(/* parameters */);
    }
    /**
     * Tests IndexController->indexAction()
     */
    public function testIndexAction ()
    {
        // TODO Auto-generated IndexControllerTest->testIndexAction()
        $this->markTestIncomplete("indexAction test not implemented");
        $this->IndexController->indexAction(/* parameters */);
    }
}

How do I fix that?

Teodor Talov
  • 1,933
  • 2
  • 25
  • 39
  • How did you run the test? Did you do "Right click the script" > "Run as" > "PHPUnit Test"? – Gordon May 04 '11 at 17:22
  • Yes, this is exactly what I did. – Teodor Talov May 04 '11 at 17:27
  • 1
    remove the parent:: calls and try again pls, though that shouldnt be reason for the error (the calls just aint needed). I have this error from time to time but it usually goes away when I rerun the test a second time. – Gordon May 04 '11 at 17:29
  • I just did, I got the same thing. :( – Teodor Talov May 04 '11 at 17:33
  • hmm, odd. I can run the test fine (substituting IndexController for a StdClass). Can you restart Zend Studio? Do you have PHPUnit installed at the command line and can see if it works from there? – Gordon May 04 '11 at 17:36
  • No, i dont have it installed. I was trying to avoid that. I restarted Zend Studio, but no luck – Teodor Talov May 04 '11 at 17:42
  • No idea. Sorry :( If no one else comes up with an idea, try the [Zend Forums](http://forums.zend.com/index.php) and/or open a support ticket. – Gordon May 04 '11 at 17:45
  • I'd recommend running your unit tests from the command line. The Zend Studio version isn't going to scale for you (e.g. continuous integration) and has some limitations in terms of code coverage measurement. Oh, and your "Tests Class->method()" comments are good but "@covers Class::method" is even better. It describes what's being tested and PHPUnit will use it for more concise code coverage reporting. $.02 – allnightgrocery May 05 '11 at 13:27

1 Answers1

3

You must remove the test case's __construct() method. PHPUnit passes parameters to the constructor so you must either pass them along to parent::__construct() or--more likely--remove the constructor entirely.

Also, if you are using Zend Framework and testing Zend_Controller_Action classes, you may want to consider using Zend_Test_PHPUnit_ControllerTestCase as it provides a lot of scaffolding for you. Note that each test will go from a route straight through to the rendered content which might not be fine-grained enough for your needs. It wasn't for ours so I created base test case classes for controllers and views separately.

David Harkness
  • 35,992
  • 10
  • 112
  • 134
  • 2
    You can leave __construct() methods in unit test classes. It's superfluous but won't harm anything. – allnightgrocery May 05 '11 at 13:24
  • @Inkspeak - If your tests uses `@dataProvider`s the constructor will break them. It also won't set the test's name which appears to interfere with running tests in isolation. – David Harkness May 05 '11 at 20:19
  • 1
    I don't see @Teodor using @dataProvider and, per my comment on the original post, I recommended he run his tests via a suite. Thanks for clarifying though. – allnightgrocery May 06 '11 at 01:20
  • I'm just saying that removing the constructor is a good step toward sanity. It's possible that since all tests are incomplete that Zend Studio doesn't consider the test case to have any tests. I would focus on getting it to work from the command line first and only then involve the IDE. – David Harkness May 06 '11 at 07:26