Let's say I pass the following command to PHPUnit:
phpunit tests --coverage-clover build/logs/clover.xml
Where I can go to see the actual clover.xml file? I've looked everywhere, but I can't find it. I also can't find a /build/logs directory.
My tests run fine, as I see the following output: Tests: 1, Assertions: 1, Failures: 1.
The path you're passing: build/logs/clover.xml is relative to the current dir when you execute phpunit (because it does not start with / ).
If you want the path to be /build/logs/clover.xml, just add the slash to the command parameter:
phpunit tests --coverage-clover /build/logs/clover.xml
Related
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
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.
I have already posted this question on php unit first test symfony
I installed phpunit via the composer as a per project installation.
When trying vendor/bin>phpunit -c ../../app every thing is ok and I get a positive answer.
Whereas this command give the answer to all the tests in the tests directory.
But I want the result to every test alone.
When trying /vendor/bin>phpunit -c ../../src/xxx/Bundle/tests/entity/yyy.php and I get the following message : could not load c:\wamp\www\symfony\src/xxx/Bundle/tests/entity/yyy.php Parse PI : PI php never end ... Start ttag expected, '<' not found
and when trying /vendor/bin>phpunit -c ../../src/xxx/Bundle/tests/entity/yyy and I get the following message : could not read "..\..\src/xxx/Bundle/tests/entity/yyy"
Could anybody help me to know how should I write the command and from where execute it???
Any ideas???
Don't use the -c option here. The -c option is a shortcut for --configuration and it points to the directory of a PHPunit configuration file (like app/phpunit.xml.dist). That configuration tells PHPunit where to look for the test classes and some other configuration, like the bootstrap file.
If you want to run tests for a specific test, you can do it like phpunit path/to/tests/MyTest.php. But you'll loose the autoloading then. To get that back, you can use the --bootstrap option to point to the bootstrap file. So it'll be phpunit --bootstrap vendor/autoload.php path/to/tests/MyTest.php.
If you want to run this command more often, you can better edit the app/phpunit.xml.dist file and create a new suite:
<?xml version="1.0" encoding="utf-8" ?>
<phpunit ...>
<!-- ... -->
<testsuites>
<testsuite name="MyBundle">
<file>path/to/tests/MyTest.php</file>
</testsuite>
</testsuites>
<!-- ... -->
</phpunit>
And then run: phpunit -c app --testsuite MyBundle
Happened with me too, but in fact we are misreading the documentation you are forgetting 'app' in the command line look:
phpunit -c app src/AppBundle/Tests/Util/CalculatorTest.php
Note the app parameter in command sentence.
Is there a way to automatically load a Listener (or phpunit configuration file for that matter), without using no more than:
phpunit testdir
? Today I'm using:
phpunit -c phpunit.xml --bootstrap bootstrap.php testdir
and want to exclude all switches. I know that I could have a phpunit.xml file in every directory, but thats not an option..
Thanks in advance!
PHPUnit by itself should look in the current directory for a phpunit.xml file. So your first and second example should both find and include the same phpunit.xml.
Use a Makefile:
unittest:
phpunit -c phpunit.xml --bootstrap bootstrap.php testdir
And simply call it with make unittest
Now you can add more for different cases, in the same pattern.
I have created my own codesniffer rules standards.They are working fine. Now I want to test codesniffer rules using PHP UnitTest. PhpCodesniffer has already have their framework for PHPUnit test case.
So using that I have extended their AbstractSniffUnitTest and unit test class at location
Standards/TestRules/Tests/Function/FunctionUnitTest.php and script on which is to be tested kept in Standards/TestRules/Tests/Function/FunctionUnitTest.inc.
But when I am going to run the script by command phpunit PEAR\PHP\tests\AllTests.php, it gives following error.
PHPUnit 3.5.14 by Sebastian Bergmann.
......................................F
Time: 6 seconds, Memory: 10.00Mb
There was 1 failure:
1) TestRules_Tests_Function_FunctionUnitTest::getErrorList
An unexpected exception has been caught: Source file C:/Program Files/PHP/PEAR/PHP/CodeSniffer/Standards/TestRules/Tests/Function/FunctionUnitTest.inc does not exist
C:\Program Files\PHP\PEAR\PHP\tests\Standards\AbstractSniffUnitTest.php:138
C:\Program Files\PHP\PEAR\PHP\tests\TestSuite.php:48
FAILURES!
Tests: 39, Assertions: 146, Failures: 1.
Warning: Deprecated PHPUnit features are being used 2 times!
Use --verbose for more information.
It gives error file FunctionUnitTest.inc not found at given location. I have given full permission to folder also verify the path and file location, but it gives same error. I have tested it on linux machine also, but it gives same error.
Is it a my code issue or codesniffer unittest framework issue?
Are your files in the top-level tests or CodeSniffer directory? Note that the abstract test case you're extending is in tests
C:\Program Files\PHP\PEAR\PHP\tests\Standards\AbstractSniffUnitTest.php
but is trying to include the file from CodeSniffer:
C:/Program Files/PHP/PEAR/PHP/CodeSniffer/Standards/TestRules/Tests/Function/FunctionUnitTest.inc
Try moving your two files to the other directory.
I see a space near i i nc - make sure if that is mis-typo and correct it, it should work. Other than this I don't see any problem.
Change:
C:/Program Files/PHP/PEAR/PHP/CodeSniffer/Standards/TestRules/Tests/Function/FunctionUnitTest.i nc
To
C:/Program Files/PHP/PEAR/PHP/CodeSniffer/Standards/TestRules/Tests/Function/FunctionUnitTest.inc
It seems that it has path issue on windows. I tested the test cases on Linux, it works fine. When I checked the actual code, it seems that on there is file mapping from test case file to sniff file. But on window, after file being mapped , the file path contains mixing of forward & backward slahes.But codesniffer not going to allow this. Because of that it is returning that “file is not found error”. Test cases are executing fine on Linux.