치명적인 오류 인 이유 : 클래스 'PHPUnit_Framework_TestCase'를…에서 찾을 수 없습니까?
이 PHP 오류가 발생하는 이유는 무엇입니까?
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...
phpunit을 문서를 말한다 phpunit을 / Framework.php를 포함 / 요구하는 말을 사용하는 등 다음과 같습니다 :
require_once ('PHPUnit/Framework/TestCase.php');
최신 정보
PHPUnit 3.5부터는이를 처리 할 수있는 내장 오토로더 클래스가 있습니다 :
require_once 'PHPUnit/Autoload.php';
이것을 지적 해준 Phoenix에게 감사합니다!
phpunit을 2017-02-03에 릴리스 된 버전 6 이상으로 업데이트 한 후 여기에 도착하는 사람들 (예 : 작곡가가있는)의 경우 phpunit 코드가 네임 스페이스이기 때문에이 오류가 발생할 수 있습니다 ( changelog 확인 ).
다음과 같은 것들을 리팩토링해야 \PHPUnit_Framework_TestCase
합니다.\PHPUnit\Framework\TestCase
에 대한 높은 버전 과 같은 phpunit을의 6.4 네임 스페이스를 사용해야합니다 phpunit을 \ 프레임 워크 \의 TestCase를
사용 의 TestCase 대신 PHPUnit_Framework_TestCase
// use the following namespace
use PHPUnit\Framework\TestCase;
// extend using TestCase instead PHPUnit_Framework_TestCase
class SampleTest extends TestCase {
}
파일 이름을 지정했기 때문에이 오류가 발생할 수 있습니다. 그렇다면 PHPUnit_Framework_TestCase가 백 슬래시로 시작하여 글로벌 네임 스페이스에 있도록 지정해야합니다.
namespace AcmeInc\MyApplication\Tests
class StackTest extends \PHPUnit_Framework_TestCase {}
나는 문서 를 수정하기 위해 대화를 시작하기 위해 원유 PR 을 제출 했습니다 .
PHP5에서 PHPUnit 테스트를 실행 한 후 PHP7도 지원해야했습니다. 이것이 내가 한 일입니다.
composer.json에서 :
"phpunit/phpunit": "~4.8|~5.7"
내 PHPUnit 부트 스트랩 파일 (내 경우에는 /tests/bootstrap.php
) :
// PHPUnit 6 introduced a breaking change that
// removed PHPUnit_Framework_TestCase as a base class,
// and replaced it with \PHPUnit\Framework\TestCase
if (!class_exists('\PHPUnit_Framework_TestCase') && class_exists('\PHPUnit\Framework\TestCase'))
class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
다시 말해, 이것은 원래 PHPUnit 4 또는 5 용으로 작성된 테스트에 적용되지만 PHPUnit 6에서도 작동해야합니다.
PHPUnit을 설치하여 명령을 실행할 수 있습니다 ( https://github.com/sebastianbergmann/phpunit/#php-archive-phar ).
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit
단일 테스트 실행
그런 다음 PHPunit test를 실행하십시오.
phpunit test.php
테스트 파일의 내용은 다음과 같습니다.
<?php
class StackTest extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
}
public function testSave()
{
}
}
테스트 스위트 실행
테스트 스위트 구성 : demosuite.xml. demo
모든 테스트가 포함 된 디렉토리입니다. 테스트 파일 이름은 *_test.php
( suffix
) 로 지정해야합니다 .
<testsuites>
<testsuite name="DemoTestSuite">
<directory suffix="test.php">demo</directory>
</testsuite>
</testsuites>
테스트 스위트는 다음 명령으로 실행됩니다.
phpunit -c demosuite.xml --testsuite DemoTestSuite
인수:
Phpunit (3.7) 은 콘솔 환경에서 사용할 수 있습니다.
동작:
콘솔에 다음 명령을 입력하십시오.
SHELL> phpunit "{{PATH TO THE FILE}}"
코멘트:
콘솔에서 실행하지 않으려면 새 버전의 PHPUnit에 아무것도 포함하지 않아도됩니다. 예를 들어 브라우저에서 테스트를 실행합니다.
If you have Centos or other Linux distribution you have to install phpunit package, I did that with yum install phpunit and it worked. Maybe you can have to add a repository, but I think it has to work smooth with the default ones (I have CentOS 7)
It may well be that you're running WordPress core tests, and have recently upgraded your PhpUnit to version 6. If that's the case, then the recent change to namespacing in PhpUnit will have broken your code.
Fortunately, there's a patch to the core tests at https://core.trac.wordpress.org/changeset/40547 which will work around the problem. It also includes changes to travis.yml, which you may not have in your setup; if that's the case then you'll need to edit the .diff file to ignore the Travis patch.
- Download the "Unified Diff" patch from the bottom of https://core.trac.wordpress.org/changeset/40547
Edit the patch file to remove the Travis part of the patch if you don't need that. Delete from the top of the file to just above this line:
Index: /branches/4.7/tests/phpunit/includes/bootstrap.php
Save the diff in the directory above your /includes/ directory - in my case this was the Wordpress directory itself
Use the Unix patch tool to patch the files. You'll also need to strip the first few slashes to move from an absolute to a relative directory structure. As you can see from point 3 above, there are five slashes before the include directory, which a -p5 flag will get rid of for you.
$ cd [WORDPRESS DIRECTORY] $ patch -p5 < changeset_40547.diff
After I did this my tests ran correctly again.
NOTICE: Command php bin/console generate:doctrine:crud
also create TestController
in src/Tests
so it can throw error when you tried to start server if you don't have UnitTests
. Remove the file fix it!
I am using php 5.6 on window 10 with zend 1.12 version for me adding
require_once 'PHPUnit/Autoload.php';
before
abstract class Zend_Test_PHPUnit_ControllerTestCase extends PHPUnit_Framework_TestCase
worked. We need to add this above statement in ControllerTestCase.php file
I use ZF2 and work for me when replaced 'PHPUnit_Framework_TestCase' to '\PHPUnit\Framework\TestCase'
For me, it was because I ran
$ phpunit .
instead of
$ phpunit
when I already had a configured phpunit.xml
file in the working directory.
Short and sweet; this works for me:
PHP ping
function
function ping($host){
$start=microtime(true);
$fp=@fsockopen($host, 80, $errno, $errstr, 10);
return (!$fp?'(Unknown)': round((microtime(true)-$start)*1000));
}
Usage:
ping('google.com');
'Programing' 카테고리의 다른 글
컴파일러 / 최적화 프로그램이 더 빠른 프로그램을 만들 수있게하는 코딩 방법 (0) | 2020.07.19 |
---|---|
절대적으로 위치한 부모 div 안에 div를 세로로 가운데에 배치하는 방법 (0) | 2020.07.19 |
Android에서 사용자 정의 모양에 그림자 추가 (0) | 2020.07.19 |
배열 복사 대 버퍼 블록 복사 (0) | 2020.07.18 |
SQLite에 Long 유형이 있습니까? (0) | 2020.07.18 |