How to run single test method with phpunit?

前端 未结 13 2098
猫巷女王i
猫巷女王i 2020-11-30 16:13

I am struggling to run a single test method named testSaveAndDrop in the file escalation/EscalationGroupTest.php with phpunit. I tried

相关标签:
13条回答
  • 2020-11-30 16:59

    I am late to the party though. But as personal I hate to write the whole line.

    Instead, I use the following shortcuts in the .bash_profile file make sure to source .bash_profile the file after adding any new alias else it won't work.

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

    Usage:

    puf function_name
    
    puf filename
    

    If you use Visual Studio Code you can use the following package to make your tests breeze.

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

    You can then set the keybinding in the settings. I use Command + T binding in my MAC.

    Now once you place your cursor on any function and then use the key binding then it will automatically run that single test.

    If you need to run the whole class then place the cursor on top of the class and then use the key binding.

    If you have any other things then always tweek with the Terminal

    Happy Coding!

    0 讨论(0)
  • 2020-11-30 17:05

    for run phpunit test in laravel by many way ..

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

    for test single class :

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

    for test single method :

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

    for run tests from all class within namespace :

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

    for more way run test see more

    0 讨论(0)
  • 2020-11-30 17:07
    • Run this inside your project root directory i am using in laravel root directory.

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

    Example with custom 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');
      }
    

    Example with test keyword

    /**
    * A basic test example.
    *
    * @return void
    */
     public function testBasicTest()
     {
      $this->assertTrue(true);
     }
    
    0 讨论(0)
  • 2020-11-30 17:08

    Here's the more generic answer:

    If you are sure the method name is unique you can only filter by method name (this works for me)

    phpunit --filter {TestMethodName}
    

    However it is safer to specify the file path/reference as well

    phpunit --filter {TestMethodName} {FilePath}
    

    Example:

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

    Quick note: I've noticed that if I have a function named testSave and another function named testSaveAndDrop using command phpunit --filter testSave will also run testSaveAndDrop and any other function that starts with testSave*, it's weird!!

    0 讨论(0)
  • 2020-11-30 17:11

    So, something like this

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

    Without = and with '

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

    0 讨论(0)
  • 2020-11-30 17:12

    I prefer marking the test in annotation as

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

    Then running it with

    phpunit --group failing
    

    No need to specify the full path in the command line, but you have to remember removing this before commit, not to clutter the code.

    You may also specify several groups for a single test

    /**
      * @group failing
      * @group bug2204 
      */
    public function testSomethingElse()
    {
    }
    
    0 讨论(0)
提交回复
热议问题