I'm been following a PHPUnit tutorial for the first time and my tests run fine locally. However, when running my tests on Travis CI, no tests are executed and my build exits with 0.
My directory structure and full code can be seen on the repo.
Build log from Travis CI (Full build log)
1.51s$ curl -s http://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...
Composer successfully installed to: /home/travis/build/idavidmcdonald/phpunit-tutorial/composer.phar
Use it: php composer.phar
before_script.2
0.33s$ php composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
0.08s$ vendor/bin/phpunit --debug
PHPUnit 3.7.14 by Sebastian Bergmann.
Configuration read from /home/travis/build/idavidmcdonald/phpunit-tutorial/phpunit.xml
Time: 13 ms, Memory: 2.00Mb
No tests executed!
The command "vendor/bin/phpunit --debug" exited with 0.
Done. Your build exited with 0.
phpunit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Application Test Suite">
<directory>phpunittutorial/tests/</directory>
</testsuite>
</testsuites>
</phpunit>
.travis.yml:
language: php
php:
- 5.4
- 5.5
before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install
script: vendor/bin/phpunit --debug
My tests run successfully locally, however maybe there is an issue somewhere with my phpunit.xml file?
The directory containing your tests is incorrect.
The correct path would be phpUnitTutorial/tests. Note that Windows does not care about case sensitivity, but everyone else in the world does. Best thing would be to always use lower case for paths, or double check you are using the correct case everywhere (PSR-0 and PSR-4 would require path names with the same case as the class name, which will usually include upper case letters).
And by the way: You should probably upgrade to a more recent version of PHPUnit. That old 3.7 series is not getting any more updates for years now, and the transition to 4.x isn't too steep - you should just do it.
language: php
php:
- 5.4
- 5.5
install: composer install
script: ./vendor/bin/phpunit
Not sure about install: composer install, probably can be omitted
Related
I've created a simple PHP library using PHP 5.6.
It includes PHPUnit test cases and I succeeded in creating the package.
Then I found that I can install these packages directly from GitHub, using composer, in other projects also. I can run tests independently at the development stage. But once I publish the package I can't run the tests, as it is not properly finding the autoloader files.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader">
<testsuites>
<testsuite>
<directory>tests</directory>
</testsuite>
</testsuites>
I've added the xml files in root directory. The path of the autoload file is correct when it is independent. But the directory structure will be a different one once it is installed.
The problem still persist if we use require_once the autoloader file, as the directory structure change after installation.
Questions:
Can we test our package after installing it into any frameworks (laravel/Symfony) or in any other projects?
What is the best practice? Is the test needed for the developers who is using the package?
If yes, then any solution to solve this? Is there any other method to autoload problem in both environments?
Repo in Github
I would say you're best to look at the other repositories and see if it works or not.
For me
cd vendor/phpunit/phpunit && composer install && phpunit => worked
cd doctrine/collections/ && composer install && phpunit => worked
So it seems that it should be working for you.
If you look at the phpunit.xml.dist for the other vendors they use:
bootstrap="./tests/Doctrine/Tests/TestInit.php" (doctrine)
bootstrap="tests/bootstrap.php" (phpunit)
It looks like your file isn't so different. Are you sure you ran composer install from inside your packages directory inside the vendor folder?
Update:
I added your repository as a dependency to a default Symfony installation.
"repositories": [
{
"url": "https://github.com/jerintk/Validator.git",
"type": "git"
}
],
And in the require block:
"Jthedev/Validators": "dev-master"
I then ran
composer update
It ran fine. You need to run composer update and check in the new composer.lock because it's out of date.
From there I cd'ed into the directory for your repo.
cd vendor/Jthedev/Validators
I then ran
composer install
and
vendor/phpunit/phpunit/phpunit
and got
OK (2 tests, 2 assertions)
Update Two
(since this was too long for a comment)
#JTheDev composer update adds your dependencies for the laravel project, but it doesn't create the vendor folder inside your vendor/your-project directory. If composer installed all dependencies separately for each project, like:
vendor/
my-project/
vendor/
dependency-A
dependency-C
another-library/
vendor/
dependency-A
dependency-B
it would waste a lot of space and bandwidth. Instead composer gets all the dependencies and installs them in separate folders and they work with each other because composer loads them all using vendor/autoload.php for that project. What you're talking about is creating the vendor folder inside vendor/your-project. This is not normal - usually you only have to run tests when developing yourself, but anyway your question is about how to run your tests for your project when it's added as a dependency for another project.
To do this you need to run composer install inside the vendor/your-project directory, that means:
cd vendor/Jthedev/Validators && composer install && vendor/phpunit/phpunit/phpunit
Final update (hopefully)
From chat:
The autoloader is only generated when you run composer install inside your project directory. You are correct, the vendor folder should not be there usually but you need it if you want to do what you are trying to do. It is not conventional. Normally developers run their tests inside their project root folder, not on the dependencies, but your questions was "how can I run my tests when it is a dependency". The answer is you need to create the vendor and autoload files inside your project folder
The tests only run if you run composer install inside the project folder. But it's not a problem if the tests don't run without doing that.
As given in https://phpunit.de/manual/current/en/installation.html#installation.phar.verification, the steps to globally install the PHAR are:
$ wget https://phar.phpunit.de/phpunit.phar
$ chmod +x phpunit.phar
$ sudo mv phpunit.phar /usr/local/bin/phpunit
$ phpunit --version
PHPUnit x.y.z by Sebastian Bergmann and contributors.
I followed the above, but with the URL for the older version, i.e. https://phar.phpunit.de/phpunit-old.phar (because our PHP version is older). I ran the following commands then -
$ chmod +x phpunit-old.phar
$ sudo mv phpunit-old.phar /usr/local/bin/phpunit
Note - My PHP version is 5.3.29. and the Old Stable Release section in phpunit.de says PHPUnit 4.8 is supported on PHP 5.3, PHP 5.4, PHP 5.5, and PHP 5.6.
Looks fine till here. But, running phpunit --version gives -
PHP Fatal error: require(): Cannot redeclare class phpunit_extensions_database_constraint_tableisequal in /usr/local/bin/phpunit on line 109
zend_mm_heap corrupted
So, instead of moving the .phar to /usr/local/bin/ (in the step 3), I was managing so far by running this -
$ php phpunit-old.phar –-version
I was also able to run my unit test cases in this way -
php /home/sandeepan/phpunit-old.phar /var/cake_1.2.0.6311-beta/app/webroot/openx/lib/ad_agencies/unittests/Admarvel_generic_network_test.php
But, now I need to integrate phpunit with phing. I would like to use the basic utilities provided by PHPUnitTask of phing. So, I guess it needs phpunit phar to be globally installed.
I tried my luck by writing the following -
<phpunit haltonfailure="true" haltonerror="true"
pharlocation="/home/sandeepan/phpunit-old">
<formatter type="plain" usefile="false" />
<batchtest>
<fileset dir="${dir.scratchpad}/${dir.subdir}/unittests">
<include name="**/*_test.php"/>
</fileset>
</batchtest>
</phpunit>
But I get this error -
BUILD FAILED
...
: PHPUnitTask requires PHPUnit to be installed
Update
With reference to stackoverflow.com/a/23410676/351903, I tried with this older version of Phpunit, i.e. PHPUnit-3.7.35. Now, phpunit --version command works. But I still have no success using the PHPUnitTask of Phing. Still getting PHPUnitTask requires PHPUnit to be installed error.
Update 2
The solution which worked for me was using PHPUnit 3.7.35. It seems there is some compatibility issue of phing with PHPUnit 4.8.
I have just downloaded the old PHPUnit file you provided from the phpunit.de website, and have applied succesfully the commands you wrote. Running phpunit worked like a charm in my case.
Perhaps there is an incompatibility between your PHP version and the PHPUnit version you downloaded?
I cannot reproduce the issue you describe:
$ wget https://phar.phpunit.de/phpunit-old.phar
--2016-04-07 09:47:35-- https://phar.phpunit.de/phpunit-old.phar
Resolving phar.phpunit.de (phar.phpunit.de)... 188.94.27.25
Connecting to phar.phpunit.de (phar.phpunit.de)|188.94.27.25|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://phar.phpunit.de/phpunit-4.8.24.phar [following]
--2016-04-07 09:47:35-- https://phar.phpunit.de/phpunit-4.8.24.phar
Reusing existing connection to phar.phpunit.de:443.
HTTP request sent, awaiting response... 200 OK
Length: 3086772 (2.9M) [application/octet-stream]
Saving to: ‘phpunit-old.phar’
phpunit-old.phar 100%[=====================================================================================================================================>] 2.94M 3.97MB/s in 0.7s
2016-04-07 09:47:36 (3.97 MB/s) - ‘phpunit-old.phar’ saved [3086772/3086772]
$ php phpunit-old.phar --version
PHPUnit 4.8.24 by Sebastian Bergmann and contributors.
The solution which worked for me was using PHPUnit 3.7.35. It seems there is some compatibility issue of phing with PHPUnit 4.8.
Source - Phing can't see PHPUnit
I am encountering a strange issue while trying to run PHP unit tests on Travis CI.
.travis.yml
sudo: false
language: php
php:
- 5.4
env:
- VUFIND_HOME=$PWD VUFIND_LOCAL_DIR=$PWD/local
before_script:
- pear install pear/PHP_CodeSniffer
- pear channel-discover pear.phing.info
- pear install phing/phing
- composer global require fabpot/php-cs-fixer
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- phpenv rehash
script:
- phpunit --stderr --configuration module/VuFind/tests/phpunit.xml
- phpunit --stderr --configuration module/Swissbib/tests/phpunit.xml
- phpcs --standard=PEAR --ignore=*/config/*,*/tests/* --extensions=php $PWD/module
- phing php-cs-fixer-dryrun
module/VuFind/tests/phpunit.xml is a third party framework
module/Swissbib/tests/phpunit.xml is our own code
module/Swissbib/tests/phpunit.xml
<phpunit bootstrap="Bootstrap.php">
<testsuites>
<testsuite name="sbvfrd">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
The tests from the third party framework run without errors. Our own tests do not work and we get the error message:
$ phpunit --stderr --configuration module/Swissbib/tests/phpunit.xml
Could not read "module/Swissbib/tests/phpunit.xml".
Locally (Mac OS X) all the tests run through. Strangely enough the Bootstrap.php defined in module/Swissbib/tests/phpunit.xml runs completely through on Travis CI, I verified this using echo statements. Nevertheless phpunit tells us that it could not read phpunit.xml.
Travis: https://travis-ci.org/swissbib/vufind
Repo: https://github.com/swissbib/vufind (development branch)
Any ideas what could be going wrong?
I found the solution by downloading the phpunit source and debugging with it.
We were changing the directory within the Bootstrap.php file to a different location then the phpunit command was run from. We run the phpunit command from our project root folder and then changed the working directory to the tests folder, because we were using relative paths. I changed everything to absolute paths (using __DIR__) so we do not have to change the working directory anymore.
Bottom line: Do not change the directory in the bootstrap file as it causes phpunit to fail with this error message: Could not read phpunit.xml.
Whenever I run phpunit tests from PHPStorm I get an error. I have provided more info below. I am not sure where I have miss configured the setup.
My Setup
Ubuntu
PHPStorm 8.0.1
PHPUnit 4.3.4
More Info:
PHPUnit.phar is located at /usr/local/bin/phpunit.phar. I have setup PHPUnit path directly in PHPStorm. Tests run from bash with no issues. I have also setup my configuration file phpunit.xml in PHPUnit, which is located in the root of my project. The phpunit.xml file tells phpunit to load the composer autoload.php file.
PHPUnit Output:
/usr/bin/php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 /tmp/ide-phpunit.php --configuration /home/mkelley/projects/CompanyName/phpunit.xml
Testing started at 10:33 AM ...
PHPUnit 4.3.4 by Sebastian Bergmann.
Configuration read from /home/mkelley/projects/CompanyName/phpunit.xml
PHP Fatal error: Call to undefined method CompanyNameTests\Boundaries\BoardMemberVotingBoundaryTest::hasExpectationOnOutput() in phar:///usr/local/bin/phpunit.phar/phpunit/TextUI/ResultPrinter.php on line 545
PHP Stack trace:
PHP 1. {main}() /tmp/ide-phpunit.php:0
PHP 2. IDE_Base_PHPUnit_TextUI_Command::main($exit = *uninitialized*) /tmp/ide-phpunit.php:500
PHP 3. PHPUnit_TextUI_Command->run($argv = *uninitialized*, $exit = *uninitialized*) /tmp/ide-phpunit.php:243
PHP 4. PHPUnit_TextUI_TestRunner->doRun($suite = *uninitialized*, $arguments = *uninitialized*) phar:///usr/local/bin/phpunit.phar/phpunit/TextUI/Command.php:186
PHP 5. PHPUnit_Framework_TestSuite->run($result = *uninitialized*) /home/mkelley/projects/CompanName/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:423
PHP 6. PHPUnit_Framework_TestSuite->run($result = *uninitialized*) /home/mkelley/projects/CompanName/vendor/phpunit/phpunit/src/Framework/TestSuite.php:703
PHP 7. PHPUnit_Framework_TestCase->run($result = *uninitialized*) /home/mkelley/projects/CompanName/vendor/phpunit/phpunit/src/Framework/TestSuite.php:703
PHP 8. PHPUnit_Framework_TestResult->run($test = *uninitialized*) /home/mkelley/projects/CompanName/vendor/phpunit/phpunit/src/Framework/TestCase.php:771
PHP 9. PHPUnit_Framework_TestResult->endTest($test = *uninitialized*, $time = *uninitialized*) /home/mkelley/projects/CompanName/vendor/phpunit/phpunit/src/Framework/TestResult.php:760
PHP 10. PHPUnit_TextUI_ResultPrinter->endTest($test = *uninitialized*, $time = *uninitialized*) /home/mkelley/projects/CompanyName/vendor/phpunit/phpunit/src/Framework/TestResult.php:378
Process finished with exit code 255
I have searched Google and was unable to find a similar issue. I appreciate any help!
EDIT
Here is my phpunit.xml file. PHPStorm is using this as a "Use alternative configuration file"
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
bootstrap="./vendor/autoload.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
This appears to be the autoloading issue. When you bootstrap your app for the test suite you must initialise your autoloader, which doesn't seem to be happening, as something doesn't get found. The easiest way would be to use Composer to manage the PHPUnit dependency and autoload your classes via the autoload directive. See the the psr-4 part in documentation.
Then in your PhpStorm PHPUnit configuration window select Use custom autoloader and specify path to your vendor/autoload.php script.
Sometimes is better an image...
As you can see, you can also use your phpunit.phar file.
I will answer my own question in case someone else comes across this issue.
The issue was autoloading PHPUnit via composer and using phpunit.phar. Once I removed the phpunit dependence from composer PHPStorm was able to successfully run all my tests.
The problem isn't that you autoloading phpunit via composer, but that in the composer you use an old version of phpUnit. In my case instead of using 4.0.0 I updated to 4.6.*.
I've been having this same issue with composer and found using the .phar didnt have any issues. Today I've just realised slaps forehead it was just caused by installing phpunit via composer and then not reindexing the vendor folder.
I haven't found that I've had this issue previously when installing new packages with composer but for some reason when installing phpunit it hadn't reindexed the vendor folder causing inconsistencies.
Reindex, everything working normally.
I'm trying to setup a new Continuous Integration server that utilizes Phing and PHPUnit for automatically running test cases.
I've installed Phing with Pear:
pear channel-discover pear.phing.info
pear install phing/phing
I've installed PHPUnit using the new PHAR method:
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit
Then I go to the directory where my build.xml file is located, rung phing and it looks like Phing has no idea where PHPUnit is.
[jenkins#leroy workspace]$ phing
Buildfile: /home/jenkins/.jenkins/jobs/Framework/workspace/build.xml
Framework > test:
[echo] Running Tests
Execution of target "test" failed for the following reason: /home/jenkins/.jenkins/jobs/Framework/workspace/build.xml:9:37: /home/jenkins/.jenkins/jobs/Framework/workspace/build.xml:9:37: PHPUnitTask requires PHPUnit to be installed
BUILD FAILED
/home/jenkins/.jenkins/jobs/Framework/workspace/build.xml:9:37:
/home/jenkins/.jenkins/jobs/Framework/workspace/build.xml:9:37: PHPUnitTask requires PHPUnit to be installed
Total time: 0.0764 seconds
[jenkins#leroy workspace]$
Checked the location of Phing and PHPUnit:
[jenkins#leroy workspace]$ which phing
/usr/bin/phing
[jenkins#leroy workspace]$ which phpunit
/usr/local/bin/phpunit
And, making sure PHP is new enough:
[jenkins#leroy workspace]$ php -v
PHP 5.3.3 (cli) (built: Dec 11 2013 03:29:57)
I'm doing all of this because we're replacing an older Jenkins server with this new one. Trying to use the newest software, but I can't figure out how to tell Phing where to find PHPUnit.
Thanks in advance for any help!
David
Seems like adding the pharlocation attribute to the phpunit task element, pointing to your local phpunit executable, does the job:
<phpunit haltonfailure="true" haltonerror="true"
bootstrap="./fw_init.php"
pharlocation="/usr/local/bin/phpunit">
<formatter type="plain" usefile="false" />
...
</phpunit>
It looks like there are some actual issues with phing and the latest 4.x versions of PHPUnit: http://www.phing.info/trac/ticket/1091.
So to fix the issue, I removed PHPUnit 4 and specified an older version:
pear install phpunit/PHPUnit-3.7.35
Phing and PHPUnit worked immediately at this point.
Here is a guy with exactly the same problem: How do I tell Phing where PHPUnit is installed?
His theory is that:
PHP version on this machine could be too old and not be able to handle PHAR archives
I can't comment if he is right or not, but that question received no replies from anyone other than the author
Maybe a silly remark, but do you have /usr/local/bin/phpunit in your PHP include path?
I get the same error when I remove the location of phpunit.phar from my include path.