Asked  1 Year ago    Answers:  5   Viewed   12 times

Using phpunit and I am having some trouble with include paths, not for phpunit itself, but for my code and tests directory.

I have the following code structure:

Application
  -StringCalculator.php

tests
  -StringCalculatorTest.php

Inside my StringCalculatorTest.php i have a require statement:

require_once('../StringCalculator.php');

Running phpunit StringCalculatorTest.php from inside the tests folder works perfectly.

However, when i then introduce a phpunit.xml configuration file in the root directory i.e.

Application
  -StringCalculator.php

tests
  -StringCalculatorTest.php

phpunit.xml

the include path is screwed. I have to replace the require_once to

require_once('StringCalculator.php');

What is the correct way to set include paths between the application and the test directory?

 Answers

1

The best place to set your PHP include path is in your bootstrap file. Usually, your phpunit.xml file will include a bootstrap attribute:

<phpunit backupGlobals="true"
     backupStaticAttributes="false"
     bootstrap="bootstrap.php"
     cacheTokens="true"
     colors="true"
     ... and so on ...
</phpunit>

Then in your bootstrap file you can set include paths, include important files, etc..

set_include_path(get_include_path() . PATH_SEPARATOR . '../my/sources');

The config file is covered in Appendix C of the PHPunit docs.

EDIT: Link updated

Saturday, May 29, 2021
 
hakre
 
2

Your setUp() method gets called before every test, so testHandleSynchronousMessageForFailure() is also expecting sendNonBlockingAsynchronous() to be called:

$this->context->sendNonBlockingAsynchronous('platform_session_initiated', Argument::type("array"))
        ->shouldBeCalled();

Even if you call shouldNotBeCalled() on it in the failure test. So, move the shouldBeCalled() call to the testHandleSynchronousMessageForSuccess(), that way it will expect it to be called in the success test, and not to be called in the failure test.

You should also tell PHPUnit to expect an IdNotDefinedException in the failure test:

$this->expectException(GpxExceptionsIdNotDefinedException::class);
Thursday, April 1, 2021
 
4

This is one of those places where you are 'officially' allowed to use the @ operator :)

Make one test to check the return value, another test to check if the warning gets triggered. And by the way, I'd suggest you do test if the warning is triggered.

class SimpleTest extends PHPUnit_Framework_TestCase
{
   function testSimpleMethodReturnValue()
   {
     $toBeTestedObject = new ToBeTested();
     $this->assertFalse(@$toBeTestedObject->simpleMethod(0));
   }

   /**
    * @expectedException PHPUnit_Framework_Error
    */
   function testSimpleMethodEmitsWarning() {
     $toBeTestedObject = new ToBeTested();
     $toBeTestedObject->simpleMethod(0);
   }
}
Saturday, May 29, 2021
 
4

There are a number of ways to not run a particular test - putting it into a blacklist so it's never run may not be the way - as changing it means editing the blacklist, and you'll often endup bouncing it in and out of version control.

There are several other ways that may be more appropriate:

If a test is not yet ready to run:

$this->markTestIncomplete('This test has not been implemented yet.');

If there's an outside reason it should not be run, skip it:

if (!extension_loaded('mysqli')) {
    $this->markTestSkipped('The MySQLi extension is not available.');
}

You can also put that into the setUp() function, so it will skip all the tests in a test-class.

You can make a test dependant on a previous one succeeding:

public function testEmpty()
{
    $stack = array();
    $this->assertTrue(empty($stack));
    return $stack;   // also sends this variable to any following tests - if this worked
}
/**
 * only runs if testEmpty() passed
 *
 * @depends testEmpty
 */
public function testPush(array $stack)
{
}

The @group -name- annotation is one of the best ways to specifically stop, or run one group of tests

/**
 * @group database
 * @group remoteTasks
 */
public function testSomething()
{
}

testSomething() is now in two groups, and if either is added on the command line (or in the config.xml) --exclude-group parameter. it won't be run. Likewise, you could run only tests that belong to a particular group - say named after a feature, or bug report.

Wednesday, September 8, 2021
 
5

I was searching for the same thing and came across this page:

http://code.google.com/p/imagetestsuite/

It contains test suites for PNG/GIF/JPEG/TIFF created so that they try to achieve maximum amount of code coverage with the least amount of images based on the open source decoder code-bases. The idea seems good, but I have not yet had time to check out the actual images.

Sunday, September 12, 2021
 
Sperick
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :