How to Run Single Test Method with PHPunit

How to run single test method with phpunit?

The following command runs the test on a single method:

phpunit --filter testSaveAndDrop EscalationGroupTest escalation/EscalationGroupTest.php

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

For newer versions of phpunit, it is just:

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

Run a single test with phpUnit

Can you try running the full phpunit executable?

./vendor/bin/phpunit tests/Controller/DefaultControllerTest

The second version of the command should work. You can go even more fine-grained than testfiles by putting an @group in the PHPdoc part to run individual functions like so:

/**
* A basic test example.
* @group test
* @return void
*/

public function testHomePage()
{
// basic get test
$this->get('')->assertStatus(200)
->assertSee('Home');
}

run it with

./vendor/bin/phpunit --group=test

how to test specific test class using phpunit in laravel

After trying several ways, I found out that I don't need to include the folder to test the specific test class. This works for me it runs all the test on the class:

phpunit --filter ApplicationVersionFormatTest

I think it's because my ApplicationVersionFormatTest extend The TestCase and return application instance which serves as the "glue" for all the components of Laravel.

How to run a single test case method in cakephp

There is no need to write a wrapper script

To run one test method

Use the filter option:

-> phpunit --help
PHPUnit 4.4.1 by Sebastian Bergmann.

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

--filter <pattern> Filter which tests to run.

For example:

-> phpunit --debug --filter testValidationDefault tests/TestCase/Model/Table/PostsTableTest.php 
PHPUnit 4.4.1 by Sebastian Bergmann.

Configuration read from /var/www/cakephp.dev/phpunit.xml.dist

Starting test 'App\Test\TestCase\Model\Table\PostsTableTest::testValidationDefault'.
I

Time: 130 ms, Memory: 9.75Mb

OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Incomplete: 1.
www-data @ dev [ /var/www/cakephp.dev ]
->

PhpStorm run single test by clicking on the test method

If you place caret on method in PHPUnit test file, pressing "Run" shortcut will allow to choose to run only this method. Once configuration is created, it will be launched automatically so you just place the caret and trigger "Run" action.
There's also "Run Context Configuration" action/shortcut which quickly brings you to creating a configuration for method and running it without selecting from available options first.

Run PHPUnit Tests in Certain Order

Maybe there is a design problem in your tests.

Usually each test must not depend on any other tests, so they can run in any order.

Each test needs to instantiate and destroy everything it needs to run, that would be the perfect approach, you should never share objects and states between tests.

Can you be more specific about why you need the same object for N tests?

How to run specific files in PHPUnit?

You should use regular expressions. For example

phpunit --filter 'Test1|Test2'

You can use offical documentation https://phpunit.readthedocs.io/en/stable/textui.html#command-line-options

How do I run the phpunit in a single file for all the model/controller

For phpunit tests to run they either start with the word test or have a @test annotation. Your product tests does not follow these rules.

The usually solution

public function test_can_user_insert_product(){

Or the annotation solution

/** @test **/
public function can_user_insert_product

PHPUnit test specific method of class with dependencies

You could create a mocked repository and set it into the container. The rest will be resolved via autowiring. Make sure that your container is getting reloaded for each test.

Example

To set a mocked instance into the container add this method into a trait or base class:

use PHPUnit\Framework\MockObject\MockObject;

// ...

protected function mock(string $class): MockObject
{
$mock = $this->getMockBuilder($class)
->disableOriginalConstructor()
->getMock();

$this->container->set($class, $mock);

return $mock;
}

Test Usage

public function testFindAllUsers()
{
// Data provider
$userProvider = new UserProvider();
$users = $userProvider->getSampleUsers();

// Mock the required repositories
$this->mock(UserRepository::class)
->method('findAllUsers')
->willReturn($users);

// Instantiate UserService
$service = $this->container->get(UserService::class);

// Compare the result
$this->assertEquals($users, $service->findAllUsers());
}


Related Topics



Leave a reply



Submit