您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
如何在PHPstorm中配置使用单元测试phpunit
发布时间:2021-08-12 19:09:34编辑:雪饮阅读()
首先这里我的phpstorm版本信息如下:
PhpStorm 2019.1
Build #PS-191.6183.95, built on March 27, 2019
Licensed to wenyanjun
You have a perpetual fallback license for this version
Subscription is active until July 8, 2089
JRE: 1.8.0_202-release-1483-b39 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
然后在这里(file->settings>Languages & Frameworks > PHP>Test Frameworks)添加phpunit local配置在path top phpunit.phar选择你现在的phpunit单元测试框架的phar存档文件
对于php的版本要求不能太低,之前我配置的5.4就是有问题的,后来换了7.3.4nts
配置ok后,自己编写一个单元测试类,然后点击这里就可以运行测试类,这里新建一个测试类StackTest.php,所用代码还是上次咱们用的那个测试类的代码:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class StackTest extends TestCase
{
public function testPushAndPop(): void
{
$stack = [];
$this->assertSame(0, count($stack));
array_push($stack, 'foo');
$this->assertSame('foo', $stack[count($stack)-1]);
$this->assertSame(1, count($stack));
$this->assertSame('foo', array_pop($stack));
$this->assertSame(0, count($stack));
}
}
test framework quit unexpectedly
Testing started at 18:56 ...
D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar --no-configuration StackTest C:\Users\Administrator\PhpstormProjects\untitled\StackTest.php --teamcity
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
Cannot open file "StackTest".
Process finished with exit code 1
可以看到这里报错了,这个错误是因为它多加了一个类名,像我这里的类名就是StackTest
于是可以手动复制这个命令,然后修改后如:
D:\phpstudy_pro\Extensions\php\php7.3.4nts>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar --no-configuration C:\Users\Administrator\PhpstormProjects\untitled\StackTest.php --teamcity
然后再次运行于命令行中即可进行单元测试
关键字词:PHPstorm,单元测试,phpunit