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.
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.
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 have set up PhpStorm 5 with PHPUnit, and I'm curious if PhpStorm might have some functionality that will automatically run a unit test when saving a file. Like watchr and guard. I have tried search our beloved www and the PhpStorm docs, but haven't been able to find a solution for it.
As of version 6, PHPStorm has "File Watchers"
Open your project preferences.
Select File Watchers from the left hand list of options.
Click the + symbol at the bottom of the empty right hand panel.
Select <custom>
You will have to set up a command line for PHPUnit, it wont be the integrated testing, but you can have errors output to the console (which is good a good start!)
Various macro options are available to you, so you can include (for example) $FileNameWithoutExtension$Test.php in the arguments passed to your command line.
I personally had to set up two watchers. The first detected modifications to project files, and the second detected changes to test files (the second did not append Test.php to the filename) I also created a new project scope to exclude the tests directories from the first watcher.
You may also want to turn off immediate synchronisation, as this causes PHPUnit to run when PHPStorm auto-saves files.
My other settings are like:
File Type: PHP files (PHP)
Scope: Project excluding tests
Program: /path/to/php
Arguments: /path/to/phpunit --configuration /path/to/phpunit.xml.dist /path/to/tests/$FileNameWithoutExtension$Test.php
Working directory: $FileDir$
Output paths: $FileDir$
No output filters set, syntax error checks enabled, and console showing errors.
PHPUnit watcher named as hot phpunit runner
https://github.com/slavahatnuke/hot-phpunit-runner
You can also have a look at TDDRunner
It is console tool that execute PHPUnit autmaticly on file changes. You can also configure PHPUnit by excuting only one file ot whatever.
/usr/bin/tddrunner --group=test
There's a a German article providing further detail.
In 2017 Jetbrains release a feature that allow auto-run for tests.
It's located in the Run console, therefore it's linked to the run settings, easing the setup of this autorun.
See https://blog.jetbrains.com/phpstorm/2017/07/autorun-phpunit-tests-in-phpstorm-2017-2/
I want to measure code coverage of an HTML test suite for selenium. Therefore I want to use PHPUnit in order to execute the suite, because PHPUnit has nice support for code coverage analysis.
Therefore: Is it possible to run an HTML test suite from PHPUnit?
Short answer
Running individual HTML test files is not a problem, running HTML suites files however does not seem to work. As long as you put all the HTML test files from a suit in a directory by themselves you can just run runSelenese($folderName)
Long answer
I had no idea that running Selenium HTML files directly was even possible until I did some more digging.
What I used to do is convert/export them first with the Selenium IDE PHP Formatter plugin for Firefox.
Apparently this is not necessary. All the way at the bottom of Chapter 17. PHPUnit and Selenium the manual states:
Using the runSelenese($filename) method, you can also run a Selenium test from its Selenese/HTML specification. Furthermore, using the static attribute $seleneseDirectory, you can automatically create test objects from a directory that contains Selenese/HTML files. The specified directory is recursively searched for .htm files that are expected to contain Selenese/HTML. Example 17.5 shows an example.
Example 17.5: Use a directory of Selenese/HTML files as tests
<?php
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
class SeleneseTests extends PHPUnit_Extensions_SeleniumTestCase
{
public static $seleneseDirectory = '/path/to/files';
}
?>
Once you have your Tests in PHPUnit you can use it to get code coverage from the Selenium Server.
Again, from the manual:
PHPUnit_Extensions_SeleniumTestCase can collect code coverage
information for tests run through Selenium:
Copy PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php into your webserver's document root directory.
In your webserver's php.ini configuration file, configure PHPUnit/Extensions/SeleniumTestCase/prepend.php and PHPUnit/Extensions/SeleniumTestCase/append.php as the auto_prepend_file and auto_append_file, respectively.
In your test case class that extends PHPUnit_Extensions_SeleniumTestCase, use
protected $coverageScriptUrl = 'http://host/phpunit_coverage.php';
to configure the URL for the phpunit_coverage.php script.
As this can be a bit of a hassle to set up, I've always just used the Page Coverage plugin to get an insight into HTML page coverage.
Unit testing PHP HTML output is a great idea. More people should do it, but turns out it has not been that simple in the past.
I had the same problem and can up with this: https://packagist.org/packages/phpfui/html-unit-tester It can validate HTML and CSS from a URL, file, or string snippet.
To do what you want, you should set up a local web server that your tests can hit for your application. You probably have this already, but make sure it works for your local env and any server you run tests on.
Then you can use the assertValidUrl method with a path to your local server. Since you are not comparing to a template, but just testing that the HTML is valid, you can quickly find recently introduced HTML errors.
Hope this helps.
This might be a stupid question, but I can't seem to make it to work.
I'm using PHPUnit to test. Currently I have two classes in a file called Tests.php:
class XTest extends PHPUnit_Framework_TestCase {...}
class YTest extends PHPUnit_Framework_TestCase {...}
However, I'm unable to run both classes. I'm running the following command on Windows:
php "C:\Program Files (x86)\PHP\phpunit" Tests
And it tries to run a test class called "Tests". Instead, I'd like it to run "XTest" and "YTest" and all that are on the file. How could I run multiple test classes easily?
Putting all of your tests under the same directory and asking PHPUnit to traverse them recursively would work, but if you have your tests under different directories or only want to run specific portions of specific test classes, then the #group annotation might be what you're looking for.
When you execute your tests, you can use the php "C:\Program Files (x86)\PHP\phpunit" --group <insert_name_of_group_to_which_xtests_and_ytests_belong> and PHPUnit will only execute those tests that have #group insert_name_of_group_to_which_xtests_and_ytests_belong in their PHPDoc.
The PHPUnit Docs explain the arguments the command line test runner expects.
In your case, you're providing Tests, which means PHPUnit looks for a class Tests in a file Tests.php.
With this knowledge, it's easy to see that the best way to organise your tests will be to write one test class per file, with the filenames equal to TestClassName.php.
However, if for some reason you don't want to do that, you can provide an extra argument to tell the test runner which file the test class is declared in:
php "C:\Program Files (x86)\PHP\phpunit" XTest Tests.php
php "C:\Program Files (x86)\PHP\phpunit" YTest Tests.php