PHP SimpleTest - Using multiple test cases
-
07-02-2021 - |
Question
I have been trying to use SimpleTest to begin unit testing my code, I have a working test that works on its own, but I want to use a single directory that will contain a range of tests, and a TestSuite will run all of those tests, my working test is:
<?php
require_once(dirname(__FILE__) . '/../simpletest/autorun.php');
define("ROOT",'/var/web/trunk/');
require_once('/usr/share/log4php/src/main/php/Logger.php');
class TestBayCrazy extends UnitTestCase {
function testDatabase () {
require_once(ROOT.'includes/libs.inc.php');
$database = new Database();
$this->assertTrue($database->connected == TRUE);
$database = new Database('a','b','c','d','e');
$this->assertTrue($database->connected == FALSE);
$database = null;
}
}
My TestSuite is:
<?php
require_once(dirname(__FILE__) . '/simpletest/autorun.php');
define("ROOT",'/var/web/trunk/');
require_once('/usr/share/log4php/src/main/php/Logger.php');
class AllTests extends TestSuite {
function AllTests() {
$this->TestSuite('All Tests');
$this->addFile('tests/testDatabase.php');
$this->addFile('tests/testSession.php');
$this->addFile('tests/testValidate.php');
}
}
But this returns the following when run:
2011/10/05 12:37:47 [error] 3242#0: *309 FastCGI sent in stderr: "PHP Fatal error:
Call to a member function getDumper() on a non-object in
/var/web/trunk/private/simpletest/test_case.php on line 316 PHP Stack trace: PHP
1. simpletest_autorun() /var/web/trunk/private/simpletest/autorun.php:0 PHP
2. run_local_tests() /var/web/trunk/private/simpletest/autorun.php:28 PHP
3. TestSuite-run() /var/web/trunk/private/simpletest/autorun.php:52 PHP
4. TestSuite->run() /var/web/trunk/private/simpletest/test_case.php:563 PHP
5. TestSuite->run() /var/web/trunk/private/simpletest/test_case.php:563 PHP
6. TestSession->testSession() /var/web/trunk/private/simpletest/test_case.php:559 PHP
7. UnitTestCase->assertIsA() /var/web/trunk/private/tests/testSession.php:10 PHP
8. SimpleTestCase->assert() /var/web/trunk/private/simpletest/unit_tester.php:110"
while reading response header from upstream, client: 0.0.0.0,
server: example.com, request: "GET /private/unittest.php HTTP/1.1",
upstream: "fastcgi://127.0.0.1:9001", host: "0.0.0.0
So, what on earth am I doing wrong? I've only found examples on how to do a testSuite, not how a test needs to be different when it's a member of a suite, rather than stand alone (may be down to my lack of familiarity with the language of unit testing).
Solution
We actually use GroupTest instead of a Suite
$dbgroup = new GroupTest("running database tests");
$dbgroup->addTestFile('tests/testDatabase.php');
$dbgroup->run(new HtmlReporter());
It does the job well enough...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow