【问题标题】:How to run single test method with phpunit?如何使用 phpunit 运行单个测试方法?
【发布时间】:2014-11-23 13:26:08
【问题描述】:

我正在努力在文件escalation/EscalationGroupTest.phpphpunit 中运行一个名为testSaveAndDrop 的测试方法。我尝试了以下组合:

phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=escalation/EscalationGroupTest.php::testSaveAndDrop
phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=EscalationGroupTest.php::testSaveAndDrop
phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=EscalationGroupTest::testSaveAndDrop
phpunit EscalationGroupTest escalation/EscalationGroupTest.php --filter=testSaveAndDrop

在每种情况下所有文件escalation/EscalationGroupTest.php中的测试方法都被执行。如何只选择一种方法?

类的名称是EscalationGroupTestphpunit的版本是3.2.8。

【问题讨论】:

  • 你的测试类的类名是什么?

标签: php phpunit


【解决方案1】:

以下命令在单个方法上运行测试:

phpunit --filter testSaveAndDrop EscalationGroupTest escalation/EscalationGroupTest.php

phpunit --filter methodName ClassName path/to/file.php

对于较新版本的phpunit,它只是:

phpunit --filter methodName path/to/file.php

【讨论】:

  • 是否需要编写 EscalationGroupTest?它有什么用?
  • 好的,我知道这是类名
  • 它也会运行名为 testSaveAndDrop* 的测试方法(例如:testSaveAndDropSomething)。要准确运行 testSaveAndDrop,请使用 --filter '/::testSaveAndDrop$/'
  • @mujaffars 我对此表示怀疑,由于空格,“我知道了”不是 php 中的有效标识符。
  • 现在不行了。只是phpunit --filter methodName path/to/testing/file,不是ClassName
【解决方案2】:

我更喜欢将注释中的测试标记为

/**
 * @group failing
 * Tests the api edit form
 */
public function testEditAction()

然后运行它

phpunit --group failing

不需要在命令行中指定完整路径,但你必须记住在提交之前将其删除,以免使代码混乱。

您还可以为单个测试指定多个组

/**
  * @group failing
  * @group bug2204 
  */
public function testSomethingElse()
{
}

【讨论】:

  • 我喜欢这种方法,是否可以通过注解分配多个组? @group failing, Integration
  • 是的,当然。但不是逗号分隔。编辑答案来说明这一点。
  • 我更喜欢这种方法...作为参考 phpunit.xml 中的 元素也可用于过滤测试...在<phpunit> 下使用类似的内容:失败
  • 但问题是 - 运行 SINGLE 测试。此外 - 名为失败的组是您可以使用的最糟糕的。因为在某些测试失败后,您可以运行 phpunit --group failed 运行失败的测试而不给他们组。令人困惑。
  • 最佳答案!如果 @depends 来自另一个测试,则没有单一测试,因此您必须查明所有相互关联的测试,因此使用 @group 注释。
【解决方案3】:

这是更通用的答案:

如果您确定方法名称是唯一的,您只能按方法名称过滤(这对我有用)

phpunit --filter {TestMethodName}

但是指定文件路径/引用也更安全

phpunit --filter {TestMethodName} {FilePath}

例子:

phpunit --filter testSaveAndDrop reference/to/escalation/EscalationGroupTest.php

快速说明:我注意到,如果我有一个名为 testSave 的函数和另一个名为 testSaveAndDrop 的函数,使用命令 phpunit --filter testSave 也将运行 testSaveAndDrop 和任何其他以 testSave* 开头的函数,它是奇怪!!

【讨论】:

  • 一点也不奇怪,基本就是子串匹配。如果您想要这种行为,请使用字符串结束标记:/testSave$/
  • 如果您只想指定文件路径以便运行该文件中的所有测试怎么办?
【解决方案4】:

以下命令将执行完全 testSaveAndDrop 测试。

phpunit --filter '/::testSaveAndDrop$/' escalation/EscalationGroupTest.php

【讨论】:

  • 对我不起作用。使用\b 而不是$ 终于成功了:--filter '/::testSaveAndDrop\b/'
  • 方法名称结尾 '/::testSaveAndDrop\b/''/::testSaveAndDrop$/' 都像 phpunit --filter ClassName::methodName$phpunit --filter ClassName::methodName\b 一样为我工作(在 Linux 上)
【解决方案5】:
  • 在我在 laravel 根目录中使用的项目根目录中运行它。

vendor/bin/phpunit --filter 'Your method name'

带有自定义方法名称的示例。

 /** @test //Initilize this for custom method name, without test keyword
  *  
  * Test case For Dashboard When User Not logged In it will redirect To login page
  */
  public function only_logged_in_user_see_dashboard()
  {
    $response = $this->get('/dashboard')
                   ->assertRedirect('/login');
  }

测试关键字示例

/**
* A basic test example.
*
* @return void
*/
 public function testBasicTest()
 {
  $this->assertTrue(true);
 }

【讨论】:

    【解决方案6】:

    通过多种方式在 laravel 中运行 phpunit 测试..

    vendor/bin/phpunit --filter methodName className pathTofile.php
    
    vendor/bin/phpunit --filter 'namespace\\directoryName\\className::methodName'
    

    用于测试单个类:

    vendor/bin/phpunit --filter  tests/Feature/UserTest.php
    vendor/bin/phpunit --filter 'Tests\\Feature\\UserTest'
    vendor/bin/phpunit --filter 'UserTest' 
    

    用于测试单一方法:

     vendor/bin/phpunit --filter testExample 
     vendor/bin/phpunit --filter 'Tests\\Feature\\UserTest::testExample'
     vendor/bin/phpunit --filter testExample UserTest tests/Feature/UserTest.php
    

    对于命名空间内所有类的运行测试:

    vendor/bin/phpunit --filter 'Tests\\Feature'
    

    更多方式运行测试see more

    【讨论】:

      【解决方案7】:

      所以,像这样

      phpunit --filter 'EscalationGroupTest::testSaveAndDrop' EscalationGroupTest escalation/EscalationGroupTest.php 
      

      没有='

      https://phpunit.de/manual/3.7/en/textui.html

      【讨论】:

      • 不,这不起作用。正在处理 EscalationGroupTest 类中的所有测试。
      • 仍在运行所有 9 个测试。 phpunit 3.2.8版
      • --filter 移到文件名之前,一切正常。
      • 在 PHPUnit 4.8.10 上为我工作
      • 这给了我在 PHPUnit 上的 No tests executed! 4.8.35 @Schleis 你到底是如何更改命令的?
      【解决方案8】:

      如果您在 netbeans 中,您可以右键单击测试方法,然后单击“运行重点测试方法”。

      【讨论】:

        【解决方案9】:

        你可以试试这个我可以运行单个测试用例

        phpunit tests/{testfilename}
        

        例如:

        phpunit tests/StackoverflowTest.php
        

        如果你想在 Laravel 5.5 中运行单个测试用例,试试

        vendor/bin/phpunit tests/Feature/{testfilename} 
        
        vendor/bin/phpunit tests/Unit/{testfilename} 
        

        例如:

        vendor/bin/phpunit tests/Feature/ContactpageTest.php 
        
        vendor/bin/phpunit tests/Unit/ContactpageTest.php
        

        【讨论】:

        • 问题是如何运行单一测试方法(不是类)
        【解决方案10】:

        所有测试都在运行的原因是文件名后面有--filter 标志。 PHPUnit 根本不读取选项,因此正在运行所有测试用例。

        从帮助屏幕:

         Usage: phpunit [options] UnitTest [UnitTest.php]
                phpunit [options] <directory>
        

        因此,将--filter 参数移到您想要的测试文件之前,如@Alex 和 @Ferid Mövsümov 回答。而且你应该只拥有你想要运行的测试。

        【讨论】:

        • 我不认为这解决了最初的问题......我遇到了同样的问题,需要在包含许多单元测试的文件中运行其中一个测试,而另外两个答案使用过滤器为我工作
        • @jfoo OP 命令的问题之一是--filter 选项位于文件名之后。其他两个答案是正确答案,但没有指出为什么要应用过滤器。
        【解决方案11】:

        如果您使用的是 XML 配置文件,您可以在 phpunit 标签内添加以下内容:

        <groups>
          <include>
            <group>nameToInclude</group>
          </include>
          <exclude>
            <group>nameToExclude</group>
          </exclude>
        </groups>
        

        https://phpunit.de/manual/current/en/appendixes.configuration.html

        【讨论】:

          【解决方案12】:

          不过我迟到了。但作为个人,我讨厌写整行。

          相反,我在 .bash_profile 文件中使用以下快捷方式,确保在添加任何新别名后source .bash_profile 文件,否则它将不起作用。

          alias pa="php artisan"
          alias pu="vendor/bin/phpunit" 
          alias puf="vendor/bin/phpunit --filter"
          

          用法:

          puf function_name
          
          puf filename
          

          如果您使用 Visual Studio Code,您可以使用以下包轻松完成测试。

          Package Name: Better PHPUnit
          Link: https://marketplace.visualstudio.com/items?itemName=calebporzio.better-phpunit
          

          然后您可以在设置中设置键绑定。我在我的 MAC 中使用 Command + T 绑定。

          现在,一旦您将光标放在任何 函数 上,然后使用键绑定,它就会自动运行该单个测试。

          如果您需要运行整个,请将光标放在类的顶部,然后使用键绑定。

          如果您有任何其他事情,请始终使用 Terminal

          编码愉快!

          【讨论】:

            【解决方案13】:

            您必须使用 --filter 来运行单个测试方法 php phpunit --filter "/::testMethod( .*)?$/" ClassTest ClassTest.php

            上述过滤器将单独运行 testMethod。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2022-01-24
              • 2018-10-31
              • 2010-12-24
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多