我正在使用 Laravel 8 并测试我的应用程序,我正在运行
php artisan test --coverage-html reports
Run Code Online (Sandbox Code Playgroud)
测试运行成功。问题是没有生成覆盖率报告。我已经在这里查看了答案,据说解决方案将以下内容添加到我的phpunit.xml
<logging>
<log type="coverage-html" target="tests/coverage" showUncoveredFiles="true"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
Run Code Online (Sandbox Code Playgroud)
那是行不通的。我也尝试过
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
<report>
<html outputDirectory="tests/reports/coverage"/>
</report>
</coverage>
Run Code Online (Sandbox Code Playgroud)
依然没有; 下面是我的phpunit.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
<directory suffix="Test.php">./packages/okotieno</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="tests/coverage" showUncoveredFiles="true"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<php> …Run Code Online (Sandbox Code Playgroud) 我正在为 Laravel Artisan 控制台命令编写一个非常基本的测试,如下所示:
$this->artisan("my-command", ["--some-option" => "some-value"])
->expectsOutput("the expected output");
Run Code Online (Sandbox Code Playgroud)
测试没有通过。我真的很麻烦,因为“预期输出”正是手动执行时命令输出的内容。
但没关系,我只需要检查通过自动化测试执行时命令输出的实际情况,对吧?但是等等,我该怎么做呢?
我尝试了以下方法:
$output = new BufferedConsoleOutput();
Artisan::call("my-command", ["--some-option", "some-value"], $output);
// dd($output->fetch()
$this->assertTrue($output->fetch() === "the expected output");
Run Code Online (Sandbox Code Playgroud)
但 $output->fetch() 似乎总是空的。
简而言之:如何在测试上下文中打印 Laravel 命令的实际输出?
我有以下测试:
public function testStoreMemberValidation()
{
$response = $this->withExceptionHandling()->post('/api/members', [
"name" => "Eve",
"age" => "invalid"
], ['X-Requested-With' => 'XMLHttpRequest']);
dd($response->json());
};
Run Code Online (Sandbox Code Playgroud)
我试图断言响应是验证错误的形式。控制器方法如下:
public function store(Request $request)
{
$data = $request->validate([
'name' => 'required|string',
'age' => 'required|integer',
]);
Member::create($data);
}
Run Code Online (Sandbox Code Playgroud)
但是,每当我调用任何调用$response->json()(大多数断言)的断言时,都会出现异常:
Illuminate\Validation\ValidationException :给定的数据无效。
如何在不抛出此错误的情况下对此响应执行断言?
请注意,我使用的是 Laravel 5.7。
我正在尝试为我的laravel 5.2项目创建一个测试,该测试将在rest资源控制器中测试注册页面。当我用测试的确切输入手动测试它时,一切正常,但是当我使用它进行测试时,phpunit我被重定向到错误页面,使我的页面断言失败。但是,该错误显示断言失败的错误,而不是错误页面上的内容,因此不显示测试失败的原因。我怎么看?
测试用例:
class registerTest extends TestCase
{
use WithoutMiddleware;
public function testRegisterCompanyCorrectly()
{
$this->actAsAdmin();
$this->visit('/Companies/create')
->type('testCompany', 'CName')
->type('www.testCompany.com', 'CUrl')
->type('testManager@gmail.com', 'Mail')
->type('John Doe', 'Name')
->type('Keith Richards', 'Password')
->type('Keith Richards', 'CPassword')
->press('Registreren')
->seePageIs('/Companies/3')
;
}
private function actAsAdmin()
{
$user = User::find(1);
$this->actingAs($user);
}
}'
Run Code Online (Sandbox Code Playgroud)
phpunit 错误:
There was 1 failure:
1) registerTest::testRegisterCompanyCorrectly
Did not land on expected page [http://localhost/Companies/3].
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/Companies/3'
+'http://localhost/Companies'
C:\Users\Stefan\Documents\producten\Applicatie\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:170 …Run Code Online (Sandbox Code Playgroud) 我正在测试控制器方法,并且正在测试中访问路由。
然后我想确保在视图中返回正确的模型并加载所有正确的关系。
我知道我可以这样做:
$this->assertViewHas("content");
Run Code Online (Sandbox Code Playgroud)
但是我如何验证返回到视图中的内容模型是否具有正确的(例如类别)?即我如何获取内容模型对象,然后执行类似的操作
$this->assertEquals($content->category->name, "category 1");
Run Code Online (Sandbox Code Playgroud)
?
我正在将旧的Laravel个人项目从5.2升级到5.4.升级到5.3似乎已经没问题,但现在我已经转向5.4我遇到了一个问题.
该项目使用了旧的测试层,因此我安装了BrowserKit测试包以保持向后兼容性.我还为Browserkit测试创建了以下基本测试用例:
<?php
namespace Tests;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
abstract class BrowserKitTestCase extends BaseTestCase
{
use CreatesApplication;
public $baseUrl = 'http://localhost';
}
Run Code Online (Sandbox Code Playgroud)
使用正常测试用例的模型测试工作正常,但是当我运行任何使用BrowserKit测试用例的测试时,我看到以下错误消息:
PHP Fatal error: Class 'PHPUnit\Framework\Constraint\Constraint' not found in /home/matthew/Projects/myproject/vendor/laravel/browser-kit-testing/src/Constraints/PageConstraint.php on line 10
PHP Fatal error: Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /home/matthew/Projects/myproject/vendor/laravel/framework/src/Illuminate/Container/Container.php:895
Stack trace:
#0 /home/matthew/Projects/myproject/vendor/laravel/framework/src/Illuminate/Container/Container.php(735): Illuminate\Container\Container->notInstantiable('Illuminate\\Cont...')
#1 /home/matthew/Projects/myproject/vendor/laravel/framework/src/Illuminate/Container/Container.php(608): Illuminate\Container\Container->build('Illuminate\\Cont...')
#2 /home/matthew/Projects/myproject/vendor/laravel/framework/src/Illuminate/Container/Container.php(575): Illuminate\Container\Container->resolve('Illuminate\\Cont...')
#3 /home/matthew/Projects/myproject/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(728): Illuminate\Container\Container->make('Illuminate\\Cont...')
#4 /home/matthew/Projects/myproject/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExce in /home/matthew/Projects/myproject/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 895
Run Code Online (Sandbox Code Playgroud)
Google对此并没有多大用处,而且错误信息也不是很明显.它似乎与命名空间有关,因为该类PHPUnit\Framework\Constraint\Constraint似乎不存在,但我不确定如何解决该问题.我已根据需要将PHPUnit的版本升级到5.7,但这并不能解决问题.任何人都可以建议问题可能是什么?
编辑:只是想尝试将版本降级到1.0,这似乎解决了现在的问题,所以也许2.0版本可以与PHPUnit 6一起使用?不过,希望这篇文章将来会帮助某人.
根据Laravel 文档,我可以使用Queue::fake();防止作业排队。
不清楚如何在未排队的情况下测试 (PHPUnit) 作业类中的一些方法。
例如:
class ActionJob extends Job
{
public $tries = 3;
protected $data;
public function __construct($data)
{
$this->data = $data;
}
public function handle()
{
if ($this->data['action'] == "deleteAllFiles") {
$this->deleteAllFiles();
}
}
protected function deleteAllFiles()
{
//delete all the files then return true
// if failed to delete return false
}
}
Run Code Online (Sandbox Code Playgroud)
这是我要测试的示例deleteAllFiles()- 我需要模拟它吗?
$this->assertDatabaseHas()不使用JSON/JSONb列。
那么如何在 Laravel 中测试这些类型的列呢?
目前,我有一个商店行动。如何执行断言,保存具有预定义值的特定列。
就像是
['options->language', 'en']
不是一个选项,因为我有一个包含元内容的大量 JSON 。
如何JSON立即检查数据库中的内容?
我在我的项目中使用 spatie/laravel-sluggable 包。我需要断言已调度事件。Event::fake([AdvertPublished::class])问题是当我在测试中伪造事件 ( ) 时,理论上只有我的 Advertpublished 事件应该被伪造,但似乎 pacakge 事件也被伪造,并且创建 slug 的方法generateSlugOnCreate永远不会执行,这会导致 sql 错误,因为我的 slug 字段不能为空。
Event::fake([AdvertPublished::class]);
$response = $this->json('POST', '/company/adverts', ['foo' => 'bar'])
->assertStatus(201);
$advert = Advert::first();
Event::assertDispatched(AdvertPublished::class, function ($e) use ($advert) {
return $e->advert->id === $advert->id;
});
Run Code Online (Sandbox Code Playgroud)
有人知道我是否遗漏了什么或者可能是什么问题?
运行所有测试
php artisan test
一切都按预期运行并且所有测试都运行
现在,当我运行单一测试时,php artisan test --filter test_get_profile我收到此有线错误
An error occurred inside PHPUnit.
Message: Cannot find TestCase object on call stack
Location: D:\laragon\www\project\vendor\phpunit\phpunit\src\TextUI\TestRunner.php:68
Run Code Online (Sandbox Code Playgroud)
但其他一些测试仍然有效,例如 test_login 和 test_register 有效,但是当我创建新测试时,有时它有效,有时我会收到此有线错误
PS:我添加了文件路径示例,php artisan test tests/Feature/AccountTest.php --filter test_get_profile我没有收到任何错误,但我不知道要始终包含文件路径
请注意,所有测试都是空的
public function test_get_profile(): void
{
$response = $this->get('/');
$response->assertStatus(200);
}
Run Code Online (Sandbox Code Playgroud)
有人知道这个问题吗?我正在使用 laravel 10 和 phpunit 10
phpunit.xml:
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory …Run Code Online (Sandbox Code Playgroud) laravel ×10
laravel-testing ×10
php ×6
phpunit ×4
eloquent ×1
jsonb ×1
laravel-5.2 ×1
laravel-5.4 ×1
laravel-5.6 ×1
mockery ×1
postgresql ×1
unit-testing ×1