标签: phpunit

PHPUnit测试问题 - 如何对我的班级进行单元测试

我正试图进行单元测试,看看它引入的明显的积极因素,我正在尝试为我前几天写的一个类写一个单元测试.(我知道这与TDD相反,请耐心等待)

我的班级,Image与其他一些人一起用于图像处理.

Image基本上包装GD图像资源并与其一起存储数据.例如,一个实例Image将始终包含它的当前状态,即调整大小时的新宽度/高度,原始图像数据等.

Image班还包含方法,

  • 从文件,字符串数据或URL创建自己,例如 $image->loadFromPath()
  • 从当前Image实例的属性创建新的GD图像资源,例如,用于调整图像大小以保持背景透明度等.
  • 克隆GD图像资源以在操作类中使用

我正在努力的是如何使用PHPUnit正确地测试这个类.我已经完成了一些阅读,我对如何处理它有一些相互矛盾的想法,我不知道什么是正确的.我,我

  1. 为每个类的方法编写一个测试.我在某处读到了我应该测试的每一种方法.但是,有些方法会运行其他方法(我也可以添加),因此您将拥有一系列依赖关系.但我也读到每个单元测试应该独立于另一个.那么如果是这样的话我该怎么办?
  2. 将每个测试写为类的使用路径.我还读到某个地方,每个测试应该代表您可以在课程中使用的1个路径/使用路径.因此,如果您涵盖所有用途,您最终将获得完整的代码覆盖率.

那么,哪些是正确的,如果有的话?

php phpunit unit-testing

13
推荐指数
2
解决办法
2613
查看次数

将PHPT测试用例与PHPUnit集成

如何让PHPUnit运行我的PHPT测试用例并将通过/失败状态集成到整体指标中?我已经知道如何使用run-phpt命令行运行这些测试,但我希望在PHPUnit中使用其他测试运行它们.

我知道PHPUnit_Extensions_PhptTestCase存在,但我找不到任何关于如何使用它的样本.

php phpunit phpt

13
推荐指数
1
解决办法
1838
查看次数

PHPUnit命令行工具不工作

我在Windows上运行XAMPP.

我最近从PEAR CLI工具下载了PHPUnit.

一切看起来很好,(eclipse正确建议代码完成,这意味着它被正确地添加到include_path),每当我尝试通过键入phpunit(它在环境PATH变量中)通过控制台工作时,它会挂起秒或两秒然后关闭(就像它被终止一样).

我也尝试过phpunit --help,但没有成功.

也试过了phpunit --verbose.

有没有其他人有同样的问题或知道解决方案?

php windows phpunit command-line-interface

13
推荐指数
1
解决办法
8187
查看次数

使用PHPUnit区分NULL和FALSE

有没有人知道用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)

php null phpunit unit-testing

13
推荐指数
2
解决办法
7726
查看次数

在phpunit测试中回显

我一直试图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)

这两个processIsolationverbose被接受,但debug并非如此.

当我直接将它传递给phpunit时,该命令实际上工作得很好:

phpunit --debug MyTest.php # here stuff is echoed correctly
Run Code Online (Sandbox Code Playgroud)

但是使用xml配置文件看起来它被忽略了.

php phpunit

13
推荐指数
1
解决办法
2万
查看次数

多次运行PHPUnit测试用例

我正在寻找一种如何使用不同的设置多次运行测试用例的方法.

我正在测试一个数据库访问类(几十种测试方法),并希望在"正常模式"下测试它,然后在"调试模式"下测试它.两种模式都必须产生相同的测试结果.

在测试用例设置中是否有可能这样做?或者重写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)

php phpunit testcase

13
推荐指数
2
解决办法
3184
查看次数

Laravel框架类在PHPUnit数据提供程序中不可用

我在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

如果我删除对 …

php phpunit automated-tests laravel

13
推荐指数
3
解决办法
3793
查看次数

如何PHPUnit测试一个没有返回值的方法?

我正在尝试从我编写的以下类中测试方法(函数比显示的更多,基本上,每个函数都是_*()方法):

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)

php phpunit unit-testing

13
推荐指数
3
解决办法
1万
查看次数

TDD - 无法模拟的依赖关系

假设我有一节课:

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)

php testing tdd phpunit unit-testing

13
推荐指数
1
解决办法
389
查看次数

Laravel 5.1 PHPUnit - press()给我'Unreachable field""'

当试图调用该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)

正如您所看到的,我的字段无法在表单之外.

php testing phpunit laravel laravel-5.1

13
推荐指数
2
解决办法
2596
查看次数