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.
Related
I'm trying to configure PhpStorm 2017.2 to use PhpUnit 5 for my PHP 5.6 project.
I've downloaded the phpunit-5.7.21.phar file from the official source and placed it in my PHP 5.6 installation dir.
In PhpStorm Settings >> Languages & Frameworks >> PHP >> Test Frameworks, I've linked to the .phar executable and set the default config file to a phpunit.xml in the project root directory
Here are the contents of phpunit.xml:
.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<testsuites>
<testsuite name="Test suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
I'm trying to structure tests in a tests/unit directory within which my source file project structure would be mirrored as described in the manual. For instance:
// project files:
ClassOne.php
vendor/
ClassTwo.php
Utility.php
// test files
tests/unit/
ClassOneTest.php
vendor/
ClassTwoTest.php
UtilityTest.php
I have two problems though:
First, I don't know how to configure PhpStorm to create tests within tests/unit/ mirroring the sructure with respect to the project root. When I create a test, by default the file is put in the same directory as the project file.
Secondly, I don't know how to get PhpStorm to index the PHPUnit source code. Even though I've linked to phpunit-5.7.21.phar file as shown above, the IDE complains when I create a test:
namespace vendor;
class UtilityTest extends \PHPUnit_Framework_TestCase{}
Undefined class PHPUnit_Framework_TestCase
Update 1
I solved the 2nd problem by adding the directory where I had saved the .phar to the PhpStorm include path, set in Settings >> Languages & Frameworks >> PHP >> Include Path tab. Alternatively, I could just put the .phar file within the project directory and it will be indexed.
I still need help with my first problem.
Update 2
Thanks to Ástþór's answer I figured out how to get PhpStorm to mirror the project structure within a dedicated tests directory. Go to PhpStorm Settings >> Directories and select the base testing directory. The click Test near the top to mark it as a Test Sources Root
The next time you create a test, it will automatically be placed in that directory.
Mark a directory with your source classes as "Source root" and a directory with tests as "Test Sources Root". After that directories will be pre-filled on a test creation (e.g. via ctrl+shift+T on a source class).
Not sure though if that would work fine with your "mirroring" system: I guess you would still have to manually adjust directories for the part of your tests
When running a test, my phpunit.xml is being loaded by PHPUnit. I can see it in the console as --bootstrap parameter, but it seems like it's not being executed?
In the xml I set bootstrap="bootstrap.php"
Is it normal that the xml file contents are echoed just below the line Testing started at... and just above PHPUnit 3.7.21 by Sebastian Bergmann.?
Any way in my bootstrap.php file I just put
die "bootstrap executed";
But never see that. What could be wrong?
Edit: This is the contents of phpunit.xml:
<phpunit bootstrap="./bootstrap.php">
</phpunit>
You need to launch the phpunit in one of the following example:
#for load a bootstrap php file (with full path and file extension)
>phpunit --bootstrap bootstrap.php
or
#for load with your xml configuration
>phpunit -c phpunit.xml
Hope this help
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
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.