我正试图进行单元测试,看看它引入的明显的积极因素,我正在尝试为我前几天写的一个类写一个单元测试.(我知道这与TDD相反,请耐心等待)
我的班级,Image与其他一些人一起用于图像处理.
Image基本上包装GD图像资源并与其一起存储数据.例如,一个实例Image将始终包含它的当前状态,即调整大小时的新宽度/高度,原始图像数据等.
该Image班还包含方法,
$image->loadFromPath()Image实例的属性创建新的GD图像资源,例如,用于调整图像大小以保持背景透明度等.我正在努力的是如何使用PHPUnit正确地测试这个类.我已经完成了一些阅读,我对如何处理它有一些相互矛盾的想法,我不知道什么是正确的.我,我
那么,哪些是正确的,如果有的话?
我在Windows上运行XAMPP.
我最近从PEAR CLI工具下载了PHPUnit.
一切看起来很好,(eclipse正确建议代码完成,这意味着它被正确地添加到include_path),但每当我尝试通过键入phpunit(它在环境PATH变量中)通过控制台工作时,它会挂起秒或两秒然后关闭(就像它被终止一样).
我也尝试过phpunit --help,但没有成功.
也试过了phpunit --verbose.
有没有其他人有同样的问题或知道解决方案?
有没有人知道用PHPUnit区分FALSE和NULL的可靠方法?
我试图在断言中的返回值中区分NULL和FALSE.
这失败了:
$this->assertNotEquals(FALSE, NULL);
Run Code Online (Sandbox Code Playgroud)
这些断言通过:
$this->assertFalse(NULL);
$this->assertNull(FALSE);
Run Code Online (Sandbox Code Playgroud)
编辑:对于某些上下文,这是为了区分错误状态(FALSE)和空结果(NULL).为了确保功能正常返回,我需要区分这两者.谢谢
编辑...根据我正在测试的一些问题,我正在添加测试.
Class testNullFalse extends PHPUnit_Framework_TestCase{
public function test_null_not_false (){
$this->assertNotEquals(FALSE, NULL, "False and null are not the same");
}
public function test_null_is_false (){
$this->assertFalse(NULL, "Null is clearly not FALSE");
}
public function test_false_is_null (){
$this->assertNull(FALSE, "False is clearly not NULL");
}
public function test_false_equals_null(){
$this->assertEquals(FALSE, NULL, "False and null are not equal");
}
public function test_false_sameas_null(){
$this->assertSame(FALSE, NULL, "False and null are not the same");
}
public function test_false_not_sameas_null(){
$this->assertNotSame(FALSE, NULL, …Run Code Online (Sandbox Code Playgroud) 我一直试图echo填写我的phpunit测试但到目前为止没有运气.
我阅读了有关xml配置文件的文档,显然debug参数是我正在寻找的.不幸的是它仍然无效.无论如何这是我的xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true"
processIsolation="true"
verbose="true"
debug="true">
</phpunit>
Run Code Online (Sandbox Code Playgroud)
这两个processIsolation和verbose被接受,但debug并非如此.
当我直接将它传递给phpunit时,该命令实际上工作得很好:
phpunit --debug MyTest.php # here stuff is echoed correctly
Run Code Online (Sandbox Code Playgroud)
但是使用xml配置文件看起来它被忽略了.
我正在寻找一种如何使用不同的设置多次运行测试用例的方法.
我正在测试一个数据库访问类(几十种测试方法),并希望在"正常模式"下测试它,然后在"调试模式"下测试它.两种模式都必须产生相同的测试结果.
在测试用例设置中是否有可能这样做?或者重写run()方法?我当然不想写两次测试:)
谢谢
编辑:GOT IT!
public function run(PHPUnit_Framework_TestResult $result = NULL)
{
if ($result === NULL) {
$result = $this->createResult();
}
/**
* Run the testsuite multiple times with different debug level
*/
$this->debugLevel = 0;
print "Setting debug level to: " . $this->debugLevel . PHP_EOL;
$result->run($this);
$this->debugLevel = 8;
print "Setting debug level to: " . $this->debugLevel . PHP_EOL;
$result->run($this);
$this->debugLevel = 16;
print "Setting debug level to: " . $this->debugLevel . PHP_EOL;
$result->run($this);
return $result;
}
public …Run Code Online (Sandbox Code Playgroud) 我在Laravel中设置了以下内容:
在/app/controllers/MyController.php:
class MyController extends BaseController {
const MAX_FILE_SIZE = 10000;
// ....
}
Run Code Online (Sandbox Code Playgroud)
在/app/tests/MyControllerTest.php:
class MyControllerTest extends TestCase {
public function myDataProvider() {
return [
[ MyController::MAX_FILE_SIZE ]
];
}
/**
* @dataProvider myDataProvider
*/
public function testMyController($a) {
// Just an example
$this->assertTrue(1 == 1);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行时,vendor/bin/phpunit我收到以下错误:
PHP Fatal error: Class 'Controller' not found in /home/me/my-app/app/controllers/BaseController.php on line 3 Fatal error: Class 'Controller' not found in /home/me/my-app/app/controllers/BaseController.php on line 3
如果我删除对 …
我正在尝试从我编写的以下类中测试方法(函数比显示的更多,基本上,每个函数都是_*()方法):
class Validate {
private static $initialized = false;
/**
* Construct won't be called inside this class and is uncallable from the outside. This prevents
* instantiating this class. This is by purpose, because we want a static class.
*/
private function __construct() {}
/**
* If needed, allows the class to initialize itself
*/
private static function initialize()
{
if(self::$initialized) {
return;
} else {
self::$initialized = true;
//Set any other class static variables here
}
}
... …Run Code Online (Sandbox Code Playgroud) 假设我有一节课:
class XMLSerializer {
public function serialize($object) {
$document = new DomDocument();
$root = $document->createElement('object');
$document->appendChild($root);
foreach ($object as $key => $value) {
$root->appendChild($document->createElement($key, $value);
}
return $document->saveXML();
}
public function unserialze($xml) {
$document = new DomDocument();
$document->loadXML($xml);
$root = $document->getElementsByTagName('root')->item(0);
$object = new stdclass;
for ($i = 0; $i < $root->childNodes->length; $i++) {
$element = $root->childNodes->item($i);
$tagName = $element->tagName;
$object->$tagName = $element->nodeValue();
}
return $object;
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何单独测试?在测试这个类时,我也在测试DomDocument类
我可以传入文档对象:
class XMLSerializer {
private $document;
public function __construct(\DomDocument $document) { …Run Code Online (Sandbox Code Playgroud) 当试图调用该press()方法时,我总是得到
InvalidArgumentException:无法访问的字段""
在那条线上.
根据文件:
"按"具有给定文本或名称的按钮.
我的方法:
press('Create')
Run Code Online (Sandbox Code Playgroud)
我的按钮是
<button class="btn btn-lg btn-primary" type="submit" name="submit">Create</button>
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用name相同的结果.
我也试过submitForm('Create')哪个有效,但seeInDatabase('employees', ['email' => 'john@email.com'])总是失败.
无法在数据库表[employees]中找到与属性[{"email":"john@email.com"}]匹配的行.
这是我的完整方法
public function testExample()
{
$this->actingAs(\App\Employee::where('username', 'grant')->first())
->visit('/employees/create')
->type('john', 'username')
->type('1234', 'password')
->type('john@email.com', 'email')
->submitForm('Create')
->seeInDatabase('employees', ['email' => 'john@email.com'])
->see('Employee Directory');
}
Run Code Online (Sandbox Code Playgroud)
更新1
这是我的表格:
{!! Form::open(['route' => 'employees.store', 'method' => 'POST']) !!}
@include('employees.form')
{!! Form::close() !!}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我的字段无法在表单之外.