gpt4 book ai didi

php - phpunit 目录错了?

转载 作者:行者123 更新时间:2023-12-04 10:56:35 25 4
gpt4 key购买 nike

我想为 phpunit 创建一个单独的文件夹。我想为所有与单元测试相关的文件夹创建一个文件夹。 unit_test 是名称。但我收到一条消息:

PS C:\Users\dh_netsense.no\utv\NSS-Hyttetjenester> unit_test/vendor/bin/phpunit
PHPUnit 8.4.3 by Sebastian Bergmann and contributors.

Usage:
phpunit [options] UnitTest [UnitTest.php]
phpunit [options] <directory>

我如何配置 phpunit 以便我可以在此路径中运行它?我现在没有看到正在运行的测试。

最佳答案

创建一个放在 unit_test 文件夹根目录中的 phpunit.xml。
unit_test/phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Daniels test suite">
<directory>./</directory>
</testsuite>
</testsuites>
<php>
<env name="DB_CONNECTION" value="testing" force="true"/>
<env name="APP_ENV" value="testing" force="true"/>
<env name="CACHE_DRIVER" value="array" force="true"/>
<env name="SESSION_DRIVER" value="array" force="true"/>

</php>
</phpunit>

这个文件会告诉 phpunit 在哪里寻找什么文件,设置什么环境变量等等......你也可以通过命令行标志来完成大部分工作,但是有一个文件来保存设置会让生活变得更容易。

然后在您的 unit_test文件夹,您可以放置​​测试文件,如果您愿意,甚至可以放在子目录中。只需遵循命名文件和测试函数的约定即可。

包含测试的文件应该命名为:DanielsFirstApplication 测试 .php

文件中的测试函数应以小写的 test 开头。确保为您的测试函数提供描述性名称。

public function testDanielsApplicationInitialization() {
}



在你的 WhatTest.php 文件中,你也可以有一个名为 setUp() 的函数。和 tearDown() .这些将在每个 testYourNameHere() 之前调用函数,这样你就可以在那里初始化你需要的文件,创建模型、需要的文件、数据库记录等......并在拆卸中清理它们。

如果你想为不同的测试做不同的准备 Action ,你可以在 setUp 和 tearDown 中使用这样的 if 语句
if($this->getName() == 'testDanielsApplicationInitialization') {
// test thing.
}

在 bootstrap.php 中,加载要测试的框架/应用程序初始化所需的所有文件。例如对于 octobercms 我使用这样的东西:
unit_test/bootstrap.php
date_default_timezone_set('Europe/Amsterdam');
$autoloader_exitx = require __DIR__.'/../include/vendor/autoload.php';
require_once(__DIR__.'../../vendor/autoload.php');
require __DIR__.'../../bootstrap/autoload.php';
$app = require_once $OCTOBER_PATH.'/bootstrap/app.php';
$kernel = $app->make(\Illuminate\Contracts\Http\Kernel::class);
$request = Illuminate\Http\Request::capture();
$app->instance('request', $request);
$kernel->bootstrap();

或者你的框架用来加载的任何东西。

如果您需要任何建议,请不要为远离主项目的单元测试创​​建单独的目录。

只需通过 composer require --dev phpunit/phpunit 包含 php 单元这样它只会安装在开发机器上。然后,您可以在根应用程序目录中创建 php 单元,并创建一个名为 tests 的文件夹,并将您的测试套件设置为测试文件夹:
<testsuites>
<testsuite name="Daniels test suite">
<directory>./tests</directory>
</testsuite>
</testsuites>

通过这种方式,您的项目始终与您的测试相关联,并且它们仍位于自己的文件夹中。

关于php - phpunit 目录错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59136291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com