I want to test specific testClass in my project since there are a lot of test class that's failure and I just want to test one Class at a time.
I've created the test Class in following folder \test\repositories\ApplicationVersionFormat.php :
<?php
use App\Repositories\ApplicationVersionFormat;
class ApplicationVersionFormatTest extends \TestCase
{
public function testValidFormat()
{
$version = '1.2.3';
$appVersion = new ApplicationVersionFormat();
$this->assertEquals(true,$appVersion->isValidVersion($version));
}
public function testInvalidFormat()
{
$version = '11.2.3';
$appVersion = new ApplicationVersionFormat();
$this->assertEquals(false,$appVersion->isValidVersion($version));
}
public function testInvalidFormat2()
{
$version = '1.22.3';
$appVersion = new ApplicationVersionFormat();
$this->assertEquals(false,$appVersion->isValidVersion($version));
}
public function testInvalidFormat3()
{
$version = '1.2.33';
$appVersion = new ApplicationVersionFormat();
$this->assertEquals(false,$appVersion->isValidVersion($version));
}
public function testInvalidFormat4()
{
$version = '11.22.33';
$appVersion = new ApplicationVersionFormat();
$this->assertEquals(false,$appVersion->isValidVersion($version));
}
}
so I've tried this follwing command but none of this works :
phpunit "repositories\AppVersionTest". => Cannot open file "test/repositories/AppVersionTest.php"
phpunit "test\repositories\AppVersionTest". => Cannot open file "test/repositories/AppVersionTest.php"
phpunit --filter "repositories\AppVersionTest". => No tests executed!
phpunit --testsuite "repositories\AppVersionTest". => No tests executed!
any help? thanks
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.
for run phpunit test in laravel by many ways ..
vendor/bin/phpunit --filter methodName className pathTofile.php
vendor/bin/phpunit --filter 'namespace\\directoryName\\className::methodName'
for test single class:
vendor/bin/phpunit tests/Feature/UserTest.php
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
To do the same thing using artisan run:
php artisan test --filter ApplicationVersionFormatTest
For newer version this might work
php artisan test --filter {MethodName}
// for instance
php artisan test --filter test_example
OR
php artisan test --filter {ClassName}::{MethodName}
//for instance
php artisan test --filter ExampleTest::test_example
You can probably manage that with this answer to a related question.
Just mark the class with #group annotation and run PHPUnit with --group <group_name>
Update
Your command with --filter is not complete. Try this: phpunit --filter AppVersionTest "repositories\ApplicationVersionFormat.php"
For laravel 8
php artisan test --filter ExampleTest
phpunit --filter <relative_path_to_file>
for example , files to test are,test/repos/EloquentPushTest.php,test/repos/EloquentWebTest.php
for testing EloquentWebTest.php
use phpunit --filter ./repos/ElqouentWebpush
Example of use:
php phpunit-5.7.27.phar -c app/ "src/package/TestBundle/Tests/Controller/ExampleControllerTest.php"
Using phpunit.xml
the #group option can be used on classes and methods.
class A
/**
* #group active
* */
class ArrayShiftRightTest extends TestCase
{
}
class B
/**
* #group inactive
* */
class BinaryGapTest extends TestCase
{
}
in phpunit.xml
<groups>
<include>
<group>active</group>
</include>
<exclude>
<group>inactive</group>
</exclude>
</groups>
the classes that belongs to group active will be executed.
Related
I wrote the following test:
use PHPUnit\Framework\TestCase;
//other uses
class StatusCheckerTest extends TestCase
{
public function testEntryValidation() {
//some code
$this->assertFalse($validation_result);
}
}
I tried to run it with phpunit StatusCheckerTest.php
But when I do it, I see error:
PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in
/var/www/html/nextcloud/apps/globalstatus/tests/OCA/GlobalStatus/Tests/StatusCheckerTest.php on line 12
I used PHPUnit 8.4.3 (also tried with 6.5.5). I installed it manually.
phpunit --version
PHPUnit 8.4.3 by Sebastian Bergmann and contributors.
Why does it look for PHPUnit_Framework_TestCase? It must be deprecated.
If you want to run only a specific test class, you need to pass it with a filter option:
phpunit --filter StatusCheckerTest
I am trying to mock a class for phpunit. Php unit fails with the error Could not load mock ... class already exists. This is the only test I'm running, so it can't be the case that the class is mocked already.
Any suggestion would be appreciated.
Here is the error case:
namespace Tests\Feature;
use Tests\TestCase;
class DeactivateACSTest extends TestCase
{
public function testDeactivateAcs()
{
$deviceController = \Mockery::mock('overload:App\Http\Controllers\Cloud\DeviceController');
$deviceController
->shouldReceive('deactivateACS')
->andReturn('hilfehilfehilfe');
$devCon = new \App\Http\Controllers\Cloud\DeviceController();
$this->assertEquals('hilfehilfehilfe', $devCon->deactivateACS());
}
}
When running it without --code-coverage it works:
[13:10:15] vagrant#homestead [~/Code/ekp] $ phpunit --filter DeactivateACS
PHPUnit 6.5.10 by Sebastian Bergmann and contributors.
==> Tests\Feature\DeactivateACSTest ✓
Time: 1.08 seconds, Memory: 16.00MB
OK (1 test, 3 assertions)
However, when running it with --code-coverage it fails:
[13:10:23] vagrant#homestead [~/Code/ekp] $ phpunit --coverage-html coverage --coverage-text=code_coverage.txt --filter DeactivateACSTest
PHPUnit 6.5.10 by Sebastian Bergmann and contributors.
==> Tests\Feature\DeactivateACSTest ⚈
Time: 5.79 seconds, Memory: 44.00MB
There was 1 error:
1) Tests\Feature\DeactivateACSTest::testDeactivateAcs
Mockery\Exception\RuntimeException: Could not load mock \App\Http\Controllers\Cloud\DeviceController, class already exists
/home/vagrant/Code/ekp/vendor/mockery/mockery/library/Mockery/Container.php:220
/home/vagrant/Code/ekp/vendor/mockery/mockery/library/Mockery.php:116
/home/vagrant/Code/ekp/tests/Feature/DeactivateACSTest.php:11
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
Generating code coverage report in HTML format ... done
You should add these annotations before the functions that are mocking this class.
/**
* #runInSeparateProcess
* #preserveGlobalState disabled
*/
For reference you can check out the phpunit documentation.
https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.runInSeparateProcess
https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.preserveGlobalState
I ran into the same issue and fixed like this:
There was another test in my unit tests (not mockery test) which had require_once on the PHP file that had the class I was mocking. I've removed that line.
I've added processIsolation="true" in test suite
I have a Laravel app installed in my directory f:\lara_app. I use PHP artisan serve to run the app. I have Laravel version 5.4.36 (via Composer Install)
I am not trying my hand in using the PHP Unit to do testing. Inside f:/lara_app/tests/Unit/ExampleTest.php, I have the following codes:
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testBasicTest()
{
$this->assertTrue(true);
}
public function myFirstTest()
{
$value = 'Hello World';
$this->assertTrue( 'Hello' === $value, 'Value should be Hello World');
}
}
I now try to run the test from my command prompt:
f:\lara_app> .vendor/phpunit/phpuni/phpunit
But I'm getting the message below:
'.vendor' is not recognized as an internal or external command, operable program or batch file
How do I run the PHPUnit test?
Thanks in advance
Use quotes:
"./vendor/bin/phpunit"
This should work on windows
f:\lara_app> php vendor/phpunit/phpunit/phpunit
This works for me
C:\code\project-name> vendor\bin\phpunit
For every Laravel project from version 7.x, if you want to run tests you can do one of these:
You can use artisan: php artisan test docs
Or you can use phpunit: if you want to run phpunit on windows, only by typing phpunit, you can create a bat file in your project's main directory by running this command:#echo php vendor\phpunit\phpunit\phpunit > phpunit.bat and then you can type phpunit in your command and enjoy.
Happy Testing...
Also for future readers, you can run it on windows powershell, without having to change anything, if you're in the root directory of your project. Simply Type
./vendor/bin/phpunit
Use quotes or use backslashes. Unlike linux, Windows does not like forward slashes.
Check if you have phpunit in composer.json, and run:
composer update
And then run PHPUnit:
vendor/bin/phpunit
I am struggling to run a single test method named testSaveAndDrop in the file escalation/EscalationGroupTest.php with phpunit. I tried the following combinations:
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
In each case all test methode in the file escalation/EscalationGroupTest.php are executed. How to select just ONE method instead?
The name of the class is EscalationGroupTest and the version of phpunit is 3.2.8.
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
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()
{
}
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!!
Following command will execute exactly testSaveAndDrop test.
phpunit --filter '/::testSaveAndDrop$/' escalation/EscalationGroupTest.php
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);
}
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
So, something like this
phpunit --filter 'EscalationGroupTest::testSaveAndDrop' EscalationGroupTest escalation/EscalationGroupTest.php
Without = and with '
https://phpunit.de/manual/3.7/en/textui.html
If you're in netbeans you can right click in the test method and click "Run Focused Test Method".
You Can try this i am able to run single Test cases
phpunit tests/{testfilename}
Eg:
phpunit tests/StackoverflowTest.php
If you want to run single Test cases in Laravel 5.5 Try
vendor/bin/phpunit tests/Feature/{testfilename}
vendor/bin/phpunit tests/Unit/{testfilename}
Eg:
vendor/bin/phpunit tests/Feature/ContactpageTest.php
vendor/bin/phpunit tests/Unit/ContactpageTest.php
The reason your tests are all being run is that you have the --filter flag after the file name. PHPUnit is not reading the options at all and so is running all the test cases.
From the help screen:
Usage: phpunit [options] UnitTest [UnitTest.php]
phpunit [options] <directory>
So move the --filter argument before the test file that you want as mentioned in #Alex and
#Ferid Mövsümov answers. And you should only have the test that you want run.
Given that you
vendor/bin/phpunit --filter=EscalationGroupTest::testSaveAndDrop
If you're using an XML configuration file, you can add the following inside the phpunit tag:
<groups>
<include>
<group>nameToInclude</group>
</include>
<exclude>
<group>nameToExclude</group>
</exclude>
</groups>
See https://phpunit.de/manual/current/en/appendixes.configuration.html
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!
You must use --filter to run a single test method
php phpunit --filter "/::testMethod( .*)?$/" ClassTest ClassTest.php
The above filter will run testMethod alone.
I have script called Script.php and tests for it in Tests/Script.php, but when I run phpunit Tests it does not execute any tests in my test file. How do I run all my tests with phpunit?
PHPUnit 3.3.17, PHP 5.2.6-3ubuntu4.2, latest Ubuntu
Output:
$ phpunit Tests
PHPUnit 3.3.17 by Sebastian Bergmann.
Time: 0 seconds
OK (0 tests, 0 assertions)
And here are my script and test files:
Script.php
<?php
function returnsTrue() {
return TRUE;
}
?>
Tests/Script.php
<?php
require_once 'PHPUnit/Framework.php';
require_once 'Script.php'
class TestingOne extends PHPUnit_Framework_TestCase
{
public function testTrue()
{
$this->assertEquals(TRUE, returnsTrue());
}
public function testFalse()
{
$this->assertEquals(FALSE, returnsTrue());
}
}
class TestingTwo extends PHPUnit_Framework_TestCase
{
public function testTrue()
{
$this->assertEquals(TRUE, returnsTrue());
}
public function testFalse()
{
$this->assertEquals(FALSE, returnsTrue());
}
}
?>
Php test's filename must end with Test.php
phpunit mydir will run all scripts named xxxxTest.php in directory mydir
(looks likes it's not described in the phpunit documentation)
I created following phpunit.xml and now atleast I can do phpunit --configuration phpunit.xml in my root directory to run the tests located in Tests/
<phpunit backupGlobals="false"
backupStaticAttributes="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Tests">
<directory suffix=".php">Tests</directory>
</testsuite>
</testsuites>
</phpunit>
I think forPHPUnit to decide to automatically run it it must follow a filename convention: somethingTest.php.
You think they would have documented this. I just looked through the manual, and they say you can pass a directory, but not really how to do it.
Perhaps your class name has to match the basename (everything but the ".php") of your test scripts filename?
<?php
//Files required for phpunit test
require_once 'PHPUnit/Framework.php';
//Knowing the drupal environment
require_once './includes/bootstrap.inc'; //initialize the Drupal framework
//Loading the drupal bootstrap
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
//Helper file
include_once 'helper.inc';
//Including inc file of addresses module
include_once(module_load_include('inc','addresses_user','addresses_user'));
class addresses_test extends PHPUnit_Framework_TestCase {
protected $uid;
protected function setUp()
{
$this->uid = 1;
}