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
Related
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 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');
}
}
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 was writing the code auto generated drop like google search help and trying to print the values of that auto generated drop down as output.
in selenium WebDriver to catch multiple elements which will match with a xpath locator we have to use findElementsBy() function,
the code i have written is given below
<?php
require_once 'www/library/phpwebdriver/WebDriver.php';
class PHPWebDriverTest extends PHPUnit_Framework_TestCase {
protected $webdriver;
protected function setUp() {
$this->webdriver = new WebDriver("localhost", 4444);
$this->webdriver->connect("firefox");
}
protected function tearDown() {
// $this->webdriver->close();
}
public function testgooglesearch() {
$this->webdriver->get("http://google.com");
$element=$this->webdriver->findElementBy(LocatorStrategy::name, "q");
$element->sendKeys(array("selenium" ) );
$result=$this->webdriver->findElementsBy(LocatorStrategy::xpath,"//*[#id=\'gsr\']/table/tbody/tr/td[2]/table/tbody/tr[*]/td/");
echo $countresult=count($result);
}
}
?>
As per the binding findElementsBy() function will suppose to return an array. so when i am trying to count the array length a error is returning .
error : Trying to get property of non -object.
can any one please help me how i can proceed.
At Last i am able to find the solution of the problem on my own .
My main motto was to print the value of the auto generated drop down
The main problem was the running speed of the test .As the speed of test was fast so " findElementsBy " function was not able to work properly.
so i used sleep command just before that function so that it can work properly .
The code which work properly for me is given below
<?php
require_once "/phpwebdriver/WebDriver.php";
class WebdriverTest extends PHPUnit_Framework_TestCase
{
protected $webdriver;
protected function setUp()
{
$this->webdriver=new WebDriver("localhost", 4444);
$this->webdriver->connect("firefox");
}
protected function tearDown() {
$this->webdriver->close();
}
public function testSearch()
{
$this->webdriver->get("http://google.com");
$element=$this->webdriver->findElementBy(LocatorStrategy::name,"q");
$element->sendKeys(array("selenium" ) );
sleep(2);
$result=$this->webdriver->findElementsBy(LocatorStrategy::xpath,"//td[#class='gssb_a gbqfsf']");
$countresult=count($result);
echo "Records Count = ". $countresult ."\n";
$x=1;
$y=0;
echo "\n";
while($y<$countresult)
{
$output=$result[$y]->getText();
echo $output."\n";
$x++;
$y++;
}
$r=$this->webdriver->findElementBy(LocatorStrategy::xpath,"//div[#class='gbqlca']");
$r->click();
}
}
?>
This might be useful for you.
Click here
FYI : The above implementation is in java-selenium bindings.
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');
}
}
?>