phpunit --testsuite command does not work - phpunit

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

Related

Grouping phpunit tests. Launching test from specified directory

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

PHPUnit pass configuration file and test file path

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
?

Excluding certain tests from loading in phpunit

Some of my testcases use a custom testing library. Also these testcases are very slow. So I would like to run them only in the build server and not in my local. I want to run the other tests locally.
Following is the directory structure. The ones inside the slow directory are the slow test cases that should be excluded.
/tests/unit-tests/test-1.php
/tests/unit-tests/test-2.php
/tests/unit-tests/slow/test-1.php
/tests/unit-tests/slow/test-2.php
/tests/unit-tests/foo/test-1.php
/tests/unit-tests/bar/test-2.php
I tried creating groups using #group annotation. This works, but the issue is that these test files are getting loaded (tests not executed though). Since they require the test library which is not installed locally, it is giving an error.
What is the best way to create the phpunit.xml configuration such that these slow tests are excluded (and not even loaded) by default and can be executed if needed?
There are 2 options:
In your phpunit.xml create 2 test suits - one for CI server and one for local development
<testsuites>
<testsuite name="all_tests">
<directory>tests/unit-tests/*</directory>
</testsuite>
<testsuite name="only_fast_tests">
<directory>tests/unit-tests/*</directory>
<!-- Exclude slow tests -->
<exclude>tests/unit-tests/slow</exclude>
</testsuite>
</testsuites>
So on CI server you can run
phpunit --testsuite all_tests
And locally
phpunit --testsuite only_fast_tests
Obviously, you can name test suites as you want.
I think preferable way is:
Create phpunit.xml.dist and configure default execution of phpunit (for CI server and all those who just cloned repository)
Modify phpunit.xml by configuring your local execution of phpunit
(by adding <exclude>tests/unit-tests/slow</exclude> to default
testsuite)
Exclude phpunit.xml from version control.
From the docs:
If phpunit.xml or phpunit.xml.dist (in that order) exist in the
current working directory and --configuration is not used, the
configuration will be automatically read from that file.
Some links:
The XML Configuration File. Test Suites
How to run a specific phpunit xml testsuite?

Phpunit testsuite name

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;

Suppressing the request when running PHPUnit with Kohana 3.2

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.

Resources