Probleme beim Unit-Testen von phpunit
代言
代言 2017-06-26 10:49:13
0
2
1024
<?php 
namespace app\tests;

use PHPUnit\Framework\TestCase;
use GuzzleHttp\Client;

class UserTest extends TestCase
{
    private $client;

    public function setUp()
    {
        $this->client = new \GuzzleHttp\Client( [
            'base_uri' => 'http://z.slim.com',
            'http_errors' => false, 
        ]);
    }


    public function testUser()
    {
        $response = $this->client->get('/user/vilay');
        $this->assertEquals(200, $response->getStatusCode());
        $body = $response->getBody();
        $data = json_decode($body, true);
        $this->assertArrayHasKey('code', $data);
        $this->assertArrayHasKey('msg', $data);
        $this->assertArrayHasKey('data', $data);
        $this->assertEquals(0, $data['code']);
    }
}

Nachdem Sie den Testcode geschrieben haben, führen Sie ihn aus


php vendor/bin/phpunit app/tests/UserTest.php

Der Test wurde nicht ausgeführt und das PHPUnit-Skript wurde direkt ausgegeben


dir=$(d=${0%[/\]*}; cd "$d"; cd "../phpunit/phpunit" && pwd)

# See if we are running in Cygwin by checking for cygpath program
if command -v 'cygpath' >/dev/null 2>&1; then
    # Cygwin paths start with /cygdrive/ which will break windows PHP,
    # so we need to translate the dir path to windows format. However
    # we could be using cygwin PHP which does not require this, so we
    # test if the path to PHP starts with /cygdrive/ rather than /usr/bin
    if [[ $(which php) == /cygdrive/* ]]; then
        dir=$(cygpath -m "$dir");
    fi
fi

dir=$(echo $dir | sed 's/ /\ /g')
"${dir}/phpunit" "$@"

Darüber hinaus habe ich den Unit-Test in der Systemumgebungsvariablen konfiguriert und den Test ausgeführt


phpunit app/tests/UserTest.php

Autoload.php muss am Kopf des Codes eingefügt werden


require_once 'vendor/autoload.php';

Ist ein automatisches Laden nicht möglich

代言
代言

Antworte allen(2)
大家讲道理

已解决,在前面两位朋友的答案提示下,自己找到了解决方法

网上找些教程很多都是用命令执行


php vendor/bin/phpunit tests.php

可能是由于版本原因,早期的版本是php脚本文件,我的版本是"phpunit/phpunit": "^6.2"vendor/bin/phpunit是个shell脚本文件,(没有用过5.x的具体我也不知道)。

正确使用方式是,给脚本文件可执行权限


chmod a+x vendor/bin/phpunit
chmod a+x vendor/phpunit/phpunit/phpunit

执行测试


sh vendor/bin/phpunit app/tests/UserTest.php

自动加载方式实现了,使用composer加载的phpunit组件包,在项目的根目录中有个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/5.7/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         backupGlobals="false"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutTestsThatDoNotTestAnything="true"
         beStrictAboutTodoAnnotatedTests="true"
         verbose="true">
    <testsuite>
        <directory suffix="Test.php">tests</directory>
    </testsuite>

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

    <logging>
        <log type="coverage-html" target="build/coverage/html" title="phpDox"
             charset="UTF-8" highlight="true" lowUpperBound="60" highLowerBound="90"/>
    </logging>

</phpunit>

世界只因有你

你可以在框架外,随便写一个单元测试文件,使用phpunit xxx.php 测试一下,这样就知道是否phpunit安装正常,是否引入autoload.php方式错了,逐一排查。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!