问题
The Problem
To improve my quality of code, I've decided to try to learn how to test my code using Unit Testing instead of my mediocre-at-best testing solutions.
I decided to install PHPUnit using composer for a personal library that allows me to achieve common database functions. At first I didn't have a configuration file for PHPUnit and when I ran commands like:
$ phpunit tests/GeneralStringFunctions/GeneralStringFunctionsTest
Please note that this is a terminal command, so I didn't include the .php
extension. The GeneralStringFunctionsTest referred to above is actually a GeneralStringFunctionsTest.php
file.
The output is what I expected:
Time: 31 ms, Memory: 2.75Mb
OK (1 test, 1 assertion)
I then tried to use a configuration file to automatically load the test suite instead of having to manually type in the file every time. I created a file called phpunit.xml
in my root directory, and entered the following into the file: http://pastebin.com/0j0L4WBD:
<?xml version = "1.0" encoding="UTF-8" ?>
<phpunit>
<testsuites>
<testsuite name="Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
Now, when I run the command:
phpunit
I get the following output:
PHPUnit 4.5.0 by Sebastian Bergmann and contributors.
Configuration read from /Users/muyiwa/Projects/DatabaseHelper/phpunit.xml
Time: 16 ms, Memory: 1.50Mb
No tests executed!
In case it's helpful, my directory structure is as follows:
src - Top level directory (with all my source code)
tests - Top level directory (with all my tests, structured the same as my src folder)
vendor - Composer third party files
I also have the composer json and lock file, as well as the phpunit xml file in the top level as files.
Things I've Tried
- Changing the directory in
phpunit.xml
totests/GeneralStringFunctions
- Changing the directory in
phpunit.xml
to./tests
- Moving the
phpunit.xml
file to thetests
directory and then changing the directory to be./
instead oftests
. - Adding a suffix attribute to the directory tag in
phpunit.xml
to specify "Tests" as the explicit suffix.
回答1:
For what it's worth (being late), I ran into this recently while I was making a new Laravel 5.1 project for a simple website. I tried to debug it and was confused when I tried:
php artisan make:test homeTest
(which has a default test that just asserts true is true)
and saw the output
No tests executed!
What the problem ended up being for me was related to my PHP installation -- "phpunit" was globally registered and configured differently, whereas the phpunit that came with the Laravel installation was configured just right and ran perfectly.
So the fix is running the vendor's configured phpunit (from the same root directory as app/ and tests/):
./vendor/bin/phpunit
Hope that helps someone else!
回答2:
Your XML file is fine as it is. However, you have to make sure that the PHP files in your tests/
folder are named as follows:
tests/Test.php <--- Note the uppercase "T"
tests/userTest.php
tests/fooBarTest.php
etc.
The filenames must end with "Test.php". This is what PHPUnit is looking for within directories.
Furthermore, every test method must have a name that starts with "test":
public function testFooBar()
{
// Your test code
}
Hope that helps!
回答3:
On windows use the following command on terminal
.\vendor\bin\phpunit
that's if the command
phpunit
returns "No tests executed!"
while on Mac
./vendor/bin/phpunit
Hope it helps.
回答4:
I had the same problem after PHPUnit on our virtual machines updated to version 6. Even --debug and --verbose said nothing useful, just "No tests executed". In the end it turned out that classes and namespaces were changed in the new version and it just didn't want to execute the files that contained references to old classes. The fix for me was just to replace in every test case this:
class MyTestCase extends \PHPUnit_Framework_TestCase {...}
with:
use PHPUnit\Framework\TestCase;
class MyTestCase extends TestCase {...}
回答5:
You Need Just To Call It From Vendor File
vendor\bin\phpunit
Notice \ Not /
回答6:
I realize this is super old, but it just happened to me too. Hopefully this will help someone.
My problem was that I forgot the '@' symbol in /** @test */
WRONG:
/** test */
function a_thread_can_be_deleted()
{
...
}
RIGHT:
/** @test */
function a_thread_can_be_deleted()
{
...
}
回答7:
I pulled my hair for 10 minutes before i decided to use --debug (good way to go by the way) to discover the simple fact that file name didn't respect the naming convention, i had an extra "s" at the end.
wrong
CreateAdminTests
right
CreateAdminTest
hope this note could help for someone
回答8:
if you are using PHPSTORM go to Settings then goto
Test Frameworks
and click + and choose
PHPUnit Local
thenUse Composer Auto Loader
then paste this like in path to script fieldC:\{YOUR PROJECT NAME}\vendor\autoload.php
- click OK
HAPPY TESTING
回答9:
Have you added a test suite to you phpunit.xml file?
<phpunit>
<testsuite name="app1" >
<directory>./</directory>
</testsuite>
</phpunit>
You can add multiple directories in there.
回答10:
Check phpunit.xml file, look inside testsuites.
My version of phpunit (2019) is looking for files ending (suffix) *Test.php . So, make sure all the test files are named properly (ex.: BookTest.php is correct, BookTests.php is not, BookTestCase.php is not).
回答11:
For me, using phpunit --debug showed me which test it was not executing, inside, I had
$this->visit('/')
->see('Laravel');
and I think because the directory was protected with .htaccess authentication, it could not get through to visit the page
The solution for me was to take out this test (or most likely take out .htaccess authentication)
回答12:
This is very late but I hope it helps someone.
I got my tests to run by using an absolute reference. folder structure [ project/tests/test.php]
my directory line looked like this ./tests/test.php
回答13:
A little bit on the side maybe, but if you are (like me) using Laravel in Vagrant, make sure you are running phpunit inside of the vagrant box and not on the "windows side". :)
回答14:
I had the issue of no tests being executed, even when things were set up fine.
The cause was the namespace was not the first command of the file, it was after some doc-block comments.
reverting caused phpunit to see the tests and run correctly.
回答15:
Mine was a bit funny.
When I used php artisan make:test
I accidentally put .php like ProductRewardPointController.php
which created ProductRewardPointController.php.php
and phpunit simply ignored it.
I just delete the extra .php and things back to normal
回答16:
I had the same issue of No tests executed!, solved by keeping the same name of file and class name.
回答17:
If you are using IDEs like JetBrains PHPStorm, please also notice that: in the Run/Debug Configurations
window, the Test scope
needs to be set to directory
and point that directory to where your tests
folder located.
It just took me half an hour to figure out I forgot to set the directory. You can use global phpunit.phar
as long as you set test scope and the directory correctly, the IDE will handle other stuff for you.
回答18:
using de cmd console resolved this problem passing the enterely path Test realized
I did't find another way to do it It does not work from this way
I hope this was helpful for someone
来源:https://stackoverflow.com/questions/29299206/phpunit-no-tests-executed-when-using-configuration-file