【问题标题】:How to include file in phpunit test?如何在 phpunit 测试中包含文件?
【发布时间】:2016-09-21 23:24:02
【问题描述】:

我在 phpunit 测试中包含一个文件时遇到了一些问题。例如:当我在 PhpStorm 中执行以下代码时,我得到了预期的输出。

代码:

class NifvalidationTest extends PHPUnit_Framework_TestCase
{
    public function test_apiRequest()
    {
        $result = 1+1;
        $this->assertEquals(2, $result);
    }
}

输出:

Testing started at 16:58 ...
PHPUnit 5.2.12 by Sebastian Bergmann and contributors.



Time: 120 ms, Memory: 11.50Mb

OK (1 test, 1 assertion)

Process finished with exit code 0

但是当我需要使用包含访问另一个类的方法时,我没有得到预期的输出。举个例子,当我执行以下代码时:

class NifvalidationTest extends PHPUnit_Framework_TestCase
{
    public function test_apiRequest()
    {
        include('/../nifvalidation.php');
        $result = 1+1;
        $this->assertEquals(2, $result);
    }
}

我得到的是这个而不是预期的输出:

Testing started at 17:05 ...
PHPUnit 5.2.12 by Sebastian Bergmann and contributors.


Process finished with exit code 0

关于为什么包含会破坏测试的任何想法?

注意1:在上面的例子中我不需要包含文件,但我需要在另一个测试中。

注意2:文件'nifvalidation.php'的路径是正确的。

【问题讨论】:

  • 感谢@FelippeDuarte 的回答,但我无法理解该问题的最佳答案。
  • 您的问题似乎与您的 nifvalidation.php 文件有关。你能给我们看看代码吗?
  • @FelippeDuarte 你是对的。问题出在我的 nifvalidation.php 文件上,因为我使用不同的文件进行了测试并且没有问题。我的 nifvalidation.php 有一个扩展 prestashop 类的类: PS_VERSION')) { exit; } class Nifvalidation extends Module { //... } 问题在于类定义之前的 if 条件。但如果我没有这种情况,则找不到“模块”类。

标签: php testing include phpunit phpstorm


【解决方案1】:

我认为你的包含路径是错误的。 你的结构可能有点像这样
父目录
-> nifvalidation.php
-> 测试文件夹
-> NifvalidationTest.php

而不是

include('/../nifvalidation.php')

使用

include(dirname(__FILE__)."/../nifvalidation.php");

【讨论】:

  • 是的,结构是这样的。我尝试按照您的建议使用包含,但不幸的是也不起作用。
【解决方案2】:

使用require()require_once() 而不是include()

【讨论】:

    【解决方案3】:

    您可以在从命令行调用测试时使用bootstrap flag 来包含任何文件、定义常量以及加载所有变量、类等。

    --bootstrap <file>        A "bootstrap" PHP file that is run before the tests.
    

    例如,创建一个 autoload.php 定义常量并包含您的文件,然后您可以从命令行调用它:

    phpunit --bootstrap autoload.php testsFolder/NifvalidationTest
    

    对于更自动化的方法,您还可以create a phpunit.xml file 其中包含引导信息:

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit bootstrap="autoload.php">
        <testsuites>
            <testsuite name="Nifvalidation Test Suite">
                <directory>./testsFolder</directory>
            </testsuite>
        </testsuites>
    </phpunit>
    

    在这种特定情况下,根据您关于nifvalidation.php 内容的cmets,脚本退出,因为PS_VERSION 未定义。如果您正在执行仅需要定义一个虚拟常量的独立单元测试,那么您可以将其定义为存在于您的测试环境中。然后,您可以简单地引导您的 nifvalidation.php 文件。

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit bootstrap="../nifvalidation.php">
        <testsuites>
            <testsuite name="Nifvalidation Test Suite">
                <directory>./testsFolder</directory>
            </testsuite>
        </testsuites>
        <php>
            <const name="PS_VERSION" value="whatever you need here"/>
        </php>
    </phpunit>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-23
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 2014-08-21
      • 2016-12-19
      相关资源
      最近更新 更多