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.
Related
I followed the below tutorial step by step to run PHPUnit. But I am only getting options and configuration part.
https://www.youtube.com/watch?v=cRVkchE5kuE&t=393s
Followed the below steps to setup PHPUnit:-
Installed the composer.
Installed the PHPUnit via command line
PHP composer.phar require --dev phpunit/phpunit ^8
After these steps, I am able to get the version, options, configuration, and other information by using the command
PHPUnit (on path cd /vendor/bin/)
In the tutorial below steps used to run the PHPUnit:-
create phpunit.xml file on root and defined the below code.
<?xml version = "1.0" encoding = "UTF-8" ?>
<phpunit bootstrap = "vendor/autoload.php">
<testsuits>
<testsuit name = "unit">
<directory>tests</directory>
</testsuit>
</testsuits>
</phpunit>
Created the tests directory on the root and also create unit directory in the tests directory and again run the below command.
phpunit on path cd /vendor/bin/
But still, I am getting information part. but in the video, he is getting tested ok line after the command.
Please suggest me, what am I doing wrong?
This is the file structure
R:
vendor
bin
composer
doctrine
.
.
.
.
.
.
autoload.php
tests
unit
composer.json
composer.lock
composer.phar
phpunit.xml
Don't run phpunit from the /vendor/bin/ directory, run it from your project's root directory (the same place as the phpunit.xml file):
/project/dir # vendor/bin/phpunit
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
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.
Both Phing and PHPUnit were installed from pear on ubuntu 12.04.
PHPUnit is located at /usr/bin/phpunit and /usr/bin is on the unix path and also on the include_path in PHP.INI
How do I tell Phing where PHPUnit is installed?
webroot#gm3:~/med1/pub$ phing utest
Buildfile: /home/webroot/med1/pub/build.xml
myproject > utest:
[echo] Unittests PHPUnit...
Execution of target "utest" failed for the following reason: /home/webroot/med1/pub/build.xml:8:40: /home/webroot/med1/pub/build.xml:8:40: PHPUnitTask requires PHPUnit to be installed
BUILD FAILED
/home/webroot/med1/pub/build.xml:8:40: /home/webroot/med1/pub/build.xml:8:40: PHPUnitTask requires PHPUnit to be installed
Total time: 0.1148 seconds
Answering my own question, I think the PHP version on this machine could be too old and not be able to handle PHAR archives. Or maybe the PHAR configuration is broken. PHPUnit is installed as PHAR on this machine via PEAR.
A workaround for me was to not use <phpunit> but <exec> in the target, which worked:
<target name="test">
<exec command="phpunit --bootstrap tests/bootstrap.php --configuration tests/phpunit.xml">
...
I am not fixing this for now and I am expecting on newer machines it will work out of the box.
After a fresh symfony2 install i can run phpunit -c app/ and phpunit tests the included demo application: OK (1 test, 1 assertion).
But i receive no output (even with verbosity) when i run phpunit -c vendor/symfony/ as described here: http://symfony.com/doc/2.0/contributing/code/tests.html.
Does anyone know how to make this work?
PHPUnit: 3.6.2
PHP: 5.3.8
Symfony: 2.0.5
Testing twig, doctrine and other plugins works as expected (although doctrine tests fail for some reason).
If you have no output it's maybe because you configured php not to display errors.
You must install the vendors using the vendors.php script before lauching the Symfony test suite:
$ php vendor/symfony/vendors.php
Are you running the tests using the provided phpunit.xml.dist configuration file?
From your projects root directory:
$ phpunit --configuration app/phpunit.xml.dist vendor/symfony/tests
That should add an autoloader for vendors.