我正在使用 PHPUnit 来测试具有许多功能的类。PHPUnit 框架从上到下运行测试函数。
问题是:如何在不重新排序的情况下按指定的顺序运行测试函数,然后在源代码中。
为了澄清这个问题,假设我们有 5 个测试函数;
框架将运行 testFunc1 然后 testFunc2 直到它到达 testFunc5。
但是,我想运行 testFunc3 然后 testFunc1 然后 testFunc5 然后 testFunc2 然后 testFunc4 而不在源文件中重新排序它们。
我有一个关于 Selenium V2 (Webdriver) 和 phpUnit 的问题,那么使用它的捆绑包之间有什么最好的:
我想知道你对在我用 symfony 2.1 开发的项目中使用它的好方法的看法吗?
使用 zend 和 phpunit 进行测试时。控制台上显示标题错误。我发现错误:
Cannot modify header information - headers already sent by (output started at /usr/share/php/PHPUnit/Util/Printer.php:173)
Run Code Online (Sandbox Code Playgroud)
我尝试使用某些主题中的说明进行调试 有 没有办法在 PHPUnit 中测试 STDERR 输出? 和 PHPUnit 输出导致 Zend_Session 异常。但是当使用 --stderr 选项时,我找不到一些测试用例的输出和报告。这是控制台上的输出:
root@ubuntu:/home/boingonline/www/testunit# phpunit --stderr
PHPUnit 3.5.15 by Sebastian Bergmann.
root@ubuntu:/home/boingonline/www/testunit#
Run Code Online (Sandbox Code Playgroud)
对这个问题有什么想法吗?感谢所有的答案。
有没有办法为多个匹配断言模拟类方法字符串参数?
$this->getMock()
->expects($this->any())
->method('setString')
->with($this->stringContains('word3'))
->will($this->returnSelf());
Run Code Online (Sandbox Code Playgroud)
这个例子将通过 for ->setString('word1 word2 word3 word4')
如果 setString() 使用包含“word1”和“word3”的参数调用,我需要做的是匹配
但
$this->getMock()
->expects($this->any())
->method('setString')
->with(
$this->stringContains('word1'),
$this->stringContains('word3')
)
->will($this->returnSelf());
Run Code Online (Sandbox Code Playgroud)
此实现正在检查 setString() 的 2 个参数,这不是我打算检查的。
想法?使用 $this->callback() ?是否有一些更适合的 PHPUnit 断言?
我正在编写很多单元测试,恐怕有一天我会回来阅读测试代码,而无法理解所测试的内容。
问题是:如何使用PHPDoc记录PHPUnit测试?
当工作完成时,我试图抓住一个事件
测试代码:
class MyTest extends TestCase {
public function testJobsEvents ()
{
Queue::after(function (JobProcessed $event) {
// if ( $job is 'MyJob1' ) then do test
dump($event->job->payload());
$event->job->payload()
});
$response = $this->post('/api/user', [ 'test' => 'data' ], $this->headers);
$response->assertSuccessful($response->isOk());
}
}
Run Code Online (Sandbox Code Playgroud)
UserController中的方法:
public function userAction (Request $request) {
MyJob1::dispatch($request->toArray());
MyJob2::dispatch($request->toArray());
return response(null, 200);
}
Run Code Online (Sandbox Code Playgroud)
我的工作:
class Job1 implements ShouldQueue {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $data = [];
public function __construct($data)
{
$this->data= $data;
}
public function handle() …Run Code Online (Sandbox Code Playgroud) 我使用laravel创建PHPunit测试。对于每个测试,我都需要“重置”数据库,但是删除所有表并设置架构非常慢(在mysql上)-因此,我不想更改架构,而只删除DATA并重置每个表的AUTO-INCREMENT计数器。
我如何使用播种机做到这一点?
的PHP:7.2.7
SYMFONY:3.4.12
PHPUNIT-BRIDGE:4.1.1(phpunit 6.5.8)
我有一个用PHP编写的部署脚本,该脚本以root用户身份运行,并且由于从7.0升级到php 7.2并相应地更新了我的代码,因此在运行phpunit测试时,该脚本已开始莫名其妙地失败。我确定我在这里有错,但是经过许多小时的调试,我仍然一无所获,希望有人可以为我指明正确的方向。
该脚本以root身份运行,并在执行以下命令时运行:
exec("runuser MY-USER -c 'bin/simple-phpunit'", $output, $returnCode);
Run Code Online (Sandbox Code Playgroud)
我所有的测试都失败了:
You have requested a non-existent service "test.client"
Run Code Online (Sandbox Code Playgroud)
关于此错误有很多SO问题,并且与phpunit环境未设置为“测试”有关。我的phpunit.xml使用正确的值:
<env name="APP_ENV" value="test" />
Run Code Online (Sandbox Code Playgroud)
疯狂的是,当我自己(以MY-USER)运行时,单元测试运行得很好。当我以root身份登录并执行时,它们甚至可以正常运行
runuser MY-USER -c 'bin/simple-phpunit'
Run Code Online (Sandbox Code Playgroud)
我做的时候他们甚至跑
$ sudo su
$ php -a
php > exec("runuser MY-USER -c 'bin/simple-phpunit'", $output, $returnCode);
Run Code Online (Sandbox Code Playgroud)
当我感到绝望并开始将变量转储到控制台时,似乎一切正常:
echo "ENV = " .getenv('APP_ENV'); // [OUTPUT BELOW]
Run Code Online (Sandbox Code Playgroud)
ENV = dev
Run Code Online (Sandbox Code Playgroud)
exec('bin/console debug:container test.client -e test', $output, $return);
var_dump(implode("\n", $output)); // [OUTPUT BELOW]
Run Code Online (Sandbox Code Playgroud)
Information for Service "test.client" …Run Code Online (Sandbox Code Playgroud) 我正在写一个Laravel包,但我遇到了问题.该程序包调度执行以下操作的作业:
class ExampleJob
{
protected $exampleProperty;
function __construct($parameter)
{
$this->exampleProperty = $parameter;
}
}
Run Code Online (Sandbox Code Playgroud)
我需要测试是否使用正确的$参数调度此作业(此值是从数据库中检索的,并且根据情况,它将是不同的值).
根据文档,Laravel允许这样做:
Bus::assertDispatched(ShipOrder::class, function ($job) use ($order) {
return $job->order->id === $order->id;
});
Run Code Online (Sandbox Code Playgroud)
但这意味着$ order属性需要公开(在我的情况下,我有:protected $ exampleProperty;).
这是一个好习惯吗?我的意思是将类属性声明为公共属性?在OOP中封装的概念怎么样?
有什么想法吗?
我有以下课程
namespace MyApp;
use MyApp\SomeInterface;
class MyClass
{
public function __construct(SomeInterface $s)
{
//Some Logic here
}
//Another methods implemented There
}
Run Code Online (Sandbox Code Playgroud)
SomeInterface包含以下内容:
namespace MyApp
interface SomeInterface
{
/**
* @return SomeObject
*/
public function someMethodToImpement();
}
Run Code Online (Sandbox Code Playgroud)
我想在我的phpunit测试类上创建一个模拟:
namespace Tests\MyApp;
use PHPUnit\Framework\TestCase;
use MyApp\MyClass;
use MyApp\SomeInterface;
class MyClassTest extends TestCase
{
public function someTest()
{
$fakeClass=new class{
public function myFunction($arg1,$arg2)
{
//Dummy logic to test if called
return $arg1+$arg2;
}
};
$mockInterface=$this->createMock(SomeInterface::class)
->method('someMethodToImpement')
->will($this->returnValue($fakeClass));
$myActualObject=new MyClass($mockInterface);
}
}
Run Code Online (Sandbox Code Playgroud)
但是一旦我运行它,我得到错误:
Tests …
phpunit ×10
php ×7
unit-testing ×4
laravel ×3
symfony ×2
mocking ×1
php-7.2 ×1
phpdoc ×1
symfony-2.1 ×1
symfony-flex ×1
testing ×1
truncate ×1