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');
Related
I am using Pest library to write tests in laravel. I created the my-laravel-application/tests/Integration directory in laravel
and defined a new test suite in phpunit.xml
<testsuite name="Integration">
<directory suffix=".test.php">./tests/Integration</directory>
</testsuite>
So that laravel acknowledges the test files in Integration directory and I could write my integration tests in a separate directory with a proper name(Integration directory), And I put my test files in the my-laravel-application/tests/Integration directory and I got the following error while running php artisan test:
InvalidArgumentException - Unknown format "name"
vendor/fakerphp/faker/src/Faker/Generator.php:657
which indicates that the $this->faker->name() line of code in my UserFactory (I am using UserFactory class in my tests) has something wrong with, it says that name() method does not exist on $this->faker. But my tests used to work fine, before moving them to my-laravel-application/tests/Integration directory. What is the real problem and how can I fix this?
I found the solution based on this answer. You should enforce the use of Tests\TestCase (instead of PHPUnit\Framework\TestCase) for all the tests written in your newly created directory (Integration directory) by adding the following code to /tests/Pest.test:
uses(Tests\TestCase::class)->in('Integration');
This thread saved me lots of time.
I made sure an using right test case in the entire test dir...
uses(Tests\TestCase::class)->in(__DIR__);
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
Is it possible to tell phpunit where to put generated test skeleton file by the --skeleton-test command? Even is it possible to tell phpunit to repeat directory structure?
Lest say i have file for testing in lib/model/SomeClass.php and i want phpstorm to generate unit test class and put it in test/lib/model/SomeClassTest.php without creation of all neccessary directory structure.
Thanks in advance!
Example reproduced for convenience
mkdir lib/model tests/lib/model
echo "<?php class SomeClass{}" > lib/model/SomeClass.php
Calling the code below should do the trick
phpunit --skeleton-test SomeClass library/model/SomeClass.php SomeClassTest tests/library/model/SomeClassTest.php
The arguments (only the first one being mandatory) are
$inClassName,
$inSourceFile = '',
$outClassName = '',
$outSourceFile = ''
as described in the constructor of PHPUnit_Util_Skeleton_Class
Yeah that is possible in netbeans IDE. if you configure PHPUnit(Xdebug required) with netbeans then it automatically creates file/folder structure for your framework
Netbeans link
Also it creates the skeleton for each class of Controller and module
/vendor/phpunit/phpunit-skeleton-generator/src/TestGenerator.php
In the constructor change the $outSourceFile to the path you would like the file to save to.
Cheers
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
I've just installed PHPUnit and wrote a quick class which I saved to C:\PHP and it worked fine. If however I move the php file containing the test class to the tests directory of my application, it returns the error Class firstTest could not be found in ..
How do I resolve the problem such that it can see the class in the application test directory?
Thanks for the response - it wasn't the solution I used, but it led me to research that produced an alternative.
What I did was to add my PHP directory (C:\PHP) to the PATH environment variable. This allowed me to call phpunit from the tests directory of my application.
Check your config file and ensure that the correct path to the tests dir is given.