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
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 created a custom component loaded by Composer.
Here is the structure of my code when my component is loaded.
MyProject
vendor
myComponent
AFTER that, I created the file myComponentTest.php to run an unit test with Codeception.
MyProject
tests
myComponentTest.php
vendor
myComponent
It works very well with the command :
./vendor/bin/codecept run
Alright. Nothing special about it. The Codeception test is ok ! :)
But I guess the procedure is wrong, the file myComponentTest.php should be in to the vendor/myComponent directory, am I right ?
Because, this unit test is only related to the component. For example, If I decide to remove the component, it won't remove my myComponentTet.php file, so I'll have some error when I'll run my unit tests.
BUT, if I move my MyComponentTest.php into the vendor/myComponent directory, I won't be able to run this test, because the Codeception command only execute tests from the tests directory.
So what should I do please ? I'm confused about that. Thanks.
See how testing is implemented in projects with sub-projects in Yii2 framework
codeception.yml in root project directory
include:
- common
- frontend
- backend
paths:
log: console/runtime/logs
settings:
colors: true
Where common|frontend|backend directory with codeception.yml files
I hope this helps.
As I understand it, out of the box Codeception will put all tests in one of the folders it makes based on type such as unit, functional, or acceptance. With large projects, that can easily get out of hand though. I'm trying to figure out how to have a structure like this:
- functional
- Module1
- Applications
- ApplicationType1Cept.php
- ApplicationType2Cept.php
- Accounts
- AccountType1Cept.php
- AccountType2Cept.php
When I do this:
codecept.phar generate:cept functional AccountType1Cept
It will put the new file in the root of the functional folder. I've tried doing something like:
codecept.phar generate:cept functional/Module1/Applications AccountType1Cept
But that does not work. I suspect it has something to do with suites, but not sure.
How can I get codeception to generate (and execute) tests in a more organized structure?
I am working on something similar, but on Windows.
Right now I have installed Codeception as global using Composer:
composer global require "codeception/codeception=2.0.*"
composer global require "codeception/specify=*"
composer global require "codeception/verify=*"
This allows me to switch to a specific folder like yours "/Module1/Applications/" and then issue the commands directly, e.g.:
a) set up the test directory:
codecept bootstrap
b) create the tests by:
codecept generate:cept functional AccountType1Cept
If you prefer, you can do it from the main directory, but you have first to tell Codeception the name, then use the "-c" option to indicate that you want to execute the command in the directory that follows, and then the target directory. In your case (using Linux) it would be:
codecept.phar generate:cept functional AccountType1Cept -c ~/Module1/Applications
but for me it's too much typing, it's easier to just switch to the target folder and issue all the commands there :-)
More information:
http://codeception.com/docs/07-AdvancedUsage#Running-from-different-folders
I had a similar need.
What you need to do is something like this:
codecept.phar generate:cept functional "Application\ApplicationType1Cept"
codecept.phar generate:cept functional "Account\AccountType1Cept"
This creates the test files in the folders you want and namespaces them also.
I did this without any specific suite configuration (version 3.1.2). For example: if you make a directory structure like this:
tests
unit
Services
SomethingServiceTest.php
codecept.phar run unit will find your test.
I am trying to set some tests in symfony.
I am doing the first steps on that.
My question is from which folder should we write the phpunit -c app
I mean in c:/
or from the bundle
because I get the message of the not recognised internally command.
You must run it from root direcory of your application. app is just an argument which specify folder where phpunit.xml.dist places.
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.