Zend Studio 和 PHPUnit 入门问题
我正在尝试创建我的第一个单元测试。我使用 Zend Studio,并且已将 phpUnit 库添加到我的项目中,方法是:
Project ->属性->添加库
当我将其作为 PHP 单元测试运行时,出现以下错误:
无法运行 PHPUnit 会话。只有 PHPUNIT 类可以作为 PHPUnit 测试运行。原因:在 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 */);
}
}
我该如何解决这个问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须删除测试用例的
__construct()
方法。 PHPUnit 将参数传递给构造函数,因此您必须将它们传递给parent::__construct()
或者(更可能的是)完全删除构造函数。另外,如果您正在使用 Zend Framework 并测试
Zend_Controller_Action
类,您可能需要考虑使用Zend_Test_PHPUnit_ControllerTestCase
因为它为您提供了大量的脚手架。请注意,每个测试都会从一条路线直接到达渲染的内容,这可能不够细粒度以满足您的需求。它不适合我们,所以我分别为控制器和视图创建了基本测试用例类。You must remove the test case's
__construct()
method. PHPUnit passes parameters to the constructor so you must either pass them along toparent::__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 usingZend_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.