【问题标题】:How to compare 2 URL adres with PHPUnit?如何将 2 个 URL 地址与 PHPUnit 进行比较?
【发布时间】:2020-07-23 15:19:19
【问题描述】:

我正在处理一项任务,即公开具有给定 url 的 xml 文件。我想测试我的代码,我用 PHPUnit 来做这件事。我使用 composer 安装 PHPUnit。

我要运行的示例测试是这样的:

<?php
    use PHPUnit\Framework\TestCase;

    class IndexTest extends TestCase{

    public function testGetXmlWithUrl(){
    require 'index.php';

    $XmlClass = new XmlReaderClass("localhost", "8000", "status.xml", "");
    $url= $XmlClass->$url;
    $myUrl = "http://localhost:8000/status.xml?password=";
    
    $this->assertEquals($myUrl, $url);

}}  ?>

无法添加TestCase 类。我的意思是即使 PhpUnit 名称在代码中也不是蓝色的。所以我想我实际上无法将它包含在我的项目中。错误是这样的:

致命错误:未捕获的错误:在 /Users/demetsen/Desktop/tests/IndexTest.php:4 中找不到类“PHPUnit\Framework\TestCase”

我的第二个问题是当我尝试运行 testGetXmlWithUrl() 方法时,结果是:

未能断言 SimpleXMLElement 对象 (...) 与预期的 '\n ....

匹配

为什么我会得到这个结果,我该如何解决?

【问题讨论】:

  • 你好 Demet,你能告诉我们你是如何运行测试的吗?
  • 嗨,我是这样运行的:phpunit tests/IndexTest.php
  • 您从哪里调用 phpunit,该目录中是否有 phpunit.xml 文件?如果有,可以在这里分享一下吗?
  • 我的项目中没有这样的 phpunit.xml 文件。我应该添加它。如果是这样,我该怎么做?谢谢。
  • 你不需要 phpunit.xml。您是否从测试目录所在的目录运行此命令,而不是从测试目录?运行cat tests/IndexTest.php 而不是phpunit tests/IndexTest.php,你应该会看到这个测试文件的内容。如果你不路径是错误的。没有更多的步骤可以工作:phpunit.de/getting-started-with-phpunit.html

标签: php url phpunit


【解决方案1】:

一个默认的phpunit.xml(或者更好的是,使用phpunit.xml.dist,所以如果需要你可以在本地复制/编辑它),可以用phpunit --generate-configuration创建,将包含bootstrap的一行:

<phpunit
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
     bootstrap="path/to/bootstrap.php"
     <!-- more lines -->
</phpunit>

这里,bootstrap="...." 行可以最简单地指向作曲家自动加载器。 PHPunit 也可以为你创建一个合适的文件。

$ php ./phpunit-9.2.phar --generate-configuration  # or vendor/bin/phpunit, via composer
PHPUnit 9.2.6 by Sebastian Bergmann and contributors.

Generating phpunit.xml in /home/username/code/test

Bootstrap script (relative to path shown above; default: vendor/autoload.php):
Tests directory (relative to path shown above; default: tests):
Source directory (relative to path shown above; default: src):

Generated phpunit.xml in /home/username/code/test

# optional, but useful:
# mv phpunit.xml phpunit.xml.dist  # .dist will be read if .xml does not exist

输出文件:

$ cat phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.2/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         executionOrder="depends,defects"
         forceCoversAnnotation="true"
         beStrictAboutCoversAnnotation="true"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutTodoAnnotatedTests="true"
         verbose="true">
    <testsuites>
        <testsuite name="default">
            <directory suffix="Test.php">tests</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">src</directory>
        </whitelist>
    </filter>
</phpunit>

如果您需要将某些内容添加到引导过程中,请根据您的需要创建一个文件,并且您可能还会添加类似 require __DIR__.'/vendor/autoload.php'; 的内容来设置自动加载。

【讨论】:

  • 答案末尾的 require 语句缺少目录分隔符。应该是:require __DIR__.'/vendor/autoload.php';
猜你喜欢
  • 1970-01-01
  • 2012-02-08
  • 2012-05-06
  • 2016-07-27
  • 1970-01-01
  • 2011-06-26
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
相关资源
最近更新 更多