Laravel : phpunit cannot open file ExampleTest.php - php

Hi i am quit new in Laravel PHPUnit, getting the following error :
Laravel : phpunit cannot open file ExampleTest.php
I don't have idea why i am getting this error. I installed PHPUnit globally and when i run "phpunit" in terminal it runs fine. But I want to run it on specific file like :
phpunit ExampleTest
Thanks In Advance.

Make sure you are on the project root and referencing the file inside the tests folder.
Example:
phpunit tests/ExampleTest.php

I am working in Windows. Try to use the full path:
C:\Desktop\code\blog\vendor\bin>phpunit C:\Desktop\code\blog\tests\Feature\ExampleTest.php
PHPUnit 7.2.6 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 1.46 seconds, Memory: 10.00MB
OK (1 test, 1 assertion)

This started happening when I upgrade phpUnit, I had to update phpStorm to fix it.

try it like this:
vendor\bin\phpunit tests\Unit\ExampleTest.php
it will work surly
but first install phpunit globally.

Maybe you call wrong to your test.
Try like this :
php artisan test --filter YourTestClassName

Related

Symfony Unit tests failing on Travis CI

I'm creating a Symfony 3.4 application.
Then I started to write some tests, which totally pass locally :
/**
* Two steps :
* 1) Checks if page goes on 200 status
* 2) Posts sample datas
*/
public function testDistanceAmongSequencesAction()
{
/**
* 1 - Access to the page OK
*/
$this->client->request('GET', '/minitools/distance-among-sequences');
static::assertEquals(200, $this->client->getResponse()->getStatusCode());
}
But when I push my code and execute it with Travis CI, it fails automatically : 3) MinitoolsBundleTest\Controller\MinitoolsControllerTest::testDistanceAmongSequencesAction
Failed asserting that 500 matches expected 200.
He is my .travis.yml file :
language: php
sudo: required
dist: trusty
php:
- '7.1.16'
services:
- mysql
script:
- composer self-update
- composer install --no-interaction --no-suggest
- bin/console server:run &
- vendor/bin/phpunit
The PHP version is the same as mine. I think the VM Travis can't access to y path, but why ? Thank you for your help !
OK, in my controller I called the TWIG templates using # annotation, and Travis doesn't understand it. So I modified each call and moved the TWIG into app/ressources. Thanks.

How to run a single test case method in cakephp

I am using the cakephp test (php unit test) with xdebug to run the server side code and monitor the output from the terminal rather than using it for validation.
So, every time, i want to run some particular lib/controller/model method from the terminal and see the output, i have to comment out the other test case functions.
I know this might not be the right approach,
but i want to know, if there is a way, i can build up a wrapper around the cake test, that will take the argument of the method name which i want to run?
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 ]
->
To run only one test use the filter option in the cake command line tool:
cake test app Model/Post --filter testGetAllPostsByAuthor
Works on CakePhp 2.2

How to fix the "unrecognized option --run" on NetBeans running PHPUnit

