I have the following Selenium test in PHP:
<?php
require_once('PHPUnit/Extensions/SeleniumTestCase.php');
class Example extends PHPUnit_Extensions_SeleniumTestCase
{
protected function setUp()
{
$this->setBrowser("*chrome");
$this->setBrowserUrl("http://change-this-to-the-site-you-are-testing/");
}
public function testMyTestCase()
{
$this->open("/frontend_dev.php");
try {
$this->assertTrue($this->isTextPresent("Local Coupons"));
} catch (PHPUnit_Framework_AssertionFailedError $e) {
array_push($this->verificationErrors, $e->toString());
}
}
}
When I try to run it (by running "php filename.php") I get the error:
PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /usr/share/php/PHPUnit/Extensions/SeleniumTestCase.php on line 60
Which makes sense because the class isn't defined anywhere, but why not? I installed PHPUnit. It seems a little weird that a class called PHPUnit_Framework_TestCase wouldn't be included with PHPUnit. Any ideas?
You should read the docs for PHPUnit for the command line test runner. You can also just run 'phpunit --help' from the command line. I believe you'll find that you need to instead run something like
phpunit path/to/filename.php
Related
Try write a unit test and i need do sql query
class UpdateThrowsTest extends TestCase
{
protected $bgame;
protected $game_id = 95;
public function setUp(){
$game = new Game();
$game = $game::find($this->game_id);
}
}
and then i write "phpunit" in console and try exception
Call to a member function connection() on null.
If anyone bounce to this error during test with Laravel 6 project.
Try to check if the extends TestCase is using the right TestCase.
It could be due to Laravel 6 make:test generated test using the wrong TestCase.
Change
use PHPUnit\Framework\TestCase;
To
use Tests\TestCase;
The problem should solve.
I had this error on laravel 7 (nothing worked even php artisan serve) and fixed it with
composer dumpautoload
Before that update my vendor with composer update and then everything worked fine.
I'm just test playing with Php unit.
Here is my DependencyFailureTest class:
require_once '../vendor/autoload.php';
use PHPUnit\Framework\TestCase;
class DependencyFailureTest extends \PHPUnit\Framework\TestCase
{
public function testOne()
{
$this->assertTrue(false);
}
/**
* #depends testOne
*/
public function testTwo()
{
}
}
But on running the command phpunit --verbose DependencyFailureTest it throws
Argument #3 (No Value) of PHPUnit_TextUI_ResultPrinter::__construct() must be a value from "never", "auto" or "always".
Can anybody give an explanation for this issue?
It must be a configuration issue. I copied your code and ran it on the command line with verbose and it worked fine with version 5.4.6.
I would reinstall phpunit and ensure you have the latest version.
Also, their sample test case from their Getting Started page is:
<?php
use PHPUnit\Framework\TestCase;
class MoneyTest extends TestCase
{
// ...
public function testCanBeNegated()
{
// Arrange
$a = new Money(1);
// Act
$b = $a->negate();
// Assert
$this->assertEquals(-1, $b->getAmount());
}
// ...
}
https://phpunit.de/getting-started.html
Notice the difference in your extension usage, although I don't think it is an issue, if you use their declaration as stated, it helps to isolate the problem.
I ran into this. Was passing --colors=true, but that is incorrect.
For a given project I have two php files to run php unittests, which are located in subdirectories.
The first file (AllTestSuite.php) looks like this:
<?php
require_once dirname(__FILE__).'/unittest.inc.php';
class AllTestsSuite {
public static function suite(){
return new GlobTestsSuite(dirname(__FILE__), "/*/*Suite.php");
}
}
?>
and the second file (unittest.inc.php) looks like this:
<?
class GlobTestsSuite extends PHPUnit_Framework_TestSuite {
public function __construct($sDirectory, $sExpression){
parent::__construct();
foreach (glob("$sDirectory/$sExpression") as $sTest){
require_once($sTest);
$this->addTestSuite(basename($sTest, ".php"));
}
}
}
?>
The tests itself are invoked (on Ubuntu, phpunit Version 3.7.27) as follows:
phpunit AllTestsSuite ./AllTestsSuite.php
which gives the error message
PHP Fatal error: Class 'GlobTestsSuite' not found in /home/.../AllTestsSuite.php on line 6
I do not understand what is going here. The file AllTestsSuite.php imports the other file in which the class GlobTestSuite is defined. How to fix this problem?
A Simpletest test in MyTest.php that runs with no errors:
require_once ('simpletest/autorun.php');
class MyTest extends UnitTestCase {
function test() {
//Test...
}
}
A SimpleTest test suite
require_once ('simpletest/autorun.php');
class AllTests extends TestSuite {
function __construct() {
$this->TestSuite('All tests');
$this->addFile('MyTest.php');
}
}
The error:
Fatal error: Cannot redeclare simpletest_autorun() (previously declared in /Library/WebServer/Documents/Option/tests/simpletest/autorun.php:26) in /Library/WebServer/Documents/option/tests/simpletest/autorun.php on line 34
The (horrible) solution, change MyTest.php to:
if (!function_exists("simpletest_autorun")) {
require_once ('simpletest/autorun.php');
}
class MyTest extends UnitTestCase {
function test() {
//Test...
}
}
It appears that this example follows the documentation, it doesn't or SimpleTest has this bug?
I can only assume you have multiple copies of the SimpleTest autorun.php script.
Performing the require in your test case file attempts to include a second autorun.php file (different to the one included in your test suite) which defines simpletest_autorun() for the second time.
The fact that the line numbers don't match up adds credence to my assumption.
Looking at the full paths in the error messages should show you what's up.
Determine which copy you want to keep and remove the other, updating any incorrect paths in your code or configuration, including the include_path in php.ini.
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.