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.
phpunit.xml
to tests/GeneralStringFunctions
phpunit.xml
to ./tests
phpunit.xml
file to the tests
directory and then changing the directory to be ./
instead of tests
.phpunit.xml
to specify "Tests" as the explicit suffix.How to Run Tests in PHPUnit. You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status.
Installing PHPunit on Windows Installing PHPunit basically requires downloading one single . phar file, which can be executed with PHP. You can find it on the PHPunit Getting Started section of their page, or simply save the following link: https://phar.phpunit.de/phpunit.phar.
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!
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 either have a name that starts with "test" OR an @test
annotation:
public function testFooBar() { // Your test code }
or:
/** * @test */ public function fooBarTest() { // test code here }
Hope that helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With