标签: phpunit

PHPUnit测试的目录布局?

我是一名长期从事PHP项目的Java程序员,我正在努力让PHPUnit工作起来.在Java中进行单元测试时,通常将测试用例类和常规类放入不同的目录中,如下所示 -

/src  
  MyClass.java

/test  
  MyClassTest.java
Run Code Online (Sandbox Code Playgroud)

等等.

使用PHPUnit进行单元测试时,遵循相同的目录结构是否常见,或者是否有更好的方法来布置测试类?到目前为止,我能让"include("MyClass.php")"语句正常工作的唯一方法是将测试类包含在同一目录中,但是当我推送到时,我不想包含测试类生产.

php phpunit unit-testing

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

PHPUnit教程?或者更一般 - 单元测试教程值得推荐?

我正在寻找一个解释为什么以及如何编写有用的单元测试的教程.具体来说,我对PHPUnit很感兴趣,但是任何更通用的解释都可能是一个很好的解释.请注意,我不是在寻找有关如何使用PHPunit的技术信息 - 而是关于思考方式的教程!

php phpunit unit-testing

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

如何使用PHPUnit测试多个mysql模式?

我想测试一个使用PHPUnit的PHPUnit_Extensions_Database_TestCase类跨多个模式进行查询的进程.我已经挖掘了文档,SO和源代码,但似乎我必须使用以下内容设置我的数据库:

protected function getConnection()
{
    $this->pdo = new PDO('mysql:host=localhost;dbname=unit_test_schema', 'xxxx', 'yyyy');
    return $this->createDefaultDBConnection($this->pdo, 'unit_test_schema');    
}

protected function getDataSet()
{
    return $this->createXMLDataSet(DB_SETUP_DIR.'/schema.xml');
}
Run Code Online (Sandbox Code Playgroud)

有没有办法以某种方式使用多个模式,所以我可以测试一个查询,如:

SELECT *
FROM   schema1.tableA
JOIN   schema2.tableB
USING  (id)
Run Code Online (Sandbox Code Playgroud)

编辑:要清楚,我试图解决的问题是,我只能弄清楚如何将架构设置文件传递到一个数据库.我想找到一种方法来说"在schema1中创建tables1.xml,在schema2中创建tables2.xml".对于每个测试,将填充并杀死不同xml文件中引用的表.

php mysql phpunit unit-testing

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

使用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
查看次数

如何通过Jenkins运行phpunit测试时设置$ _SERVER ['']变量

我正在尝试为无法进行大量代码更改的应用程序编写单元测试.几乎所有代码库中的.php文件都使用了一些$ _SERVER ['']变量

require_once $_SERVER['DOCUMENT_ROOT'] . '/mainApi.php';
Run Code Online (Sandbox Code Playgroud)

所以现在当我必须编写并运行PHPUnit测试用例时,我必须以某种方式设置这些变量.目前我在用户环境中设置这些变量,然后进行

$_SERVER['DOCUMENT_ROOT'] = getenv('DOCUMENT_ROOT');
require_once $_SERVER['DOCUMENT_ROOT'] . '/mainApi.php';
Run Code Online (Sandbox Code Playgroud)

获取这样的服务器变量工作正常.我通过命令行运行我的测试$ phpunit test.php.

问题1:通过命令行运行phpunit测试时是否可以设置$ _SERVER变量?

我还必须通过Jenkins运行这些单元测试,我无法通过ANT/build文件设置这些服务器变量.

问题2:是否可以通过Jenkins中的ant构建文件设置这些变量,或者在通过Jenkins执行phpunit测试之前运行任何shell脚本?

我尝试通过shell脚本导出服务器变量

    export DOCUMENT_ROOT=/server/path-to-root-dir
Run Code Online (Sandbox Code Playgroud)

并在Jenkins的build.xml中调用该脚本

<export name="setEnv" description="set server var">
    <exec executable="sh">
       <arg value = "sumit.sh" />
    </exec> 
