Unittests running twice in parent class - php

For many good reasons I put some tests into parent test class und some tests in inhereted class. I have got this structure then:
/bar/foo/foo.php
/bar.php
This command I use to start phpunit: phpunit --configuration unit.xml I tried this one too, but with same result: phpunit --configuration unit.xml --testsuite foo
So now I want to run all tests in a folder foo. Problem is, that tests in bar.php will be running twice and I have no idea why. Do someone has the answer to that? Is it maybe a feature of phpunit? How can I told phpunit not to run them twice?
If I run phpunit bar/foo/foo.php everything works fine and test function in bar.php is running only once.
foo.php
require_once '/bar.php';
class foo_test extends bar_test {
public function test_1_pass() {
$this->assertEquals('a', 'a');
}
}
bar.php
class bar_test extends PHPUnit_Framework_TestCase {
public function test_2_fail() {
$this->assertEquals('a', 'b');
}
}
Unittest responce
PHPUnit 3.7.29 by Sebastian Bergmann.
Configuration read from /unit.xml
F.F
Time: 104 ms, Memory: 3.25Mb
There were 2 failures:
1) bar_test::test_2_fail
Failed asserting that two strings are equal.
/bar.php:6
2) foo_test::test_2_fail
Failed asserting that two strings are equal.
/bar.php:6
FAILURES!
Tests: 3, Assertions: 3, Failures: 2.
unit.xml
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
syntaxCheck="false">
<testsuites>
<testsuite name="foo">
<directory suffix=".php">bar/foo</directory>
</testsuite>
</testsuites>
</phpunit>

If you don't intend to execute the parent test class on its own, make it abstract, this prevents PHPUnit from trying to instantiate it and running its tests.

Related

PHPUnit can't find a class loaded in the bootstrap

My problem with PHPUnit is quite simple: if the test class file is included in the bootstrap file, PHPUnit won't be able to find the test class.
You can easily recreate the issue:
The file hierarchy:
./tests/MyTest.php
./bootstrap.php
./phpunit.xml
phpunit.xml
<phpunit bootstrap="./bootstrap.php">
<testsuites>
<testsuite name="MyTest">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
bootstrap.php:
<?php
require __DIR__ .'/vendor/autoload.php';
require __DIR__ .'/tests/MyTest.php';
tests/MyTest.php
<?php
namespace App\Tests;
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
public function testTrue()
{
$this->assertTrue(true);
}
}
In this case, when I run ./vendor/bin/phpunit, it returns "No tests executed!". And if I run ./vendor/bin/phpunit ./tests/MyTest.php, it returns "Class 'MyTest' could not be found in '[...]\tests\MyTest.php'.".
Now, if I remove the second require_once from autoload.php, both commands work as expected.
I've only seen a couple of similar issues on stack. They are six years old and have not been resolved, which makes me think that not allowing this way of loading classes is actually a design choice.
Could you please help me? Thank you!

phpunit / excluded files still returning warnings

using version 8.1.6, and Given the following phpunit.xml :
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="SlimSkeleton">
<directory>tests</directory>
<exclude>./tests/Functional/BaseTestCase.php</exclude>
</testsuite>
</testsuites>
</phpunit>
And the following directory structure :
./tests/Functional/serviceA/...
./tests/Functional/BaseTestCase.php
I keep getting the following output :
...
1) Warning
No tests found in class "Tests\Functional\BaseTestCase".
...
I run the suitr via a scripts command in composer.json :
{
...
"scripts": {
"test": "phpunit"
}
}
Is it expected ? Is there a way to silence this warning ?
phpunit by default finds *Test.php, so even without <exclude> in phpunit.xml it would ignore BaseTestCase.php by running composer test.
Using tests instead of tests/EmailTest would instruct the PHPUnit command-line test runner to execute all tests found declared in *Test.php sourcecode files in the tests directory.
https://phpunit.de/getting-started/phpunit-8.html
I see "No tests found" warning if I specify command-line argument like the below. But this is not the intended usage of BaseTestCase.php from Slim-Skelton.
$ composer test tests/Functional/BaseTestCase.php
> phpunit 'tests/Functional/BaseTestCase.php'
PHPUnit 8.1.6 by Sebastian Bergmann and contributors.
W 1 / 1 (100%)
Time: 21 ms, Memory: 4.00 MB
There was 1 warning:
1) Warning
No tests found in class "Tests\Functional\BaseTestCase".
WARNINGS!
Tests: 1, Assertions: 0, Warnings: 1.

PHPUnit test error, cannot find class

I'm new in PHPUnit and unit-testing, so I was install PHPUnit and phar via composer and everything had been going fine until I was try to start my simple test. I'm using PhpStorm where I can see all classes were autoload, but when I trying to start my test I got an error:
Fatal error: Class 'PharIo\Manifest\Simple' not found in C:\xampp\htdocs\mydocs\
I don't understand why he is looking for It in folder upper than PHPUnit is exists ?
I was trying to configure autoload section in composer.json and checking settings in phpunit.xml but nothing works.
Add:
I have to reinstall PHPUnit without PharIO, so now I have a little bit of progress, now I have a situation where I can test my class if I make require_once line with a name of the tested class. It looks like:
require_once '../src/Simple.php';
class SimpleTest extends PHPUnit_Framework_TestCase
{
public function testAdd() {
$sum = new Simple();
$this->assertEquals(5, $sum->add(2, 3));
}
}
So my simple class is:
class Simple {
public function add($a, $b) {
return (int) $a + (int) $b;
}
}
But, of course, I want to use namespaces. I try to make some changes based on this question: Autoloading classes in PHPUnit using Composer and autoload.php (I was try even use that repo for test, but an error is still exists) but nothing works for me. I was try to edit my autoload section in the composer.json like this
"autoload": {
"psr-4": {
"app\\": "src/"
}
},
But an error is still exists, another words autoload cannot see It. I was create phpunit.xml and phpunit.dist.xml with a same settings
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
backupGlobals="true"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php">
<testsuites>
<testsuite name="The project's test suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
and I made tests/bootstrap.php too with
require_once '../vendor/autoload.php';
I know this is an old question, but maybe you need to do
composer dump-autoload for composer to generate the map of classes.
I wasted 30mins trying to understand why PHPUnit was giving me:
Cannot stub or mock class or interface XXX because it doesn't exists
You should specify the script with autoloading classes.
You can either specify the file with autoloading in XML-file, as suggested in the other answer, or just by specifying --bootstrap option in your command to run tests:
phpunit --bootstrap vendor/autoload.php tests
Composer's autoload relies on configuration located in the vendor/autoload.php file which needs to be loaded at some point in your execution thread. You application already includes this and that's why it works, but the tests use a different entry point so you need to configure it with a file called phpunit.xml.dist.
Assuming your file structure is something like:
app/
src/
tests/
bootstrap.php <- create it in your test folder
vendor/
...
composer.json
composer.lock
phpunit.xml.dist <- create it if does not exist
You can see the various options here, but for a basic config, you can use this.
File phpunit.xml.dist:
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
backupGlobals="true"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php">
</phpunit>
File tests/bootstrap.php:
require_once '../vendor/autoload.php';
You should run phpunit from the root.

phpunit extend test suite

I extended PHPUnit_Framework_TestSuite to overwrite the setUp and tearDown methode because I need PHPUnit to do some operation before and after a suite of test. (the test are in multiple TestCase class.)
class MyTestSuite extends PHPUnit_Framework_TestSuite {
protected function setUp()
{
//do some stuff before all tests are run
}
protected function tearDown()
{
//do some stuff after all tests are run
}
}
How in a xml config file do I tell phpunit to use this TestSuite class and then bound the testCase class to it? All I can find are example like this which doesnt seem like specify which test suite class phpunit shall use.
<phpunit>
<testsuites>
<testsuite name="money">
<file>tests/IntlFormatterTest.php</file>
<file>tests/MoneyTest.php</file>
<file>tests/CurrencyTest.php</file>
</testsuite>
</testsuites>
</phpunit>
In https://phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.testsuites there is this information:
The <testsuites> element and its one or more <testsuite> children can
be used to compose a test suite out of test suites and test cases.
I use Ecomdev_PHPUnit in Magento, which does subclass like you want, look at its xml:
<testsuite name="Magento Test Suite">
<file>app/code/community/EcomDev/PHPUnit/Test/Suite.php</file>
</testsuite>
So just pass the test suite complete path I guess!

How do I run all my PHPUnit tests?

I have script called Script.php and tests for it in Tests/Script.php, but when I run phpunit Tests it does not execute any tests in my test file. How do I run all my tests with phpunit?
PHPUnit 3.3.17, PHP 5.2.6-3ubuntu4.2, latest Ubuntu
Output:
$ phpunit Tests
PHPUnit 3.3.17 by Sebastian Bergmann.
Time: 0 seconds
OK (0 tests, 0 assertions)
And here are my script and test files:
Script.php
<?php
function returnsTrue() {
return TRUE;
}
?>
Tests/Script.php
<?php
require_once 'PHPUnit/Framework.php';
require_once 'Script.php'
class TestingOne extends PHPUnit_Framework_TestCase
{
public function testTrue()
{
$this->assertEquals(TRUE, returnsTrue());
}
public function testFalse()
{
$this->assertEquals(FALSE, returnsTrue());
}
}
class TestingTwo extends PHPUnit_Framework_TestCase
{
public function testTrue()
{
$this->assertEquals(TRUE, returnsTrue());
}
public function testFalse()
{
$this->assertEquals(FALSE, returnsTrue());
}
}
?>
Php test's filename must end with Test.php
phpunit mydir will run all scripts named xxxxTest.php in directory mydir
(looks likes it's not described in the phpunit documentation)
I created following phpunit.xml and now atleast I can do phpunit --configuration phpunit.xml in my root directory to run the tests located in Tests/
<phpunit backupGlobals="false"
backupStaticAttributes="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Tests">
<directory suffix=".php">Tests</directory>
</testsuite>
</testsuites>
</phpunit>
I think forPHPUnit to decide to automatically run it it must follow a filename convention: somethingTest.php.
You think they would have documented this. I just looked through the manual, and they say you can pass a directory, but not really how to do it.
Perhaps your class name has to match the basename (everything but the ".php") of your test scripts filename?
<?php
//Files required for phpunit test
require_once 'PHPUnit/Framework.php';
//Knowing the drupal environment
require_once './includes/bootstrap.inc'; //initialize the Drupal framework
//Loading the drupal bootstrap
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
//Helper file
include_once 'helper.inc';
//Including inc file of addresses module
include_once(module_load_include('inc','addresses_user','addresses_user'));
class addresses_test extends PHPUnit_Framework_TestCase {
protected $uid;
protected function setUp()
{
$this->uid = 1;
}

Categories