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.
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've installed the symfony plugin to PHPstorm, but when I'm trying to run the project I get the following error
Fatal error: Class 'Doctrine\Tests\Common\Cache\CacheTest' not found in ...
I've checked the path and CacheTest file is in the correct folder. When I run the symfony project from the command line it works just fine.
Is there someway to fix this problem?
What you try is not correct. You execute the php.exe and that is the normal PHP executable. To run your Unit-Tests you need PHPUnit to run.
Go to Preferences -> Languages & Frameworks -> PHP -> PHPUnit and define the phpunit.phar.
When you've done that you create a new configuration to run PHPUnit and you define the configuration which is under app/phpunit.xml.
I figured it out. First of all you need add phpunit.phar as Stony explained in his answer.
After that all I really needed to do was to rename phpunit.xml.dist to phpunit.xml in app/ folder and then just right click it and choose the Run 'phpunit.xml' option. This way the Tests succeed and it doesn't give any errors.
As for running the symfony project you need to add symfony command line tool support to phpstorm. All about that in this link: https://confluence.jetbrains.com/display/PhpStorm/Symfony2+Command+Line+Tool+Integration+-+Symfony+Development+using+PhpStorm
After that you just run it as a command like you would with normal command line. That's also explained in the link.
Try to choose "Use custom autoloader" option in Preferences -> Languages & Frameworks -> PHP -> PHPUnit and specify the path to vendor/autoload.php file.
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 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