</target>
Run Code Online (Sandbox Code Playgroud)

但它不起作用.我可以为此做任何设置吗?谢谢!

php ant phpunit jenkins

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

PHPUnit代码覆盖率

我正在学习单元测试Zend Framework应用程序的绳索.到目前为止,我已经开始PHPUnit使用Zend Framework并开始编写一些简单的测试用例.

我的问题是,我想知道为什么Code Coverage不能在我的日志标记中设置为什么不起作用phpunit.xml.

我没有收到任何错误但没有生成覆盖率报告.

但是当我跑步时它会起作用 phpunit --coverage <dir>

我的phpunit的日志记录部分如下:

<phpunit bootstrap="./application/bootstrap.php" colors="true">
        <testsuite name="CI Test Suite">
            <directory>./</directory>
        </testsuite>
        <testsuite name="Library Test Suite">
            <directory>./library</directory>
        </testsuite>

        <filter>
            <whitelist>
                <directory suffix=".php">../application/</directory>
                <exclude>
                    <directory suffix=".phtml">../application</directory>
                    <file>../application/Bootstrap.php</file>
                    <file>../application/controllers/ErrorController.php</file>
                </exclude>
            </whitelist>
           <logging>
               <log type="coverage-html" target="./log/report" charset="UTF-8" yui="true"
   highlight="true" lowUpperBound="50" highLowerBound="80" />
               <log type="testdox" target="./log/testdox.html" />    
           </logging>
        </filter>
    </phpunit>
Run Code Online (Sandbox Code Playgroud)

有没有遇到过这个?什么是可能的问题?

php phpunit zend-framework code-coverage

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

强制从PHPUnit代码覆盖范围中排除文件

是否可以从PHPUnit的代码覆盖中强制排除文件夹?

我遇到的问题是,我有一个Symfony 1.4项目,其中有文件夹./lib/vendor/symfony/*.我想排除内部的任何东西./lib/vendor/*- 递归.

现在,我想排除它们是否被我的测试隐含地覆盖,即我永远不想看到这些文件夹.所以,我已将此位添加到我的phpunit.xml配置文件中,但无论我做什么,它似乎都不会排除这些文件夹:

<filter>
    <whitelist>
        <exclude>
            <directory>./lib/vendor/*</directory>
            <directory>./lib/vendor/symfony/lib/*</directory>
        </exclude>
    </whitelist>
</filter>
Run Code Online (Sandbox Code Playgroud)

在我看来,当代码被点击并且XDebug注意到它,PHPUnit将把它包含在代码覆盖中,无论如何.这方面的缺点是,这段代码已经由Symfony开发人员测试过,所以不需要将它包含在我的报告中,弄乱了我的数字:P

phpunit symfony1 code-coverage symfony-1.4

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

PHPUnit每次调用mocked方法时返回值都不同

例如,我有一个像下面这样的模拟类:

$mock= $this->getMockBuilder("SomeClass")->disableOriginalConstructor()->getMock();

$mock->expects($this->any())
     ->method("someMethod")
     ->will($this->returnValue("RETURN VALUE"));
Run Code Online (Sandbox Code Playgroud)

唯一的参数someMethod是阵列$arr.

我想要做的是第一次调用$arr[0]时返回someMethod,$arr[1]第二次调用,依此类推.

大小$arr是动态的.

如果可能的话,任何想法如何实现?

php phpunit mocking

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

如何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万
查看次数

Laravel Eloquent,声称该集合包含项目

如何断言(在PHPUnit测试中)Eloquent集合包含一个项目?

像这样的东西:

$expected = factory::create(Item::class)->create();
$eloquentCollection = someData(); // Item::orderBy(...)->...->get();
$this->assertContains($expected, $eloquentCollection);
Run Code Online (Sandbox Code Playgroud)

php phpunit eloquent laravel-5

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