The CLI output from a phpunit test contains useful information, such as:
PHPUnit 3.6.4 by Sebastian Bergmann.
F
Time: 7 seconds, Memory: 5.50Mb
There was 1 failure:
1) testHandler::testItem
Failed asserting that page text contains <itest New Family Name>.
Driver.php:632
testHandler.php:29
S163.php:18
testHandler.php:46
FAILURES!
Tests: 1, Assertions: 97, Failures: 1.
I'd like to send this information to my test tracking software by way of a POST through php curl.
However I can't find a way of catching the information in my test class (which extends PHPUnit_Framework_TestCase).
Any help / thoughts would be appreciated.
The easiest way if you have a custom build solution seems to be:
phpunit | curl -X http://url
or
phpunit 2>&1 > output.txt && cat output.txt | curl -X http://url
or something like that.
If you are using a proper Continuous Integration server look into the --log-junit and --coverage-clover or other output formats like --log-json.
I don't see any reason why you would want to get the output programmatically while the test are running. It just seems to be a lot more work than gain. There are ways to extend PHPUnit to integrate it with bugtrackers though. Have a look at the PHPUnit_Framework_TestListener.
Related
Can someone help me understand why my environment variables aren't being read inside of my phpunit test from travis.ci?
So I'm attempting to write some automatic testing using travis for a php/javascript app I'm working on. However when I wrote a test to check for environment variables reading into phpunit from travis, they fail. That means (as far as I can tell) that either the environment variables aren't able to be read by phpunit, or they aren't being passed through to the travis test properly.
.travis.yml
language: php
php:
- '7.0'
- '7.1'
before_install:
- echo "extension=ldap.so" >>php --ini | grep "Loaded Configuration" | sed -e "s|.:\s||"``
install:
- cd test
- npm install
- cd ..
script:
- echo $API_BASE_URL
- phpunit test/build_tests.php
notifications:
on_success: never
on_failure: never
phpunit Test File
<?php
use PHPUnit\Framework\TestCase;
class build_tests extends TestCase
{
public function testForEnv()
{
$this->assertEquals(isset($_ENV['API_BASE_URL']), true);
$this->assertEquals(isset($_ENV['DRINK_SERVER_URL']), true);
$this->assertEquals(isset($_ENV['LOCAL_DRINK_SERVER_URL']), true);
$this->assertEquals(isset($_ENV['RATE_LIMIT_DROPS_DROP']), true);
$this->assertEquals(isset($_ENV['DEBUG']), true);
$this->assertEquals(isset($_ENV['DEBUG_USER_UID']), true);
$this->assertEquals(isset($_ENV['DEBUG_USER_CN']), true);
$this->assertEquals(isset($_ENV['USE_LOCAL_DRINK_SERVER']), true);
}
}
?>
travis Exporting of Environment Variables
$ Setting environment variables from repository settings
$ export DRINK_SERVER_URL=https://drink.csh.rit.edu:8080
$ export LOCAL_DRINK_SERVER_URL=http://localhost:3000
$ export RATE_LIMIT_DROPS_DROP=3
$ export DEBUG=true
$ export DEBUG_USER_UID=[secure]
$ export DEBUG_USER_CN=[secure]
$ export USE_LOCAL_DRINK_SERVER=true
$ export API_BASE_URL='api/index.php?request='
phpunit Result
PHPUnit 6.1.1 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 260 ms, Memory: 6.00MB
There was 1 failure:
1) build_tests::testForEnv
Failed asserting that true matches expected false.
/home/travis/build/devinmatte/WebDrink-2.0/test/build_tests.php:9
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
Can someone help me understand why my environment variables aren't being read inside of my phpunit test? I would really appreciate it.
Try using getenv function call instead. In travis enviroment the $_ENV variables are not available
When running unit test by phpunit on console, it always displays the message I input in logfile:
This the sample file I will test:
Sample.php
public funtion testThis() {
CakeLog::write('error', "This is a test");
}
then when I run in console
$ ./Console/cake test app
I will get this result:
PHPUnit 3.7.38 by Sebastian Bergmann.
............This is a test..........50/50 (100%)
You need to check the configuration / configure the logger. My guess is that it doesn't have where to write, so is using STDOUT or STDERR for output.
Try to set up it with instructions from here (choose the appropriate Cake version).
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
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
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.