php artisan 命令在特定文件夹内创建测试文件

Kei*_*h M 6 phpunit laravel

目前我已经开始学习 Laravel 5.6 中的单元测试。默认情况下,我的 laravel 项目有一个“tests”目录,其中还有 2 个目录,即“Features”和“Unit”。每个目录都包含一个“ExampleTest.php”

./tests/Features/ExampleTest.php
./tests/Unit/ExampleTest.php
Run Code Online (Sandbox Code Playgroud)

每当我使用命令创建新的测试文件时

php artisan make:test BasicTest
Run Code Online (Sandbox Code Playgroud)

默认情况下,它总是在“Features”目录中创建测试文件,而我希望在“tests”目录下创建该文件。

是否有一个命令可以用来指定测试文件的创建路径。像这样的东西

php artisan make:test BasicTest --path="tests"
Run Code Online (Sandbox Code Playgroud)

我已经尝试过上面的路径命令,但它不是有效的命令。

我需要更改 phpunit.xml 文件中的一些代码吗?

Dav*_*vit 8

使用这个命令

php artisan make:test BasicTest --unit
Run Code Online (Sandbox Code Playgroud)

您也可以使用

php artisan make:test --help
Run Code Online (Sandbox Code Playgroud)

查看可用选项

您必须创建自定义 artiasn 命令

<?php

namespace App\Console;

class TestMakeCommand extends \Illuminate\Foundation\Console\TestMakeCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $signature = 'make:test-custom {name : The name of the class} {--unit : Create a unit test} {--path= : Create a test in path}';

    /**
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        $path = $this->option('path');
        if (!is_null($path)) {
            if ($path) {
                return $rootNamespace. '\\' . $path;
            }         

            return $rootNamespace;
        }

        if ($this->option('unit')) {
            return $rootNamespace.'\Unit';
        }

        return $rootNamespace.'\Feature';
    }
}
Run Code Online (Sandbox Code Playgroud)

在内核中注册

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        TestMakeCommand::class
    ];
    ......  
}
Run Code Online (Sandbox Code Playgroud)

然后你可以使用

php artisan make:test-custom BasicTest --path=
Run Code Online (Sandbox Code Playgroud)

或者

php artisan make:test-custom BasicTest --path=Example
Run Code Online (Sandbox Code Playgroud)


小智 8

php artisan make:test  Web/StatementPolicies/StatementPolicyListTest
Run Code Online (Sandbox Code Playgroud)

默认情况下,它将在 Tests/Feature/Web 下的 StatementPolicies 文件夹下创建一个名为 StatementPolicyListTest 的文件(如果不存在,它将创建一个同名的新文件夹)