noob phpUnit (using DevSense php Tools for Visual Studio)

Visual Studio Solution:

Installing phpUnit

  • put the dll in the php/ext directory (php_xdebug-3.0.3-8.0-vs16-x86_64.dll)
  • add a phpunit.bat or phpunitcmd file in the php directory:
@ECHO OFF php “%~dp0phpunit-9.5.2.phar” %*

Installing xDebug

Xdebug: Installation instructions result

Follow directions

Code

Snippet

<?php
// namespace LearnVStudioTests; seems like namespaces are directories ...
use PHPUnit\Framework\TestCase;

// include_once(LIB.'/tst1.php');    //  BAD

 include __DIR__ . '/../tst1.php'; // GOOD
// include '../tst1.php';  //  BAD
//use LIB;
//set_include_path('LIB');

//spl_autoload_register(function ($class_name) {
//    $filepath = '../'  . $class_name . '.php';
//    include  dirname($filepath) ;
//});


final class tst1Test extends TestCase
{

    /**
     * @group StringTests
     */
    public  function test_hello_Assert()
    {
        $result = '';

        try {
            $result = tst1::hello('todd');
        }
        catch (Exception $e) {
            echo $e->getMessage(), "\n";
        }

        $this->assertEquals('Hello todd' , $result);
    }


   /**
    * @test
    * @group MathTests
    */
   public  function one()
   {
       $result = '';

       try {
           $result = tst1::addOne(99);
       }
       catch (Exception $e) {
           echo $e->getMessage(), "\n";
       }

       $this->assertEquals(100 , $result);
   }


/*
     * https://phpunit.readthedocs.io/en/9.5/organizing-tests.html#composing-a-test-suite-using-the-filesystem
     * https://docs.devsense.com/en/vs/testing/writing-test
 *
 * */

}


?>

Test
<?php
use PHPUnit\Framework\TestCase;

set_include_path('.');

class tst1Test extends PHPUnit_Framework_TestCase
{

    function hello_Assert()
    {
        $result = tst1.hello('todd');
        $this->assertEquals('hello todd' , $result);
    }


}


?>

Config

Snippet

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd" backupGlobals="true" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" forceCoversAnnotation="false" processIsolation="false" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false" stopOnRisky="false" timeoutForSmallTests="1" timeoutForMediumTests="10" timeoutForLargeTests="60" verbose="false">
  <coverage includeUncoveredFiles="true" processUncoveredFiles="true" pathCoverage="true" ignoreDeprecatedCodeUnits="false" disableCodeCoverageIgnore="true">
  </coverage>
  <testsuites>
    <testsuite name="First">
      <directory>tests/</directory>
    </testsuite>
  </testsuites>
</phpunit>

<!-- https://github.com/moneyphp/money --> 

Notes:

  • a syntax error in the test script will prevent the test explorer from displaying your tests
  • see errors in the output windows , use the tab to select “output from tests”
  • open a command prompt in the project directory, run phpunit – this will help get your config debugged and whether or not your files can even be accessed (directory path in the config file)
  • once phpunit from command line works, then probably test explorer will display tests
  • I left the struggles I had in attempting to access LIB classes from the test directory. Apparently that’s a php thing.
  • Original question: getting started with phpUnit – DEVSENSE Community Forum

One Response to “noob phpUnit (using DevSense php Tools for Visual Studio)”

  1. Jakub Míšek Says:

    For details, please see https://docs.devsense.com/en/vs/testing/writing-test

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: