我正在尝试在this tutorial之后向phpunit添加自定义断言,以验证以字符串形式返回的复数(例如
“ - 123 + 456i”
通过我正在测试的方法)到实部和虚部的定义精度。我已经将一个Complex.php类解析为实部和虚部,并将以下断言类组合为complexAssert.php:
require_once 'PHPUnit/Framework/Assert.php';
include_once getcwd().'/custom/Complex.php';
class complexAssert extends PHPUnit_Framework_Assert {
public function assertComplexEquals($expected, $actual, $message = '', $delta = 0)
{
$expectedComplex = new Complex($expected);
$actualComplex = new Complex($actual);
if (!($actualComplex->getReal() >= ($expectedComplex - $delta) &&
$actualComplex->getReal() <= ($expectedComplex + $delta))) {
return $this->fail($message);
}
if (!($actualComplex->getImaginary() >= ($expectedComplex - $delta) &&
$actualComplex->getImaginary() <= ($expectedComplex + $delta))) {
return $this->fail($message);
}
}
}
我的单元测试脚本:
require_once getcwd().'/custom/complexAssert.php';
require_once 'testDataFileIterator.php';
class EngineeringTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider providerIMSUM
*/
public function testIMSUM()
{
$args = func_get_args();
$expectedResult = array_pop($args);
$result = call_user_func_array(array('PHPExcel_Calculation_Engineering','IMSUM'),$args);
$this->assertComplexEquals($expectedResult, $result);
}
public function providerIMSUM()
{
return new testDataFileIterator('rawTestData/Calculation/Engineering/IMSUM.data');
}
}
当我只是在做一个assertEquals时,单元测试没有错误(但是失败了)...但是现在我添加了include并更改为我的新断言,它崩溃声称它不能调用未定义的方法assertComplexEquals()。
有没有人用自定义断言扩展phpunit,并且可以看到我做错了什么?
答案 0 :(得分:1)
显然,$this->someCustomAssertion
工作的唯一方法是扩展PHPUnit_Framework_TestCase
并在那里创建包装器方法,或静态调用自定义断言。
PHPUnit_Framework_TestCase
答案 1 :(得分:0)
最后,我选择不扩展现有的断言,而是修改我的复杂断言逻辑以返回一个简单的布尔值,然后可以使用assertTrue()进行测试,并使用可以通过简单检索的错误消息getMessage()用于在phpunit结果中显示。说实话,用这种方式感觉好多了
include_once __DIR__.'/Complex.php';
class complexAssert {
private $_errorMessage = '';
public function assertComplexEquals($expected, $actual, $delta = 0)
{
$expectedComplex = new Complex($expected);
$actualComplex = new Complex($actual);
if ($actualComplex->getReal() < ($expectedComplex->getReal() - $delta) ||
$actualComplex->getReal() > ($expectedComplex->getReal() + $delta)) {
$this->_errorMessage = 'Mismatched Real part: ' .
$actualComplex->getReal() .
' !== ' .
$expectedComplex->getReal();
return FALSE;
}
if ($actualComplex->getImaginary() < ($expectedComplex->getImaginary() - $delta) ||
$actualComplex->getImaginary() > ($expectedComplex->getImaginary() + $delta)) {
$this->_errorMessage = 'Mismatched Imaginary part: ' .
$actualComplex->getImaginary() .
' !== ' .
$expectedComplex->getImaginary();
return FALSE;
}
return TRUE;
}
public function getErrorMessage() {
return $this->_errorMessage;
}
}
我的单元测试脚本:
// Custom assertion class for handling precision of Complex numbers
require_once __DIR__.'/../../custom/complexAssert.php';
// Data Provider handler
require_once 'testDataFileIterator.php';
class EngineeringTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider providerIMSUM
*/
public function testIMSUM()
{
$args = func_get_args();
$expectedResult = array_pop($args);
$result = call_user_func_array(array('PHPExcel_Calculation_Engineering','IMSUM'),$args);
$complexAssert = new complexAssert();
$this->assertTrue($complexAssert->assertComplexEquals($expectedResult, $result, 1E-8),
$complexAssert->getErrorMessage());
}
public function providerIMSUM()
{
return new testDataFileIterator('rawTestData/Calculation/Engineering/IMSUM.data');
}
}
并且记录的phpunit结果足够清晰:
3) EngineeringTest::testIMSUB with data set #7 ('-12.34-5.67i', '-123.45-67.89', '#NUM!')
Mismatched String: 111.11 !== #NUM!
Failed asserting that false is true.