For our project I have created a framework on top of the PHPUnit framework which helps us in some of the common tasks in writing unit tests.
This custom framework inherits from PHPUnit_Framework_TestCase and then modifies the mySetup() and adds bunch of useful functions for our code.
<?php
class OurUnitTestFramework extends PHPUnit_Framework_TestCase {
public $dbMock;
protected function mySetup (..) { ... }
protected function testHelper () { ... }
}
?>
Now in our test code we just extend OurUnitTestFramework and then write the tests.
<?php
require_once ("OurUnitTestFramework");
class DatabaseConnectionTest extends OurUnitTestFramework {
parent::setUp (..) { ... }
public function testSomeThing () { ... }
public function testSomeOtherThing () { ... }
}
?>
Till now we were running all the unit tests through Jenkins and it still is running fine but now when we try to run the tests in a folder it fails. All the tests inside the folder/sub-folder runs successfully but there is one failure:
[sumit#dev model]$ phpunit database
PHPUnit 3.5.14 by Sebastian Bergmann.
F........
Time: 0 seconds, Memory: 10.50Mb
There was 1 failure:
1) Warning
No tests found in class "OurUnitTestFramework".
FAILURES!
Tests: 9, Assertions: 30, Failures: 1.
I have a directory database which has sub directories and all the tests passes from that folder and its subfolder but I get failure from OurUnitTestFramework saying there is no tests found in this custom framework. So I am not able to understand why phpunit is running unit tests on the file which is included/extended in the test file?
We can simply choose to ignore this one error but I wanted to know if okay to leave like this or is there something that I need to configure to make it pass.
Thanks
Make 'OurUnitTestFramework' an abstract class.
Related
I am developping a phpcas bundle using guard component of Symfony framework. My bundle is working but I want to do some unit tests. I want to test my CasAuthenticator. PhpCAS library is using static method. So I decided to use Mock Aspect to mock it.
I configured Aspect, but I still have a bug.
Here is a simplified test which is running but failing.
Expected PhpCAS::setDebug to be invoked but it never occurred. Got:
C:\wamp64\www\casguard\casguard\vendor\codeception\aspect-mock\src\AspectMock\Proxy\Verifier.php:64
C:\wamp64\www\casguard\casguard\Tests\SimpleTest.php:32
//root_dir/Tests/SimpleTest.php
namespace AlexandreT\Bundle\CasGuardBundle\Tests;
use PHPUnit\Framework\TestCase;
use AspectMock\Test as test;
use PhpCAS;
class SimpleTest extends TestCase
{
public function testAspectMock()
{
$phpCas = test::double('PhpCAS', ['setDebug' => function () {
echo 'YES I CALL THE MOCKED Debug function';
}]);
PhpCAS::setDebug();
$phpCas->verifyInvoked('setDebug', false);
}
protected function tearDown()
{
parent::tearDown();
test::clean();
}
}
Output does not contain YES I CALL THE MOCKED Debug function, so I think that the PhpCAS is not mocked by Aspect.
I carefully read this documentation and I configured my bootstrap file like this:
//root_dir/Tests/bootstrap.php
include __DIR__.'/../vendor/autoload.php'; // composer autoload
$kernel = \AspectMock\Kernel::getInstance();
$kernel->init([
'debug' => true,
'includePaths' => [
__DIR__.'/../vendor/jasig/phpcas',
],
]);
As you can read, I added the vendor directory where the Cas.php declares the PhpCAS class. But it doesn't change anything. I made some tests: the bootstrap.php file is loaded by phpunit.
What did I miss in my Mock Aspect configuration?
I added a line to configure cache directory in my bootstrap.php file
and I can see that the Cas.php was well included.
But I still had the bug. When I was exploring the cached file, I discovered that the phpcas library doesn't respect the PSR0 convention. First letter of phpCAS Class isn't capitalized.
So I edited my test:
public function testAspectMock()
{
//$phpCas = test::double('PhpCAS', ['setDebug' => function () {
// ^
// |
// v
$phpCas = test::double('phpCAS', ['setDebug' => function () {
echo 'YES I CALL THE MOCKED Debug function';
}]);
phpCAS::setDebug(); //phpCAS instead of PhpCAS
$phpCas->verifyInvoked('setDebug', false);
//And I had an assertion else test is marked as risky.
self::expectOutputString('YES I CALL THE MOCKED Debug function');
}
2 letters... 2 hours of debugging... #Grrrr
I have a simple PHPUnit/Symfony WebTestCase set up to test our site's login form.
$form = $crawler->filter("#register")->form();
// set form values
$crawler = $this->client->submit($form);
The form will submit to /register, and then redirect to /registered on success (and 200/OK back to /register on failure).
If I use either $this->client->followRedirects(); before block above, or $this->client->followRedirect(); after the submit, I get a segfault. There's really no indication of where the segfault is taking place.
Something else of note: if I run JUST the tests in this tests parent class (2 tests) i.e. using --filter [THE CLASS] it runs fine. If I try to run this test, along with the full suite (~15 tests), I get the segfault.
I've tried giving phpunit more memory using the -d flag, but that doesn't really help.
The problem can be in controller work in conjunction with other component.
I suggest you to use the Process Isolation in PHPUnit so you can run the critical test in a separate PHP process. As Example, you can use the following annotations for:
Indicates that all tests in a test class should be run in a separate PHP process:
/**
* #runTestsInSeparateProcesses
*/
class MyTest extends PHPUnit_Framework_TestCase
{
// ...
}
Indicates that a test should be run in a separate PHP process:
class MyTest extends PHPUnit_Framework_TestCase
{
/**
* #runInSeparateProcess
*/
public function testInSeparateProcess()
{
// ...
}
}
Hope this help
I'm running into an error while trying to run some simple tests with SimpleTest for PHP.
Currently, I'm extending the class UnitTestCase as per the documentation. I'm trying to test different aspects of my class within a method. Here is my class:
<?php
class SimpleClass extends UnitTestCase {
public function __construct() {
$this->test();
}
private function test() {
$x = true;
$this->assertTrue($x, 'x is true');
}
}
I've tried extending the TestSuite class and using the syntax in the documentation but I got the same error:
Fatal error: Call to a member function getDumper() on a non-object in /simpletest/test_case.php on line 316
Any ideas on how I could do this or am I approaching class testing wrong?
Don't use a constructor in your test!
SimpleTest allows you to create classes with methods. If their name starts with "test", it is automatically recognized as a testing method that will get called if you start the test suite.
You created a constructor which calls your test method, and does an assertion without all the setup taking place, so SimpleTest does not have a reporter class that is needed to wrap it's test findings into a nice output.
Read the tutorial more closely, and you'll find some hints on how to set up a test suite or how to start a single test:
Let us suppose we are testing a simple file logging class called Log in classes/log.php. We start by creating a test script which we will call tests/log_test.php and populate it as follows...
Code example adapted from the documentation:
<?php
require_once('simpletest/autorun.php');
require_once('../classes/log.php');
class TestOfLogging extends UnitTestCase {
function testLogCreatesNewFileOnFirstMessage() {
$this->assertTrue(true);
}
}
?>
Note there is no constructor, and the autorun file will take care to run the test if this file is executed with PHP.
Actually I exposed my question here : test a repository in symfony
But When setting a test for my repository, I get the following result:
Time: 4 seconds, Memory: 18.25Mb
OK, but incomplete or skipped tests!
Tests: 76, Assertions: 183, Skipped: 9.
Is the test ok or not ok and what does assertion mean?
Why does he skip some tests??
Is the test ok?
Yes, the tests are OK ("OK, but incomplete or skipped tests").
what does assertion mean?
Assertions are expectations that are done in a test. For instance:
class CalculatorTest extends \PHPUnit_Framework_TestCase
{
public function testSum()
{
$calculator = new Calculator();
$this->assertEquals(5, $calculator->sum(2, 3));
$this->assertEquals(19, $calculator->sum(14, 2, 3));
}
}
In this code, we have 1 test (testSum) and 2 assertions (2 times assertEquals).
Why does he skip some tests?
Symfony relies on some third party libraries or PHP extensions which may not be installed. When it isn't installed, you can't test it. Thus markes Symfony the test as skipped. For instance:
class LocaleTypeTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!extension_loaded('php_intl')) {
$this->markTestSkipped('Failed to run LocaleType tests, as intl is missing.');
}
}
}
I have been going mad with this, I have found some threads on this but none of them solve my problem.
I have my bootstrap.php file for testing which looks like this
define("PHPUNIT", true);
require_once("/Applications/XAMPP/xamppfiles/htdocs/Common/test/public/index.php");
My index.php file does not call run() on the application object if PHPUNIT is defined. Is this correct to stop the application from being dispatched?
I have a test file called homepage.php, this contains a class which extends Zend_Test_PHPUnit_ControllerTestCase - this class cannot be found when I run phpunit, even when I include the file with an absolute path.
Running file_exists() on the same string returns true. I run phpunit using the --bootstrap flag with my bootstrap.php file on the test file homepage.php which is below
require_once("/Applications/XAMPP/xamppfiles/htdocs/libs_php/ZendFramework-1.10.7/library/Zend/Test/PHPUnit/ControllerTestCase.php");
class HomepageControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
public function appBootstrap()
{
$controller = $this->getFrontController();
$controller->registerPlugin(new Default_Controller_Plugin_Initialise());
}
public function setUp()
{
$this->bootstrap = array($this, 'appBootstrap');
parent::setUp();
}
public function testA()
{
$this->dispatch('/');
$this->assertController('index');
}
}
The output from phpunit I get is below, how can the class not be found? It doesn't look like a PHP error, but something from PHPUnit - there is no line number?
PHPUnit 3.3.2 by Sebastian Bergmann.
Class Zend_Test_PHPUnit_ControllerTestCase could not be found in /Applications/XAMPP/xamppfiles/htdocs/Common/test/tests/library/homepage.php
Thanks for any help on this!