I use phpunit and webdriver.
In my test.php file, I try to get the name of the testsuite from the phpunit.xml file.
<testsuites>
<testsuite name="myTestsuite">
<directory>./tests</directory>
</testsuite>
</testsuites>
Do you know, how can I access the testsuite name?
I don't think it's possible at the moment.
I created an issue in PHPUnit repository to discuss. https://github.com/sebastianbergmann/phpunit/issues/4468
You can use simplexml_load_file to load the file and access the attribute using the following code:
$xml = simplexml_load_file('phpunit.xml');
echo $xml->testsuites->testsuite->attributes()->name;
Related
I have a directory tests where I store all my tests.
Hierarchy of tests dirrectory
tests->
ApplicationTests->
IntegrationTests->
Factory->
Service->
UnitTests->
How can I make phpunit launch tests only from for example Service directory, instead of the whole tests directory? I read about #Groups, but I think that's not what I'm looking for and it would be best if I would not need to edit config files, but some how, do it from the command line.
Found a quite easy way, even with config editing.
phpunit.xml
See: The XML Configuration File Phpunit Docs and Organizing Tests Phpunit Docs<
<testsuite name="Service">
<directory>tests/IntegrationTests/Service</directory>
</testsuite>
Command line:
php ./vendor/bin/phpunit --testdox --testsuite Service
I want to keep as much of my configuration stored in a configuration file as possible. There are times when I want to only run the tests in a specific file in addition to using my normal configuration options. Is there a way to pass both a configuration file and a single test filepath for PHPUnit to run?
Can you use testsuites?
Example:
Config file:
<testsuites>
<testsuite name="Example">
<directory suffix="Test.php">src/tests</directory>
<file>tests/WhateverTest.php</file>
</testsuite>
</testsuites>
Command line:
phpunit --testsuite Example
?
Ok, i really don't understand what is happening with my PHPUnit's bootstrap file.
I have a folder structure like the following one:
[Root]
- joo_component
-- administrator
-- site
-- Tests
--- admin
--- site
--- bootstrap.php
--- phpunit.xml
- vendor
In the phpunit.xml file, i have the following code:
<phpunit bootstrap="./bootstrap.php" colors="true">
<testsuites>
<testsuite... [all the things that go here]</testsuite>
</testsuites>
</phpunit>
Now, if i call PHPUnit explicitly specifying the bootstrap file all works well: the testing starts and properly signals errors and all other things it has to signal.
This is the command that works:
vendor/bin/phpunit --bootstrap="joo_component/Tests/bootstrap.php" joo_component/Tests
But, if i try to launch the testing without explicitly specifying the bootstrap file but specifying it in the phpunit.xml file it seems like the bootstrap file isn't loaded.
vendor/bin/phpunit joo_component/Tests
Is there anyone who can help me underdtand why this happens? Is there a way to force PHPUnit to tell me which bootstrap file it is using to load the test suites?
I am on Windows in some project directory (I will call this my "root" directory). I run this:
php vendor/bin/phpunit.phar -c tests/phpunit.xml --testsuite "Suite"
command runs but ignores my "Suite" and gleefully reports "No tests executed!"
phpunit.xml contains:
<phpunit
bootstrap="bootstrap.php"
>
<testsuites>
<testsuite name="Suite">
<directory>;./</directory>
<file>ProductTest.php</file>
</testsuite>
</testsuites>
</phpunit>
I have no problem running this on Linux, but on Windows it does not run this suite. I tried changing directory from ;./ to ;\ to tests, to full directory path on Windows, but no luck. It does not complain but does not run the tests either.
How to I fix it?
<directory>..\tests</directory> worked for me
I'm having trouble correctly setting up unit testing in Kohana 3.2.
I installed PHPUnit. I changed the bootstrap to activate Kohana's unittest module. I also changed the index.php file to look like this:
if ( ! defined('SUPPRESS_REQUEST'))
{
echo Request::factory()
->execute()
->send_headers()
->body();
}
I created a folder tests in my application folder. In it, I inserted a phpunit.xml file that looks like this:
<phpunit colors="true" bootstrap="../../index.php">
<testsuites>
<testsuite name="Kohana Tests">
<directory>./</directory>
</testsuite>
</testsuites>
I am having two problems (the first one is the one I really need an answer to):
1- When I go into tests from the command line and try running phpunit, it seems that SUPPRESS_REQUEST never gets set to true. The Request is executed, and therefore no tests are run. The only way I am able to run the tests successfully is to momentarily comment out the entire Request::factory() line in index.php. Does anyone know how to get around this problem? If I should be adding a define('SUPPRESS_REQUEST', true) somewhere, where should I do it? I'm new to Kohana and PHPUnit.
2- PHPUnit complains that the variable REMOTE_ADDR is not set.
PHP Notice: Undefined index: REMOTE_ADDR in
/Users/**/Sites/root/application/bootstrap.php on line 76
This is actually not a huge problem for now as tests still seem to run in spite of this, but I'm wondering if anyone knows if I should be setting this variable somewhere in specific.
In modules/unittest there is a file called bootstrap.php which works perfectly well with phpunit.
My phpunit.xml which references that bootstrap is this:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="../../modules/unittest/bootstrap.php">
<testsuites>
<testsuite name="Kohana Tests">
<directory>./</directory>
</testsuite>
</testsuites>
</phpunit>
Also, for the REMOTE_ADDR problem, when phpunit is running the CLI version of PHP, which I don't think has access to a REMOTE_ADDR variable. If you look at the bootstrap from unittest, it does not use http related globals.
I'm not sure why you have to run Request::factory code in your bootstrap. On my vanilla 3.2 install, the Request::factory code lives in index.php and not bootstrap.php and does not have any reference to SUPRESS REQUEST. You may have some lingering files from a pre-3.2 installation which need cleaning.