I'm trying to run the PHPUnit into the NetBeans 8.0.2.
If I run # phpunit inside my folder tests all tests run. So it's seems been set right.
But in the NetBeans output I always got:
"C:\nginx\php\5.6.12\php.exe" "C:\nginx\php\5.6.12\phpunit.phar" "--colors" "--log-junit" "C:\Users\...\AppData\Local\Temp\nb-phpunit-log.xml" "--bootstrap" "E:\var\www\...\tests\TestHelper.php" "--configuration" "E:\var\www\...\tests\phpunit.xml" "C:\Program Files\NetBeans 8.0.2\php\phpunit\NetBeansSuite.php" "--run=E:\var\www\...\tests\app\utils\FormatUtilTest.php"
PHPUnit 4.8.2 by Sebastian Bergmann and contributors.
unrecognized option --run
Done.
Maybe the "--run message" it's right, because this command doesn't exist in the PHPUnit manual. But if is that so, how to create another script for the NetBeans execute the tests?
I ran into the same issue yesterday after a PHPUnit update. So I reverted back to PHPUnit 4.7.7 for now, until this is fixed in NB.
I too have encountered this error causing the latest version of PHPUnit not to work with NetBeans v8.0.2.
Further investigating this issue has resulted in determining that there is an incompatibility between NetBeansSuite.php and the latest version of PHPUnit.
The error 'unrecognized option --run' is being thrown by the phpunit.phar and not being thrown by NetBeansSuite.php. I also do not believe the NetBeansSuite.php is even being executed.
phpunit.phar, line 63816 or 63894, is where the exception is being thrown.
Until this is fixed, PHPUnit will not work from NetBeans v8.0.2.
#Pablo: I thank you for opening an issue and have commented on your issue.
PHPUnit v4.7.7 works correctly.
Opened a bug report: https://netbeans.org/bugzilla/show_bug.cgi?id=254276
The answer is: This was indeed a bug in NB and is now fixed in nightly.
Possible solutions:
Upgrade to latest nightly from http://bits.netbeans.org/dev/nightly/
Use PHPUnit 4.7.7 as mentioned by Luuk
"--run" option is used in the NetBeansSuite.php for running tests. So, you should submit that to NetBeans bugzilla [1] if you want to avoid that.
[1] https://netbeans.org/community/issues.html (PHP/PHPUnit)
2019 and the problem still exists. Downgrading PHPUnit is no option anymore today. So I created, as a workaround, a wrapper script for phpunit which takes care of removing the bad parameters.
It also removes the NetBeansSuite.php file completely because it does the same a call directly to the phpunit script would.
My setup is Windows Netbeans 11, Windows PHP 7.3, and a cygwin wrapper (because it's bash code). If somebody feels like porting this to native windows, please leave a comment here.
#!/usr/bin/env sh
# modified phpunit script originally from vendors dir
# 1. cygwin default search path is bad in my installation...
PATH="$PATH:/bin:/usr/bin"
# original code, only adjust the ../../ to match your installation.
# the directory this is run in is where the wrapper script resides
dir=$(cd "${0%[/\\]*}" > /dev/null; cd "../../vendor/phpunit/phpunit" && pwd)
if [ -d /proc/cygdrive ] && [[ $(which php) == $(readlink -n /proc/cygdrive)/* ]]; then
# We are in Cgywin using Windows php, so the path must be translated
dir=$(cygpath -m "$dir");
fi
# fix netbeans parameters
while [ $# -gt 0 ]; do
case "$1" in
*NetBeansSuite.php) shift;;
--run=*) PARAMS="$PARAMS ${1#--run=}"; shift;;
--) shift ;;
*) PARAMS="$PARAMS $1"; shift;;
esac
done
"${dir}/phpunit" $PARAMS
You have to adjust both occurrences of "../"s to your folder structure.
This solution works with ALT-F6 (run all tests), right clicking a test folder "Run Tests" and also single test files right click "Run".
The setting for "Netbeans: Tools/Options/Frameworks & Tools/PHPUnit" is then:
C:\cygwin\bin\bash.exe /cygdrive/c/workspace/Project/src/cli/phpunit
Adjust these path names to your needs, again.
HTH,
M.

PHPUnit cannot open file

I've started learning how to use PHPUnit. However, I'm facing a problem which I have no clue how to solve.
I have a folder called lessons and inside there is a composer.json which I installed PHPUnit with.
The output resulted in a folder called vendor and a sub-folder called bin which has the phpunit file in it.
In the cmd I typed: cd c:/xampp/htdocs/lessons/vendor/bin. Now the cmd folder sets to the same folder as phpunit. I've created a directory in lessons which I called tests (lessons/tests) which I store all of my tests in. I've created a file called PracticeTest.php with a very simple test script in it.
When I go back to the cmd and type phpunit tests I get cannot open file tests.php When I try to type phpunit PracticeTest I get cannot open file PracticeTest.php. When I try phpunit tests/PracticeTest (with .php or without) I get the same error that the file could not be opened.
My suspicious that it has something to do with that face that the cmd is pointing at the lessons/vendor/bin directory, but I'm not sure if it is really the problem or how to fix it.
just to arrange the paths:
lessons\vendor\bin\
lessons\tests\
lessons\tests\PracticeTest.php
Thanks in advance!
Go to path project:
D:\>cd www
D:\wwww>cd lessons
And execute:
D:\www\lessons>vendor\bin\phpunit tests
PHPUnit 4.8.27 by Sebastian Bergmann and contributors.
.....E.
Time: 1.34 seconds, Memory: 5.50MB
There was 1 error:
1) UserTest::it_should_be_able_to_construct
PHPUnit_Framework_Exception: Argument #1 (No Value) of PHPUnit_Framework_Assert
:assertInstanceOf() must be a class or interface name
D:\www\lessons\tests\UserTest.php:10
FAILURES!
Tests: 7, Assertions: 4, Errors: 1.
It Works!!!!
This is what worked for me.
vendor/bin/phpunit ./tests/PracticeTest
I was getting the same, but if you type phpunit then tab you will see what directory you are in and it should autosuggest. Then navigate to your test file.
I had include_path sections separated by comma. It has to be semicolon on Windows.
I have created a phpunit.bat in the root folder of my project containing
#ECHO OFF SET BIN_TARGET=%~dp0/vendor/phpunit/phpunit/phpunit php "%BIN_TARGET%" %*
Now it works.

Zend Framework 2 PHPUnit Test Error

Hi I am currently running through the ZF2 User Guide trying to run a PHPUnit Test on the skeleton application as outlined here http://zf2.readthedocs.org/en/latest/user-guide/unit-testing.html.
But even though I have not edited the Module and have copied all the files from the tutorial every time I run the PHPUnit Test in Zend Studio I get this error
Error:
Fatal error: Class 'ApplicationTest\Bootstrap' not found in C:\Program Files (x86)\Zend\Apache2\htdocs\exerciseDB\module\Application\test\ApplicationTest\Controller\IndexControllerTest.php on line 24
But when I click on the Bootstrap::getServiceManager(); on line 24 Zend Studio takes me to the correct method.
File Stucture:
module
|_Application
|_config
|_language
|_src
|_test
|_ApplicationTest
| |_Controller
| |_IndexControllerTest.php
|_Bootstrap.php
|_phpunit.xml.dist
|_TestConfig.php.dist
Can anyine tell me where I am going wrong?
This was a really annoying bug that is somewhat of an irritation. Your phpunit config seems to be changed slightly from the one on the skeleton application. To give you direct answer that fixed my issue I did the following...
module
|_Application
|_config
|_language
|_src
|_tests
|_ZendApplicationModule
| |_Framework
| |TestCase.php
| |_IndexControllerTest.php
| |_SampleTest.php
|_Bootstrap.php
|_phpunit.xml.dist
|_TestConfig.php.dist
With the base setup as outlined above from the skeleton APP I had to add
require_once 'Framework/TestCase.php';
To SampleTest.php
For your actual fix you will need to require_once the file that is generating the issue. That should fix it.
Also make sure to update your autoload_classmap.php
I ran into the same issue where phpunit was not seeing my "module/Application/test/phpunit.xml.dist" file (I had it miss-spelled)
Try adding the file directly to the command line:
phpunit --bootstrap Bootstrap.php .
I had the issue as well.
I solved it by running phpunit from the root of my ZF2 project with the following arguments:
./vendor/bin/phpunit
--bootstrap ./module/Rest/test/Bootstrap.php
One can also specify the Test suite to use:
./vendor/bin/phpunit
--bootstrap ./module/Rest/test/Bootstrap.php
./module/Rest/test/RestTest/Controller/RestControllerTest.php
more details here

Categories