All of my code base is in one folder and it's sub-directories, all of my tests are in another folder and it's sub-directories. When I run PHPUnit, I point it to the test folder, and all Files in my test folder and all it's sub-directories that end in Test.php are executed. This all works fine.
I now want to enable PHP_CodeCoverage for all of my code base, including files for which I don't have test files written yet.
I am able to manually enable PHP_CodeCoverage inside each test file by doing what the GitHub documentation advises:
inside my test file I include the follwoing:
require 'PHP/CodeCoverage/Autoload.php';
$coverage = new PHP_CodeCoverage;
$coverage->start('<name of test>');
// ...
$coverage->stop();
$writer = new PHP_CodeCoverage_Report_Clover;
$writer->process($coverage, '/tmp/clover.xml');
$writer = new PHP_CodeCoverage_Report_HTML;
$writer->process($coverage, '/tmp/code-coverage-report');
How do I enable it for my entire code base? I can't find any documentation for that case.
I figured it out myself:
When running phpUnit from the command line, add the following switch anywhere before the directory on which you are running phpUnit:
--coverage-html /your/coverage/directory/goes/here
Here is an example of the complete command:
$phpunit --colors --coverage-html /your/coverage/directory/goes/here /my/code/base/dir
Related
Yii2 has tests folder structure:
- tests
- _data/
- _output/
- _support/
- acceptance/
- functional/
- unit/
- _bootstrap.php
- acceptance.suite.yml.example
- functional.suite.yml
- unit.suite.yml
Я set up tests via PhpStorm and Codeception. It works fine. Test can be launched successfully. But I want to do integrational tests. I made a integrational folder, wrote test, clicked "Run" and this test didn't run. I mean, codeception didn't even go into the folder.
If I rename unit folder into unit2 or functional into functional2, then I get thr error:
Running with seed:
In Bootstrap.php line 31:
Bootstrap file /var/www/myproject/tests/functional/_bootstrap.php can't be
loaded
I thought that paths is hardcoded. But Bootstrap.php has the lines:
public function loadBootstrap(SuiteEvent $e)
{
$settings = $e->getSettings();
//....
$bootstrap = $settings['path'] . $settings['bootstrap'];
if (!is_file($bootstrap)) {
throw new ConfigurationException("Bootstrap file $bootstrap can't be loaded");
}
require_once $bootstrap;
}
Anyway. How do I configure settings (and where) to be available to run tests from the custom named folder? How to run integrational tests from integrational folder for example?
The Answer wrote Dmitriy Eliseev from https://yiiframework.ru/forum/viewtopic.php?p=273945#p273945
It's so simple.
The suites are picked up from *.suite.yml files.
So,in addition to unit.suite.yml and functional.suite.yml inside tests folder you should add an integrational.suite.yml file with settings. In this case tests from integrational folder will be work as well.
I'm looking through the documentation, but I'm not seeing any option to change the working directory used when running tests.
I'm using PhpUnit as it's included in Laravel. I want to be able to run vendor/bin/phpunit from my project's root directory, and have it run using the /public directory as the working directory.
I tried running ../vendor/bin/phpunit from the /public, but since the phpunit.xml file isn't in the public directory and I don't want to specify my config file path every time, that won't work.
Is there something I can add to my phpunit.xml file to tell it to run tests using the /public directory as the "cwd" (current working directory)?
Based on the feedback I received in the comments and the documentation, I determined the following:
It's probably not possible to change the cwd that phpunit uses by default (well, it's possible in PhpStorm, but not the command line without writing some kind of wrapper script)
Code that depends on being run from a specific directory is not a good idea.
What I had was some code in one of my classes like this:
$var = file_get_contents("../some_file.json");
This works fine -- until you try to add unit tests. The web server runs using the /public directory as the cwd, while phpunit will run using the root directory.
Rather than trying to force phpunit to always use a particular cwd (/public), I decided it's probably best to remove relative paths from the code that rely on a consistent cwd. So the line above becomes:
$var = file_get_contents(base_path("some_file.json"));
I didn't want to change production code that was already working just to get some tests in place, but this change seemed insignificant enough. (and it's an improvement anyway)
Well, you'd have to do the actual chdir in PHP, but you can define a bootstrap script in the XML (<phpunit bootstrap="./bootstrap.php">) and have that change the working directory.
Alternatively, you can put a setUpBeforeClass function into your test class that changes the working directory.
I installed the latest release of PHPUnit using the phar according to the documentation. I have some scripts to compile a code coverage report using the PHP_CodeCoverage object. With the update, when I try to include phpunit.phar phpunit is actually run. The script outputs the list of options for running PHPUnit and then exits.
How can I include the file so that I have the PHPUnit objects available in my script?
The line that I tried was
require '/usr/local/bin/phpunit.phar';
PHPUnit creates and registers an autoload function for including its files in phpunit.phar. I was able to use the PHPUnit classes in my script by extracting this function.
The code is available on Github
Basically, all the class paths are mapped and then included via require phar:///user/local/bin/phpunit.phar/<class path>
I using NetBeans 7.3 with PHP Unit test.
Test file creation works. I can create test file by right-click: tools->create PHP unit test. New test is created in Test folder (filenameTest.php).
When I run the test (Ctr+F6), in output window, I am getting error saying: that it can’t find the file I am trying to test. If I include_once the original file into test file everything works, and I can run the tests.
I would prefer not to add that include_once line manually into each testFile.
Is there a way to have NetBeans to do that for me automatically? Or how do I configure bootstrap file and/or phpunit.xml file, so it works without including the original file into testfile?
Thanks in advance.
You can create a bootstrap.php file in your tests folder that registers an autoloader to load the class that you are testing. You would then have a phpunit.xml file with the following:
<phpunit bootstrap="bootstrap.php">
</phpunit>
Registering an autoloader would be the easiest solution as then you don't have to remember to include files or if you or someone else isn't using NetBeans there aren't any problems with creating new tests.
Inside the bootstrap.php would be:
function autoloader($className) {
*** do logic to set path of file for the class ***
$classPath = "/base/path/for/file/" . $className;
require_once($classPath);
}
spl_autoload_register('autoloader');
I have a directoy structure, and all the classes of the business logic are placed in the app_dir/lib/ directory. I would like to generate unit tests for all the classes from this lib/ folder.
The problem is, that I haven't found any option to specify the source directory, only the source file:
from app_dir:
$ phpunit --skeleton-class lib/
Error: "lib/.php" could not be opened.
Is it the only solution to write my own php script, which iterates through the /lib folder
and calls the skeleton generator for every file found? And how can I specify the output folder, where all the generated test files are placed?
To generate skeleton tests, you want --skeleton-test not --skeleton-class. This will extract the filename without the extension and pass it to phpunit.
for file in *.php; do phpunit --skeleton-test "${file%.*}"; done;
I have no idea how to change the output directory which you would need if you want to run the command multiple times. I suppose a better one-liner would only select files not ending with "Test.php".
From Sebastian Bergmann's blog:
As of changeset 2764, PHPUnit 3.3's
command-line test runner accepts a
directory as its argument.
Given a directory, the test runner
will recursively scan the directory
for *Test.php files, build a test
suite out of the *Test classes, and
run it.
With PHPUnit >= 3.3 you should be able to execute just:
phpunit lib