phpunit单元测试问题

查看:150
本文介绍了phpunit单元测试问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

<?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']);
    }
}

写完测试代码之后,执行


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

没有执行到测试,直接把phpunit脚本输出了


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

另外,我把单元测试配置到系统环境变量里面,执行测试


phpunit app/tests/UserTest.php

需要在代码头部引入autoload.php


require_once 'vendor/autoload.php';

没法自动加载的吗

解决方案

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

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


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单元测试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