php 当我尝试从 PhpStorm 运行 PHPUnit 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15971310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:19:12  来源:igfitidea点击:

Error when I try run PHPUnit from PhpStorm

phptestingphpunitphpstormcomposer-php

提问by Piotr Olaszewski

I have little problem when I'm trying to run PHPUnit test in IDE PhpStorm.

当我尝试在 IDE PhpStorm 中运行 PHPUnit 测试时,我没有遇到什么问题。

I use composer file which looks:

我使用看起来像的作曲家文件:

{
    "require": {
        "phpunit/phpunit": "3.7.19"
    }
}

Now when I run test I recive exception: PHP Fatal error: Uncaught exception 'PHPUnit_Framework_Exception' with message 'Class "PHPUnit_Extensions_RepeatedTest" does not extend PHPUnit_Framework_TestCase.'

现在,当我运行测试时,我收到异常: PHP Fatal error: Uncaught exception 'PHPUnit_Framework_Exception' with message 'Class "PHPUnit_Extensions_RepeatedTest" does not extend PHPUnit_Framework_TestCase.'

What is wrong? When I included pear installed version test working OK.

怎么了?当我包含梨安装版本测试工作正常。

//EDITSample test class:

//编辑示例测试类:

 class ReaderTest extends PHPUnit_Framework_TestCase
    {
        /**
         * @test
         */
        public function shouldGetReadedValue ()
        {
            $this->assertTrue(true);
        }
    }

//EDIT2Trace:

// EDIT2跟踪:

/usr/bin/php /tmp/ide-phpunit.php --no-configuration /path/to/my/project
Testing started at 14:53 ...
PHP Fatal error:  Uncaught exception 'PHPUnit_Framework_Exception' with message 'Class "PHPUnit_Extensions_RepeatedTest" does not extend PHPUnit_Framework_TestCase.' in /path/to/my/project/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:183
Stack trace:
#0 /path/to/my/project/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php(315): PHPUnit_Framework_TestSuite->__construct(Object(ReflectionClass))
#1 /path/to/my/project/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php(389): PHPUnit_Framework_TestSuite->addTestSuite(Object(ReflectionClass))
#2 /path/to/my/project/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php(416): PHPUnit_Framework_TestSuite->addTestFile('/var/www/php-sh...')
#3 /path/to/my/project/vendor/phpunit/phpunit/PHPUnit/Runner/BaseTestRunner.php(96): PHPUnit_Framework_TestSuite->addTestFiles(Array)
#4 /path/to/my/project/vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php(150): PHPUnit_Runner_BaseTestRunner->getTest('/var/www/php-sh...', '', A in /path/to/my/project/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php on line 183

Process finished with exit code 255

采纳答案by Piotr Olaszewski

I found solution about this problem.

我找到了关于这个问题的解决方案。

In Edit configurations in directory I set path to my tests catalog (/path/to/my/project/tests), after this tests are running properly.

/path/to/my/project/tests此测试正常运行后,在目录中的编辑配置中,我将路径设置为我的测试目录 ( )。

回答by rahmat

I have same problem when using composer.

我在使用作曲家时遇到了同样的问题。

The solution is to put your test file in its own directory. Here is my working phpunit, i put all my test in testdirectory.

解决方案是将您的测试文件放在其自己的目录中。这是我的工作 phpunit,我将所有测试放在test目录中。

<phpunit bootstrap="vendor/autoload.php" 
    colors="true"
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    stopOnFailure="true">
    <testsuites>
        <testsuite name="Test Suite">
            <directory>test</directory>
        </testsuite>
    </testsuites>
</phpunit>

Hope it solves if anyone have same problem.. :)

如果有人有同样的问题,希望它能解决.. :)

回答by CommaToast

This is what worked for me, thanks to Piotr's answer above, but I'm providing with a bit more exact detail here all the steps I had to do:

这对我有用,多亏了上面 Piotr 的回答,但我在这里提供了更准确的细节,我必须做的所有步骤:

Steps to make it work (test in PHPStorm 8.0.1):

使其工作的步骤(在 PHPStorm 8.0.1 中测试):

1) InPreferences > PHP > PHPUnitmake sure that nothing is set for Default configuration file or default bootstrap file.

1)Preferences > PHP > PHPUnit确保没有为默认配置文件或默认引导文件设置任何内容。

2) Make a custom PHPUnit Configuration viaRun > Edit Configurations >in theCommand Linesubsection, and be sure to:

2) 通过Run > Edit Configurations >小节中进行自定义 PHPUnit 配置Command Line,并确保:

a) setCustom working directory:to be/absolute/path/to/vendor.

a) 设置Custom working directory:/absolute/path/to/vendor

b) check "Use alternative configuration file:" and set it to/absolute/path/to/vendor/your_app/(sub_app_if_applicable)/phpunit.xml.dist

b)选中“使用替代配置文件:”并将其设置为/absolute/path/to/vendor/your_app/(sub_app_if_applicable)/phpunit.xml.dist

Then you can run any test class in the suite by specifying the class and file, or just check "Defined in the configuration file" to run all of them according to the config.

然后您可以通过指定类和文件来运行套件中的任何测试类,或者只需选中“在配置文件中定义”即可根据配置运行所有测试类。

回答by Jeremy Harris

Inside the PHPUnit_Framework_TestSuite, this code exists in the constructor:

在 PHPUnit_Framework_TestSuite 中,此代码存在于构造函数中

if (!$theClass->isSubclassOf('PHPUnit_Framework_TestCase')) {
   throw new PHPUnit_Framework_Exception(
      'Class "' . $theClass->name . '" does not extend PHPUnit_Framework_TestCase.'
   );
}

I see in your examplethat you are extending PHPUnit_Framework_TestCasebut the error suggests you are using PHPUnit_Extensions_RepeatedTestwhich extends PHPUnit_Extensions_TestDecoratorwhich ultimately extends PHPUnit_Framework_Assert

我在你的例子中看到你正在扩展,PHPUnit_Framework_TestCase但错误表明你正在使用PHPUnit_Extensions_RepeatedTest哪个扩展PHPUnit_Extensions_TestDecorator最终扩展PHPUnit_Framework_Assert

PHPUnit_Framework_Assert
   |
   --PHPUnit_Extensions_TestDecorator
      |
      --PHPUnit_Extensions_RepeatedTest

Double check your tests because the error suggests you are attempting to run a TestSuite using a test extending PHPUnit_Extensions_RepeatedTest. Were you instead trying to extend PHUnit using Test Decorators?

仔细检查您的测试,因为错误表明您正在尝试使用扩展PHPUnit_Extensions_RepeatedTest. 您是否尝试使用测试装饰器来扩展 PHUnit?

http://docs.tadiavo.com/phpunit/www.phpunit.de/pocket_guide/3.1/en/extending-phpunit.html

http://docs.tadiavo.com/phpunit/www.phpunit.de/pocket_guide/3.1/en/extending-phpunit.html

That is all the advice I can currently offer without seeing your actual tests and how you are running them.

这就是我目前可以提供的所有建议,而无需查看您的实际测试以及您如何运行它们。