Codeception Sub Folders - php

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.

Related

How do I set the working directory for phpunit using the XML file?

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.

Codeception into another 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.

Executing DB dump dynamically in codeception acceptance helper

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.

Configuring File Names for PHPUnit

I am a new user of PHPUnit, and I am converting our existing tests (asserts) into the PHPUnit framework to provide a better test environment and code coverage. However, I need to know how to try to get PHPUnit working with our testing code structure.
Our project directories are similar to the following:
Application1/
CREDIT_CARD.class - Class naming convention for CREDIT_CARD
CREDIT_CARD.class.test - Automated Tests for CREDIT_CARD.class
File.php - Application File
File.php.test - Automated tests for File.php
File2.php
File2.php.test - Automated tests for File2.php
Application2/
ANOTHER_CLASS.class
ANOTHER_CLASS.class.test
DifferentFile.php - Application File
DifferentFile.php.test - Automated tests for File.php
lib/
UTIL/
SHARED_CLASS.class
SHARED_CLASS.class.test
VISUAL/
VISUAL_TOOL.class
VISUAL_TOOL.class.test
I need to know how to configure the PHPUnit tests so I can run the tests in lib/UTIL/.test (which load the class file using the setUp() method) then the lib/VC/.test, followed (if successful) by the Application1 and Application2 tests. I saw mention of a PHPUnit_xml file and a bootstrap file, but I can not find a reference template to see if these are what I need. Any help would be appreciated.
I know the documentation refers to a test.php addition to the file names, but I am hoping to not have to change our structure and naming conventions as I would like to be able to run a mix of the files until they are all converted over to the PHPUnit framework. Changing names will cause a procedure change in our company and training for the developers, which I am trying to avoid.
Thanks in advance for any assistance.
So you have files named Foo.class.test instead of the PHPUnit default FooTest.php ?
That shouldn't be a big problem as PHPUnit allows you to configure what file suffixes are to be treated as "tests". The Test.php is just the default.
Have a look at the xml config part of the docs regard test organisation
What you want is:
<testsuites>
<testsuite name="Our new shiny phpunit test Suite">
<directory suffix=".test">./sourceFolder</directory>
</testsuite>
</testsuites>
PHPUnit will then scan through the source folder including all files that end in .class.test and leaving the other files as is.

Generate tests for all the classes from a directory

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

Categories