I am looking at PHPunit for running Selenium tests and can run tests fine. What's missing though is the ability to run a function before the tests are run (let's say to set up some config).
<?php
require_once 'SeleniumTest.php';
class MyTest extends SeleniumTest
{
public function testTest1()
{
$this->runSelenese(__DIR__ . '/test1.html');
}
public function testTest2()
{
$this->runSelenese(__DIR__ . '/test2.html');
}
}
The config file is just a regular html file, but needs to be run once before anything else is run
Edit
Another read of the setUp() function has led me to believe that my tests should be setup as follows.
What I want to simulate is a user logging in, setting up some config, running the test and then logging out of the application at the end of the test.
This is what I have come up with; please advise on what I have done wrong, as the tests don't run at the moment. All tests timeout: is this how I should be approaching this?
<?php
require_once 'SeleniumTest.php';
class MyTest extends SeleniumTest
{
public function setUp()
{
$login_path = '/login_path/Login.html';
$logout_path = '/logout_path/Logout.html';
$config_path = '/config_path/config.html';
$this->runSelenese($login_path);
$this->runSelenese($config_path);
$this->runSelenese($logout_path);
}
public function testTest1()
{
$this->runSelenese(__DIR__ . '/test1.html');
}
public function testTest2()
{
$this->runSelenese(__DIR__ . '/test2.html');
}
}
Related
I'm creating a console application to help me develop my website in php.
The console application runs in a while loop, prints a menu with options and I'll input a number to tell the application what I want it to do. In One of my option I am running phpunit test, when I run the test it works but when I update my test and press my option again, phpunit displays "No tests executed!". When I restart the console app it will work but i still have the same problem when I try to run the unit test second time.
Here is a code example
I have tired instantiating a new phpunit command and then use the run method.
I have tried breaking out of the loop and call begin again.
require_once __DIR__ . '/vendor/autoload.php';
public function printMenu()
{
echo "*******menu*****";
echo "\r\n";
echo "option 1: do something";
echo "\r\n";
echo "option 2: Do unit test:";
echo "\r\n";
echo "option 3: Exit";
echo "\r\n";
}
public function begin(){
while(true){
$this->printMenu();
$input = trim(fgets(STDIN));
if($input == 1){
$this->doSomething();
}else if($input ==2){
PHPUnit\TextUI\Command::main(false);
}else if($input == 3){
break;
}
}
}
//Sample test class (just an example)
use PHPUnit\Framework\TestCase;
class SampleTest extends TestCase
{
public function testTrueAssertsToTrue(): void {
$this->assertTrue(true);
$this->assertTrue(true);
$this->assertTrue(false);
$this->assertTrue(true);
$this->assertTrue(true);
}
}
//update will be
use PHPUnit\Framework\TestCase;
class SampleTest extends TestCase
{
public function testTrueAssertsToTrue(): void
{
$this->assertTrue(true);
$this->assertTrue(true);
$this->assertTrue(true);
$this->assertTrue(true);
$this->assertTrue(true);
}
}
I managed to solve it by changing
PHPUnit\TextUI\Command::main(false);
to
./vendor/bin/phpunit
I am following the examples in the PHPUnit manual. See the two Test files below. I am running the tests in Eclipse PDT with PTI installed. I see the following problems:
When running DependencyFailureTest, it does not recognize it as being a test. Is does not run anything.
When running MultipleDependenciesTest, it is running and mentions that all three test cases pass, as it should. However, if I then change the expected outcome in the function testConsumer into array('first', 'third'), it still mentions that all test cases pass, although one of them should clearly fail. Also, when I change one of the assertions into $this->assertTrue(FALSE);, I expect a failed and a skipped test case, but again all test cases pass.
Has anyone experienced something similar, and solved this?
DependencyFailureTest
<?php
class DependencyFailureTest extends PHPUnit_Framework_TestCase
{
public function testOne()
{
$this->assertTrue(FALSE);
}
/**
* #depends testOne
*/
public function testTwo()
{
}
}
?>
MultipleDependenciesTest
<?php
class MultipleDependenciesTest extends PHPUnit_Framework_TestCase
{
public function testProducerFirst()
{
$this->assertTrue(true);
return 'first';
}
public function testProducerSecond()
{
$this->assertTrue(true);
return 'second';
}
/**
* #depends testProducerFirst
* #depends testProducerSecond
*/
public function testConsumer()
{
$this->assertEquals(
array('first', 'second'),
func_get_args()
);
}
}
?>
I don't have a good answer yet, only some black magic voodoo. I noticed that for running it in the command line, I need to include the class under test.
<?php
require_once ('path/to/Car.php')
class CarTest extends PHPUnit_Framework_TestCase {
...
For running it in PTI, I mention the file in the Bootstrap file in the PHPUnit preferences. Therefore, this reuire_once statement is not necessary. But worse however, this require_once statement causes the test not to run!
Something strange that I noticed is that at one time, my tests were not running, even without the require_once statement. In the PHPUnit preferences, I had the option Do not check for equal namespaces while searching for php/test case classes enabled. I disabled it, and it worked. I enabled it again, and it still worked.
Phpunit doesnot show any thing (run 0/0)
Console Error:
Fatal error: Declaration of PHPUnitLogger::addFailure(Test $test, AssertionFailedError $e, $time): void must be compatible with PHPUnit\Framework\TestListener::addFailure(PHPUnit\Framework\Test
$test, PHPUnit\Framework\AssertionFailedError $e, float $time): void in
C:\Users\xxx\AppData\Local\Temp\phpunit_printer\PHPUnitLogger.php(415): eval()'d code on line 1
TestCase
<?php
namespace PHPUnit\Framework;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
class SeleniumTest extends \PHPUnit_Framework_TestCase
{
protected $webDriver;
public function setUp()
{
// system . property_exists("Facebook\WebDriver\Firefox\FirefoxDriver", "C:\rc\geckodriver\geckodriver");
// System . set("Webdriver.gecko.driver", "C:\rc\geckodriver\geckodriver");
$this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', DesiredCapabilities::firefox());
$this->webDriver->manage()
->window()
->maximize();
$this->webDriver->get('http://localhost/farmer/login');
// $this->webDriver->get("www.gmail.com");
}
public function testLoginPass()
{
$this->webDriver->get('http://localhost/farmer/login');
$this->webDriver->findElement(WebDriverBy::name('username'))->sendKeys(' correct');
$this->webDriver->findElement(WebDriverBy::id('password'))->sendKeys('password');
$this->webDriver->findElement(WebDriverBy::name('btn-login'))->click();
$content = $this->webDriver->findElement(WebDriverBy::tagName('body'))->getText();
$this->assertContains('Dashboard', $content);
}
public function testLoginFail()
{
$this->webDriver->get('http://localhost/farmer/login');
$this->webDriver->findElement(WebDriverBy::name('mobile'))->sendKeys("800000000000");
$this->webDriver->findElement(WebDriverBy::id('password'))->sendKeys("8000000000");
$this->webDriver->findElement(WebDriverBy::name('btn-login'))->click();
$content = $this->webDriver->findElement(WebDriverBy::className('help-block'))->getText();
$this->assertContains('Your Credential Doesnot Match.Please Try Again !!', $content);
}
public function tearDown()
{
$this->webDriver->quit();
}
}
?>
While MakeGood is working Properly in Eclipse (Everything is OK)
MAKEGOOD result
i created an external class called StringHelper and i putted the require into the _bootstrap.php.
I used it into my acceptance test and it didn't work:
<?php
class StringHelper {
public static function getString($length) {
return "Hello World";
}
}
_bootstrap.php
require_once 'components/StringHelper.php';
My LoginCest.php
<?php
use \AcceptanceTester;
class LoginCest
{
public function test01(AcceptanceTester $I)
{
$I->wantTo('Try to access without permission');
$I->amOnPage('#/list');
$I->waitForText('You don`t have permission.', 10, '.alert');
}
public function test02(AcceptanceTester $I)
{
$I->wantTo(StringHelper::getString(2));
SeleniumHelper::fillField($I, '#desc_login', StringHelper::getString(2));
$I->click("#btn-enter");
$I->waitForText('Please, fill the login field', 10, '.alert');
}
}
My returned message:
Acceptance Tests (2) --------------------------------------------
Trying to Try to access without permission (LoginCest::test01) Ok
Trying to test02 (LoginCest::test02) Ok
Here in "Trying to test02" why doesn't appear "Hello World"?
Try to write a module/helper for this function as codeception queues the commands, so when your command is executed codeception is not "listening".
Refer here: http://codeception.com/docs/06-ModulesAndHelpers
I have written some test cases and want to try them out with PHPUnit. However, it does not work.
If I run phpunit CategoryTest it outputs:
PHPUnit 3.7.14 by Sebastian Bergmann.
If I do phpunit --log-json error.log CategoryTest, error.log file displays:
{"event":"suiteStart","suite":"CategoryTest","tests":5}
{"event":"testStart","suite":"CategoryTest","test":"CategoryTest::test__construct"}
So, it finds that there are 5 tests in the file, starts doing the first one and for no reason stops. Is there any log where I could find a reason why it would not continue execution?
Also, if I run test on some other file, say phpunit --log-json error.log UserTest, the shell does not display any output and neither does error.log file.
I tried reinstalling it, as it was suggested in one of the other similar questions, but it didn't do anything.
Any ideas how I could fix it?
require_once '../Category.class.php';
require_once '../../db_connect.php';
require_once 'PHPUnit/Framework/TestCase.php';
class CategoryTest extends PHPUnit_Framework_TestCase {
private $Category;
protected function setUp() {
parent::setUp ();
$this->Category = new Category(0, $mdb2);
}
protected function tearDown() {
$this->Category = null;
parent::tearDown ();
}
public function __construct() {
}
public function test__construct() {
$this->markTestIncomplete ( "__construct test not implemented" );
$cat = $this->Category->__construct(0, $mdb2);
$this->assertInstanceOf('Category', $cat);
}
public function testReturnID() {
$this->markTestIncomplete ( "returnID test not implemented" );
$id = $this->Category->returnID();
$this->assertEquals(0, $id);
}
...
}
Variable $mdb2 comes from the db_connect.php file.
I figured it out. The problem was that I included a variable from outside of a class.
You shouldn't overwrite the __construct() method in your TestCase. The constructor sets up the mock generator and some other mandatory thing for the test, so you get a lot of strange behaviour and unwanted side effects if you overwrite the constructor. The setUp() method is the special method you should use to initialize your test.
I have a simpletest suite I've been working on writing for some of my recent API wrapper code in PHP. But every time I run the test, it runs all of the tests twice.
My calling code:
require_once(dirname(__FILE__) . '/simpletest/autorun.php');
require_once('CompanyNameAPI.php');
$test = new TestSuite('API test');
$test->addFile(dirname(__FILE__) . '/tests/authentication_test.php');
if (TextReporter::inCli()) {
exit ($test->run(new TextReporter()) ? 0 : 1);
} else {
$test->run(new HtmlReporter());
}
authentication_test.php looks like:
class Test_CallLoop_Authentication extends UnitTestCase {
function test_ClassCreate(){
$class = new CallLoopAPI();
$this->assertIsA($class, CallLoopAPI);
}
//More tests
}
There aren't any more includes to autorun.php or other calls to simpletest within authentication_test.php either.
Ideas?
You should change your calling code like this:
require_once(dirname(__FILE__) . '/simpletest/autorun.php');
require_once('CompanyNameAPI.php');
$test = new TestSuite('API test');
$test->addFile(dirname(__FILE__) . '/tests/authentication_test.php');
autorun.php file executes automatically your tests calling run() methods implicitly, when you call run() method you execute tests again.
From simpletests documentation, you should use the static method prefer(REPORTER)
<?php
require_once('show_passes.php');
require_once('simpletest/simpletest.php');
SimpleTest::prefer(new ShowPasses());
require_once('simpletest/autorun.php');
class AllTests extends TestSuite {
function __construct() {
parent::__construct('All tests');
$this->addFile(dirname(__FILE__).'/log_test.php');
$this->addFile(dirname(__FILE__).'/clock_test.php');
}
}
?>