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.
Related
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 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');
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
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