PHPUnit testing individually in Symfony - phpunit

I would like to run some unit Tests individually with PHPUnit, but I have certain classes separated from the Tests, since I am using the symfony framework, and I group the Tests and the Classes in different folders.
I would like to run the Tests individually like this:
php phpunit.phar MyTest.php
The problem is that the test file uses the classes from the controllers, and phpunit doesnt seem to be able to import the needed classes for the test.
This is not a problem to run all the tests together, thanks to phpunit.xml but when I want to run them individualy, its a problem.
How could I fix this?

You have to point phpunit where you have your phpunit.xml config file (because it must know the autoloader for example). If you have default symfony 2 structure it will be in app directory, so just run your test like that (I assume that you are in project root path):
phpunit -c app/ --filter="concreteTestPattern" src/Acme/DemoBundle/Tests/MyTest.php
edit:
Above will run all tests which names match to the pattern: /.*concreteTestPattern.*/

You would use the --filter argument in your PHPUnit command string. This will only run tests that match the pattern given. If you pass only the complete name of the test that you want run, phpunit should only run that test.
If you have a data provider associated with the test and only want to run one test case, you can also filter that by using --filter <testName>::<testcase name>

PHPUnit can be set to execute using a configuration file.
In our Symfony2 project this file is located at app/phpunit.xml.dist.
As this file is suffixed with .dist, you need to copy its contents into a file called app/phpunit.xml.
If you are using a VCS such as Git, you should add the new app/phpunit.xml file to the VCS ignore list.
You will also notice the configuration is specifying the bootstrap file located at app/bootstrap.php.cache. This file is used by PHPUnit to get the testing environment setup.
We can execute this test by running the following command from the root directory of the project. The -c option specifies that PHPUnit should load its configuration from the app directory.
$ phpunit -c app

Related

Grouping phpunit tests. Launching test from specified directory

I have a directory tests where I store all my tests.
Hierarchy of tests dirrectory
tests->
ApplicationTests->
IntegrationTests->
Factory->
Service->
UnitTests->
How can I make phpunit launch tests only from for example Service directory, instead of the whole tests directory? I read about #Groups, but I think that's not what I'm looking for and it would be best if I would not need to edit config files, but some how, do it from the command line.
Found a quite easy way, even with config editing.
phpunit.xml
See: The XML Configuration File Phpunit Docs and Organizing Tests Phpunit Docs<
<testsuite name="Service">
<directory>tests/IntegrationTests/Service</directory>
</testsuite>
Command line:
php ./vendor/bin/phpunit --testdox --testsuite Service

PhpStorm doesn't get the folder's configuration when running a single test method

I have a Laravel project with three test directories - Browser, Feature, and Unit.
In Run/Debug configurations I've configured the Feature and the Unit directories to use phpunit.xml while the Browser directory uses phpunit.e2e.xml. I've also created a default configuration for all tests which uses the phpunit.xml.
If I open a class that is in the Feature/Unit directories and run a single test method or the whole class, it picks the default configuration, but when I run a single test method in the Browser directory, it uses phpunit.xml instead of phpunit.e2e.xml.
If I select a directory and run tests, it picks the proper configuration, but when I open a class in the Browser directory, it doesn't
Why doesn't PhpStorm use the configuration of the Browser directory recursively? How can I handle this problem?
As far as I know PhpStorm has never had this functionality and I wouldn't count on them adding it either. It seems way too specific.
What you can do though, is specify the configuration file you want to use through the commandline.
See: https://phpunit.de/manual/6.5/en/textui.html#textui.clioptions
So your command would look something like this:
phpunit tests\Unit\SomeTest.php --configuration phpunit.xml
or
phpunit tests\Browser\SomeTest.php --configuration phpunit.e2e.xml

Jenkins build failing on namespaced class not found (but phpunit works from command line)

I have a WP project with structure:
public_html/ // wp-project stuff like wp-config, etc)
tests/ // phpunit test classes
When running in bash shell:
$phpunit /tests
all the the tests pass just fine
But Jenkins is failing on on one of the plugins classes (which is namespaced) not found.
Not sure why this is happening (or how to fix) ?

How to save Robot framework test run logs in some folder with timestamp?

I am using Robot Framework, to run 50 Testcases. Everytime its creating following three files as expected:
c:\users\<user>\appdata\local\output.xml
c:\users\<user>\appdata\local\log.html
c:\users\<user>\appdata\local\report.html
But when I run same robot file, these files will be removed and New log files will be created.
I want to keep all previous run logs to refer in future. Log files should be saved in a folder with a time-stamp value in that.
NOTE: I am running robot file from command prompt (pybot test.robot). NOT from RIDE.
Could any one guide me on this?
Using the built-in features of robot
The robot framework user guide has a section titled Timestamping output files which describes how to do this.
From the documentation:
All output files listed in this section can be automatically timestamped with the option --timestampoutputs (-T). When this option is used, a timestamp in the format YYYYMMDD-hhmmss is placed between the extension and the base name of each file. The example below would, for example, create such output files as output-20080604-163225.xml and mylog-20080604-163225.html:
robot --timestampoutputs --log mylog.html --report NONE tests.robot
To specify a folder, this too is documented in the user guide, in the section Output Directory, under Different Output Files:
...The default output directory is the directory where the execution is started from, but it can be altered with the --outputdir (-d) option. The path set with this option is, again, relative to the execution directory, but can naturally be given also as an absolute path...
Using a helper script
You can write a script (in python, bash, powershell, etc) that performs two duties:
launches pybot with all the options you wan
renames the output files
You then just use this helper script instead of calling pybot directly.
I'm having trouble working out how to create a timestamped directory at the end of the execution. This is my script it timestamps the files, but I don't really want that, just the default file names inside a timestamped directory after each execution?
CALL "C:\Python27\Scripts\robot.bat" --variable BROWSER:IE --outputdir C:\robot\ --timestampoutputs --name "Robot Execution" Tests\test1.robot
You may use the directory creation for output files using the timestamp, like I explain in RIDE FAQ
This would be in your case:
-d ./%date:~-4,4%%date:~-10,2%%date:~-7,2%
User can update the default output folder of the robot framework in the pycharm IDE by updating the value for the key "OutputDir" in the Settings.py file present in the folder mentioned below.
..ProjectDirectory\venv\Lib\site-packages\robot\conf\settings.py
Update the 'outputdir' key value in the cli_opts dictionary to "str(os.getcwd()) + "//Results//Report" + datetime.datetime.now().strftime("%d%b%Y_%H%M%S")" of class _BaseSettings(object):
_cli_opts = {
# Update the abspath('.') to the required folder path.
# 'OutputDir' : ('outputdir', abspath('.')),
'OutputDir' : ('outputdir', str(os.getcwd()) + "//Results//Report_" + datetime.datetime.now().strftime("%d%b%Y_%H%M%S") + "//"),
'Report' : ('report', 'report.html'),

PHPUnit, Netbeans and Symfony2 and wrong tests location

I try to understand PHPUnit tests in Netbeans. I set correct phpunit.bat path and the phpunit-skelgen.bat file as well.
When I try to create test for some file, using the netbeans option such us "create test file" I get correct directory structure, but the files are placed incorrectly.
Next, following the tutorials from web I set the test folder (to jobeet/tests), the phpunit bootstrap to jobeet\app\bootstrap.php.cache and the xml configuration file to jobeet\app\phpunit.xml.dist
Please see the screenshots below

Resources