I'm using Symfony Functional Tests in NETBEANS. Is it possible to define the order in which order will the test cases be executed?
For example: I have "UserTestCase" case for testing registration of users, changing their profiles etc, and "ForumTestCase" for testing creation of forum posts etc. I want to run UserTestCase before ForumTestCase. Now the order is according to file names (F.. before U..)
You need to define order of tests in your phpunit.xml as described in docs
example:
<phpunit>
<testsuites>
<testsuite name="Object_Freezer">
<file>Tests/Freezer/HashGenerator/NonRecursiveSHA1Test.php</file>
<file>Tests/Freezer/IdGenerator/UUIDTest.php</file>
<file>Tests/Freezer/UtilTest.php</file>
<file>Tests/FreezerTest.php</file>
<file>Tests/Freezer/StorageTest.php</file>
<file>Tests/Freezer/Storage/CouchDB/WithLazyLoadTest.php</file>
<file>Tests/Freezer/Storage/CouchDB/WithoutLazyLoadTest.php</file>
</testsuite>
</testsuites>
</phpunit>
I'm using Netbeans for the development and I was wondering for a long time, why this didn't work for me.
Then I found out, that Netbeans simply ignores folder definitions and the order of execution defined in phpunit.xml
See here: https://netbeans.org/bugzilla/show_bug.cgi?id=199072
Related
I have configuration that tests my localhost API succesfully in PhpStorm using PHPUnit. Also I can stop on breakpoints but only inside my TestCase classes.
I need to stop on breakpoints that are set inside logic that are tested, but it does not stop there.
Testing command:
/usr/bin/php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 /foo/vendors/composer/phpunit/phpunit/phpunit --configuration /foo/_stuff/phpunit/config-api2.xml --teamcity
My config xml:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="../../foo/tests/app_test_case.php"
colors="true"
stopOnFailure="false"
>
<testsuites>
<testsuite name="foo">
<file>../../foo.php</file>
</testsuite>
Added to php.ini:
xdebug.remote_autostart = 1
Added to requested route:
?XDEBUG_SESSION_START=PHPSTORM
And added cookie in index.php:
header('Cookie: XDEBUG_SESSION=PHPSTORM');
Yes, you can.
The question here is how you are going to start a debug session for those API calls.
The most usual way would be adding xdebug.remote_autostart=1 to php.ini for the interpreter running the calls, so that a debug session would start for every single PHP call, API or not.
If you are not happy with it, you can add the XDEBUG_SESSION_START GET-parameter to HTTP requests of the calls, but that would obviously require code modification.
sonarqube: 5.1.2
sonar-runner: 2.2.1
php plugin: 2.6
PHPUnit 4.2.6
We're running phpunit over our application but I'm not able to get the correct coverage % on sonar as expected.
In my phpunit.xml we have filters defining folders that we only want to cover.
<whitelist addUncoveredFilesFromWhitelist="false">
<directory>./site-library-php/src/main/php/BabelCentral/Model/Content</directory>
</whitelist>
In my sonar properties
sonar.modules=php-module
php-module.sonar.phpUnit.coverage.analyzeOnly=true
php-module.sonar.phpUnit.analyzeOnly=true
php-module.sonar.php.tests.reportPath=site-main-php/src/test/target/reports/phpunit.xml
php-module.sonar.php.coverage.reportPath=site-main-php/src/test/target/reports/phpunit.coverage.xml
Viewing the log on jenkins, tests seem to run fine and ends with:
Tests: 1479, Assertions: 4165, Failures: 58, Incomplete: 14, Skipped: 55.
Generating code coverage report in Clover XML format ... done
Generating code coverage report in HTML format ... done
// further down
11:47:51.973 INFO - Analyzing PHPUnit test report: site-main-php/src/test/target/reports/phpunit.xml with org.sonar.plugins.php.phpunit.PhpUnitResultParser#35e7e715
11:47:52.604 INFO - Analyzing PHPUnit unit test coverage report: site-main-php/src/test/target/reports/phpunit.coverage.xml with PHPUnit Unit Test Coverage Result Parser
All seems to be working well. However, sonar would report a different result, covering the entire source folder. These are the number shown on the dashboard.
Unit Tests Coverage 1.8%
line coverage 1.8%
Unit test success 95.9%
Is there anyway to have sonar respect phpunit's configured filter?
Note:
I'm able to achieve the desired numbers on coverage if I explicitly set
php-module.sonar.sources
to the directories/files that I want. It's just that, comma-separated config is hard to manage than phpunit's xml config.
Defining the set of source files which should be analysed by SonarQube is crucial: it's on this set of files that coding rules are applied and that metrics are computed.
sonar.sources is therefore a mandatory property and is the main way to configure the set of source files.
Other properties may be used to refine the set of source files:
You may configure sonar.tests: test files are automatically excluded from sonar.sources.
You may have to exclude some other PHP files like dependencies or generated code with sonar.exclusions.
You should also be aware that several plugins may contribute to the global coverage of the project: if you installed the JavaScript plugin and if sonar.sources contains JavaScript files, they will also be taken into account when calculating the coverage metrics.
If you're only interested in adjusting coverage, you can exclude files from coverage metrics with sonar.coverage.exclusions.
for example, if I run the testng.xml and it contains multiple test suites
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Master Suite">
<suite-files>
<suite-file path="test_1.xml"></suite-file>
<suite-file path="test_2.xml"></suite-file>
</suite-files>
</suite>
after, I checked reportng report, it did not display both test suites report but only the last one. How to avoid overwriting previous test report?
please help
dont add listeners in suite files add those in ur testng.xml
this will solve ur issue.
I've an issue with symfony and phpunit. Our peoject is growing bigger and bigger. So we decided to activate process isolation for phpunit because the server couldn't stand the amount of tests anymore (Not enough RAM). Since then all test which send mails aren't working anymore. Could someone please help us? The test below works perfectly fine if processIsolation="false" but it fails if processIsolation="true"
Versions:
symfony 2.1.8-dev
phpunit 3.7.9
ERROR Message
Project\AppBundle\Tests\MailTest::testSendMail
PHPUnit_Framework_Exception: PHP Fatal error: Class 'Swift_Message' not found in /var/www/project/src/Project/AppBundle/Tests/MailTest.php
Test
public function testSendMail()
{
$client = static::createClient();
$message = \Swift_Message::newInstance();
$message->setFrom('example#example.com')
->setTo('example#example.com')
->setSubject('Subject')
->setBody('Hello World')
->setContentType('text/html');
$client->getContainer()->get('mailer')->send($message);
$this->assertTrue(true);
}
phpunit.xml
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
bootstrap="./autoload.php"
processIsolation="true"
stopOnFailure="false"
syntaxCheck="false" >
<testsuites>
<testsuite name="Project Test Suite">
<directory>../src/Project/AppBundle/Tests/MailTest.php</directory>
</testsuite>
</testsuites>
</phpunit>
This error is caused because Switfmailer defines a constant
SWIFT_REQUIRED_LOADED after it installs it's autoloader. The
autoloader checks this constant, and if it's already defined, refuses
to install the autoloader. In process isolation, PHPUnit ensures that
all defined constants are re-defined in the processs that gets spawned
to run the test. Unfortunately, this means that SWIFT_REQUIRED_LOADED
is defined in the test process, so the autoloader doesn't load (see
"swift_required.php" in the swiftmailer source directory). Note that
if you turn off include global state for the test via annotations your
tests still won't work, because the bootstrap file is passed to the
test process via the __PHPUNIT_BOOTSTRAP global variable (see
TestCaseMethod.tpl in the PHPUnit directory). Without the globals,
this global is undefined in the test process and the bootstrap file is
not included breaking the tests.
The only work around that I found that works was to replace the line
$constants = PHPUnit_Util_GlobalState::getConstantsAsString();
with
$constants = '';
in the run method of TestCase.php in the PHPUnit source distribution. If your code
relies on global constants to be defined before your test cases run
this fix obviously won't work for you (class constants are a
different story); unless said constants will be redefined safely in the
course of running your test cases.
This is caused by a problem related to the swiftmailer autoloader.
I encountered the same problem and fixed it in a forked repository of swiftmailer.
Here you can find the PR for the fix: https://github.com/swiftmailer/swiftmailer/pull/416.
If you do not wish to wait until this is merged you can use my repository by adding this to your composer.json.
"require": {
"swiftmailer/swiftmailer": "dev-master as 5.0.x-dev"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/NickStemerdink/swiftmailer.git"
}
]
UPDATE: The PR has been merged in the meanwhile, so you can just update to the latest swiftmailer version.
Does it work if you first instantiate $message and call static::createClient afterwards?
I currently have a problem that I have to work around in legacy code to get our interaction with a PHP Extension to work properly (Singleton Testing Question).
As such, I do not want to execute this code when running our normal production code with the application. Therefore, I need to check in regular PHP code if the code being executed is being executed as part of a test or not.
Any suggestions on how to determine this? I thought about a defined variable tied to the presence of the test files themselves (we do not ship the tests to customers) but our developers need the Extension to work normally, while the CI server needs to run the tests.
Would a Global set in the PHPUnit.xml file be recommended? Other thoughts?
An alternative approach is to set a constant in the PHP section of your phpunit.xml.*:
<php>
<const name="PHPUNIT_YOURAPPLICATION_TESTSUITE" value="true"/>
</php>
In your PHP application, you might then use the following check:
if (defined('PHPUNIT_YOURAPPLICATION_TESTSUITE') && PHPUNIT_YOURAPPLICATION_TESTSUITE)
{
echo 'TestSuite running!';
}
Define a constant in your PHPUnit bootstrap.php file. This is executed before loading or running any tests. This shouldn't impact developers running the application normally--just the unit tests.
If you're using Laravel than use App::runningUnitTests()
You could check the $argv different ways.
if(PHP_SAPI == 'cli') {
if(strpos($_SERVER['argv'][0], 'phpunit') !== FALSE) { ... }
// or
if($_SERVER['argv'][0] == '/usr/bin/phpunit') { ... }
}
Use PHPUnit Constants
You can either define constant, but that requires your work, no typos and it's not generic. How to do it better?
PHPUnit defines 2 constants by itself:
if (! defined('PHPUNIT_COMPOSER_INSTALL') && ! defined('__PHPUNIT_PHAR__')) {
// is not PHPUnit run
return;
}
// is PHPUnit