2014-01-15 26 views
1

我几次开始做一些单元测试我的功能,但我真的无法找到如何正确执行此功能。我认为这很容易,但有些东西我错过了。简单的php单元测试,但种类卡住

/** 
* Tests model->function() 
*/ 
public function testFunction() { 
    // TODO Auto-generated model->testFunction() 
    $this->markTestIncomplete ("function test not implemented"); 
    $this->model->testFunction('', '5'); 
    $this->model->testFunction('test', ''); 
    $this->model->testFunction('test', 'a'); 
    $this->model->testFunction('1', '5'); 

} 

这就是我和phpUnit只是忽略这些测试。 我想要测试我的功能(需要2个参数,两个整数)并检查:

  • 这两个参数都不为空吗?
  • 都来自整数类型的参数吗?

有人可以帮我这个吗?

非常感谢!

回答

1

第一条语句

$this->markTestIncomplete() 

会导致PHPUnit来越过这个测试文件,并在输出中我将其标记为未完成(执行)。

其次,您的测试格式不正确。您需要创建该对象,然后对其进行测试。

public function setUp() 
{ 
    $this->model = new model(); 
} 

public function testFunction() 
{ 
    $this->assertEquals('test', $this->model->Function(5)); // Test what the function should return, using parameters replacing the 5 
} 

函数应该接受一个基于我在你的尝试中看到的参数。然后这个函数会返回一些东西,你可以评估一下。

(可选)您可以使用dataProvider向测试提供多个值并查看输出。查看PHPUnit手册了解更多信息。

+0

你是我的英雄!非常感谢 !!! – Alex