phpunit unit testing problem
代言
代言 2017-06-26 10:49:13
0
2
1051
<?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']);
    }
}

After writing the test code, execute


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

The test was not executed and the phpunit script was output directly


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" "$@"

In addition, I configured the unit test into the system environment variable and executed the test


phpunit app/tests/UserTest.php

Need to introduce autoload.php

at the head of the code

require_once 'vendor/autoload.php';

Can’t it be loaded automatically?

代言
代言

reply all(2)
大家讲道理

Solved, I found the solution myself after being prompted by the answers given by the two previous friends

Many of the tutorials found online are executed using commands


php vendor/bin/phpunit tests.php

It may be due to version reasons. The earlier version was a php script file. My version is "phpunit/phpunit": "^6.2", vendor/bin/phpunit is a shell script file, (I have not used 5. I don’t know the specifics of x either).

The correct way to use it is to give the script file executable permission


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

Execute tests


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

The automatic loading method is implemented. Using the phpunit component package loaded by composer, there is a phpunit.xml in the root directory of the project, where you can set the automatic loading path

<?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>

世界只因有你

You can write a unit test file outside the framework and use phpunit xxx.php to test it. This way you will know whether phpunit is installed normally and whether the way to introduce autoload.php is wrong. Check them one by one.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!