How to stop PHPUnit from adding included/required files as part of the testsuite? - netbeans

I tried to follow the PHPUnit manual on how to setup a testsuite with a custom test execution order. I now realized that i only need these lines and some includes to get the suite working:
public static function suite()
{
$suite = new PHPUnit_Framework_TestSuite('Package');
return $suite;
}
But when i use the above lines the test execution order is defined by the sort order of my includes. And when i try to change it via suite() as followes the tests are executed twice, first in the sort order as defined by suite() and after that in the sort order of the includes:
public static function suite()
{
$suite = new PHPUnit_Framework_TestSuite('Package');
$suite->addTestSuite('Package_Class1Test');
$suite->addTestSuite('Package_Class2Test');
$suite->addTestSuite('Package_Class3Test');
return $suite;
}
Includes are done by:
require_once 'Package/Class3Test.php';
require_once 'Package/Class2Test.php';
require_once 'Package/Class1Test.php';
Result (test execution order):
1) Class1Test
2) Class2Test
3) Class3Test
4) Class3Test
5) Class2Test
6) Class1Test
I am using Netbeans 7.0beta to run the PHP 5.3.5 / PHPUnit 3.5.11 on windows 7.
I read the phpunit manual (http://www.phpunit.de/manual/3.5/en/organizing-tests.html) but i have no clue what i am doing wrong...
Please help.
PS: This problem can be solved by autoloading the classes.

PHPUnit 3.5 was released five years ago and has not been supported for four years. But even five years ago using PHPUnit_Framework_TestSuite objects to compose a test suite was no longer considered a good practice.
Please read the Getting Started article on the PHPUnit website as well as the chapter on organizing tests in the PHPUnit manual to learn how to properly organize your test suite.

Are you calling phpunit with the right parameters?
I have this setup, which works fine with suites.
/tests/
/tests/allTests.php
/tests/lib/
/tests/lib/libAllTests.php
/tests/lib/baseTest.php
/tests/lib/coreTest.php
/tests/lib/...
allTests.php:
require_once 'lib/libAllTests.php';
class AllTests
{
public static function suite()
{
$suite = new PHPUnit_Framework_TestSuite('Project');
$suite->addTestSuite('LibAllTests');
return $suite;
}
}
libAllTests.php:
require_once 'baseTest.php';
require_once 'coreTest.php';
class LibAllTests
{
public static function suite()
{
$suite = new PHPUnit_Framework_TestSuite('Lib');
$suite->addTestSuite('CoreTest');
$suite->addTestSuite('BaseTest');
return $suite;
}
}
From a command prompt I can call:
phpunit /tests/allTests.php: Runs all tests
phpunit /tests/lib/libAllTests.php: Runs all lib tests
phpunit /tests/lib/baseTest.php: Runs all base tests
phpunit /tests/*: Runs all tests
And in all four scenarios, the core tests are run before base tests, and no tests are repeated twice.
I'm using phpUnit 3.5.7.

Related

How to run a sub-set of TestCases using --where for nunit

For my project I want to run the exact same test cases twice, once locally and on a different VM in parallel in the cloud (Azure in my case).
I duplicated the TestCase and tagged one Category("Local") and the other Category("Cloud").
Running nunit3 from the console with --where="cat == Cloud" will thus run all TestCases of every test that has one or more TestCases tagged with Category("Cloud").
Is there a different way of only running selected TestCases by a commandline switch?
Simplified example:
[TestCase(TestName = "Canary, Run in cloud."), Category("Cloud")]
[TestCase(TestName = "Canary, Run locally."), Category("Local")]
public void Canary()
{
Assert.True(true);
}
Found a work-around.
Using --params:Cloud=true as command line argument and in the code
private bool ShallRunInCloud => TestContext.Parameters["Cloud"]?.ToLowerInvariant() == "true";

Junit xml report don't report each test case when 'Karate' parallel runner is used

I am running 5 API test scenarios using karate. When I run the test in non-parallel mode using #RunWith(Karate.class) then in xml generated by surefire-reports, all scenarios are reported individually as .
<testcase classname="[healthCheck]" name="[1:3] Check health check API returns status code 200" time="2.846"/>
<testcase classname="[healthCheckHeader]" name="[1:6] Check health check API returns status code 200" time="0.285"/>
<testcase classname="[userLogin]" name="[1:3] Check User Login API returns status code 200" time="0.108"/>
<testcase classname="[requestChaining]" name="[1:7] chain request demo" time="0.521"/>
<testcase classname="[viewRequests]" name="[1:10] Check View Requests API returns status code 200" time="0.278"/>
However, when I use karate parallel runner, then each scenario is not reported individually.
<testcase classname="demoTest.AutomationSuiteParallelCucRunner" name="testParallel" time="10.917"/>
I want to have similar report for parallel runner as generated when tests executed in non-parallel mode.
Here is the code for running tests in non-parallel mode:
#RunWith(Karate.class)
public class AutomationSuiteTest {
}
Here is the code for running tests in parallel mode:
#CucumberOptions(tags = {"~#ignore"})
public class AutomationSuiteParallelCucRunner {
#Test
public void testParallel() {
String karateOutputPath = "target/surefire-reports";
KarateStats stats = CucumberRunner.parallel(getClass(), 3, karateOutputPath);
assertTrue("SCENARIO FAILURES!!", stats.getFailCount() == 0);
}
}
Having struggled with this myself, the solution was surprisingly simple, though not really covered in the karate docs (as of this time).
The point is: when using the parallel runner in this manner, there is from JUnit's perspective just one unit test - the single method annotated with #Test. So this single test is all that's listed in target/surefire-reports. Karate doesn't seem to hook into the JUnit execution or so, so that's it.
But - and that was the missing piece for me - the karate runner itself can generate JUnit XML files. You just have to call .outputJunitXml(true) when creating it. This causes it to generate JUnit XML files in the karate report dir along with the HTML report and (optionally) cucumber JSON files:
import com.intuit.karate.Results;
import com.intuit.karate.Runner;
public class KarateTestRunner {
#Test
public void runParallel() {
final Results results = Runner.path("classpath:api/karate")
.outputCucumberJson(true)
.outputJunitXml(true) # <----
.parallel(8);
assertEquals(0, results.getFailCount());
}
}

how to configure doctrine command line tools on zenframework 2

i am using doctrine 2 on zendframework 2. i have configured both correcly and they are both working.
i however wish to use doctrine's command line tool to generate entities etc.
i have followed doctrine's instructions and created a cli-config.php page in the root of my application:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/configuration.html
i am however lost on two issues;
the configuration requires a bootstrap php page; however, zendframework 2 does not use a bootstrap page; so what would the equivalent be?
Secondly, it requires us to obtain an entity mangager; would the method below be the correct way to get the entity manager:
public function getEntityManager()
{
if (null === $this->em) {
$this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
}
return $this->em;
}
below is how the cli-config.php page should look;
// cli-config.php
require_once 'my_bootstrap.php';
// Any way to access the EntityManager from your application
$em = GetMyEntityManager();
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
i would really appreciate any help or advice on this matter.
warm regards
Andreea
the matter has been resolved:!!
it did not work because i was using a cygdrive command line. however, when i switched to git bash it worked perfectly. with git bash i have to use the command:
C: > cd project-directory
project-dir > vendor\bin\doctrine-module orm:validate-schema
If you have started your project using the Zend Skeleton Application you do have a composer.json file. You just need to include the DoctrineORMModule (instructions here)
Then, using the CMD just type
C: > cd project-directory
project-dir > vendor\bin\doctrine-module orm:validate-schema
There you go.
Once you have set up doctrine2 and zf2, you should be able to simply run all CLI commands.
php public/index.php orm:generate-entities
Using the parameters as described in the official documentation.
Note: DoctrineModule and DoctrineORMModule need to be enabled within your application.config.php
You need to install the doctrine/doctrine-orm-module with your Composer dependency manager. To do that, cd to your web site's top-level directory and type the following command:
php composer.phar require doctrine/doctrine-orm-module *
After executing this command, the DoctrineModule and DoctrineOrmModule will be installed into your vendor folder, and Doctrine commands will become available.
For more information about DoctrineOrmModule, see this:
https://github.com/doctrine/DoctrineORMModule

Do I need to enable javascript for PhantomJS when using Selenium Webdriver?

I have a testsuite in NUnit running on both IE and Chrome webdrivers. But when I wanted to try headerless PhantomJS (Ghostdriver) I can't get it to excute the actions I want to perform.
Having issues with easy stuff like
[TestFixture]
class PhantomJSTest{
protected IWebDriver driver;
[SetUp]
public void Setup() {
driver = new PhantomJSDriver(#"..\..\..\..");
}
[Test]
public void PhantomTest() {
driver.Navigate().GoToUrl(adress);
driver.FindElement(selector).Click();
}
[TearDown]
public void Teardown() {
driver.Close();
driver.Quit();
}
}
When the click is performed something should be set in my db, so when going back to that page manually I should be able to see it. The NUnit test itself is set to succeeded, but the action never happens. This is especially apparent when trying to do something based on the earlier action. Any help would be appreciated! =)
I would recommend you following activities:
1) try to relaunch your selenium hub with node(-s) based on phantomJs.
2) try to use instead of
driver.Navigate().GoToUrl(adress);
this one:
driver.get(URL);
driver.findElement(selector).click();
3) also see phantomJs documentation to get all phantomJs capabilities:
GhostDriver extra Capabilities
phantomjs.page.settings.SETTING = VALUE - Configure page.settings on
PhantomJS internal page objects (windows in WebDriver context) . Reference
phantomjs.page.customHeaders.HEADER = VALUE - Add extra HTTP Headers
when loading a URL . Reference
PhantomJSDriver (Java-binding) Capabilities
phantomjs.binary.path - Specify path to PhantomJS executable to use
phantomjs.ghostdriver.path - Specify path to GhostDriver main/src.js
script to use; allows to use a different version of GhostDriver then
the one embed in PhantomJS
phantomjs.cli.args - Specify command line arguments to pass to the
PhantomJS executable
phantomjs.ghostdriver.cli.args - Specify command line argument to
pass to GhostDriver (works only in tandem with
phantomjs.ghostdriver.path)
More details one can get at GhostDriver page
Also look through phantomJs command line options . This info might be helpful for you as well.
Hope this helps you.

PHPUnit test failing on plugin

my phpunit test fails with this error:
C:\workspace\internal_jets3\tests>phpunit --verbose
PHPUnit 3.5.7 by Sebastian Bergmann.
Fatal error: Call to a member function getOptions() on a non-object in C:\worksp
ace\internal_jets3\library\My\Controller\Plugin\ModuleLayoutLoader.php on line 7
Extract from plugin:
class My_Controller_Plugin_ModuleLayoutLoader extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$config = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions();
Any help on how to include plugins in testing will be helpful. Thanks!
Edit #Gordon
It's not really that obvious to me.
Doing a var_dump of:
var_dump(Zend_Controller_Front::getInstance()->getParam('bootstrap'));
die();
gives me:
object(Bootstrap)[3]
protected '_appNamespace' => boolean false
protected '_resourceLoader' => null
protected '_application' =>
which says 'object' to me; and halfway down the dump is:
protected '_options' =>
So this confuses me - I'm not familiar how to correctly phpunit test this object.
to set up PHPUnit in Zend environment, you have to fire your Zend bootstrap inside your phpunit bootstrap. After this, you still have a bootstrap object in your application.
And your errormessage said that Zend_Controller_Front::getInstance()->getParam('bootstrap')
is NULL. Are you sure, that you've debugged at the right place?