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
Related
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 want to execute different dump file for different codeception tests. Right now Db dump file is being executed from shell_exec command in _before method of AcceptanceHelper, that executes before each and every acceptance test. Something like suggested here. There are alot of tests in the app. So, the flow is as follows
- tests/acceptance/application/<contains alot of tests related to application>
- tests/acceptance/location/<contains alot of tests related to location>
Both test directories /application/ and /location/ uses same AcceptanceHelper. So, what i want is a different executable dump file for all of the tests inside /application/ directory than that of /location/ tests.
Think of something like Get current running test name. Let's say application_dump.sql for all tests inside /application/ directory and location_dump.sql for all tests inside /location/ directory.
P.S: Using a different suite for application and location is ideally not what i am looking for.
Just to help some one out there. As there was no proper way to achieve this as Get current running test name seems to be still in development.
So, this is how i managed to solve the issue. I have moved shell_exec commands out of _before method of AcceptanceHelper and created a new public method inside AcceptanceHelper which can be accessed via actor class $I in each and every acceptance test like so
$I->executeDbDump('application_dump.sql');
$I->executeDbDump('location_dump.sql');
Only drawback using this approach is i have to execute respective executeDbDump function before each and every test manually. But still seems to be best approach for the issue now.
i'm newbie & trying to learn travis within my PHP development environment.
It's very "basic", i just trying a sample .travis.yml from official guide. But it always failing.
My repo is here https://github.com/rahmatawaludin/learn_travis and my travis build is here http://travis-ci.org/rahmatawaludin/learn-travis .
Could anyone give me some help?
I guess you followed the PHPUnit manual.
If you want to write a test for class MyClass (file MyClass.php), the corresponding tests would be in class MyClassTest (file MyClassTest.php).
Now if you want to run the test you'd call phpunit like that:
phpunit MyClassTest
But you called phpunit with the filename, just remove the .php extension, then it should work.
I'm trying to generate TestCase with the phpunit generator.
I'm using the following command:
phpunit --skeleton-test "Namespace\Service\ArticleService" ../library/Namespace/Service/ArticleService.php
I'd like my tests to go in /tests/Namespace/Service/ArticleServiceTest.php
Is it possible to specify such options with PHPUnit?
No, it is not possible. Open a feature request in the phpunit bug tracker on github.
The newer phpunit-skelgen (1.2.0) allows you to specify the destination file for tests, which may include path references.
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.