Q:

Error trying to run PHPUnit Magento 2 test

I created a simple hello world Magento 2 test, when I try to run the test I get the following error:

Fatal error: Declaration of
  Magento\Framework\TestFramework\Unit\Listener\ReplaceObjectManager::startTest(PHPUnit\Framework\Test
  $test) must be compatible with
  PHPUnit_Framework_TestListener::startTest(PHPUnit_Framework_Test
  $test) in
  /Applications/MAMP/htdocs/mag221/vendor/magento/framework/TestFramework/Unit/Listener/ReplaceObjectManager.php
  on line 14

This is the phpunit.xml.dist
<? xml version="1.0" encoding="UTF-8"? >
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.2/phpunit.xsd" colors="true" beStrictAboutTestsThatDoNotTestAnything="false" bootstrap="./framework/bootstrap.php">
   <testsuite name="Magento Unit Tests">
      <directory suffix="Test.php">../../../TestStore/Checkout/HelloMessage/Test/Unit</directory>
   </testsuite>
   <php>
      <ini name="date.timezone" value="America/Los_Angeles" />
      <ini name="xdebug.max_nesting_level" value="200" />
      <ini name="memory_limit" value="-1" />
   </php>
   <filter>
      <whitelist addUncoveredFilesFromWhiteList="true">
         <directory suffix=".php">../../../app/code/*</directory>
         <directory suffix=".php">../../../lib/internal/Magento</directory>
         <directory suffix=".php">../../../setup/src/*</directory>
         <exclude>
            <directory>../../../app/code/*/*/Test</directory>
            <directory>../../../lib/internal/*/*/Test</directory>
            <directory>../../../lib/internal/*/*/*/Test</directory>
            <directory>../../../setup/src/*/*/Test</directory>
         </exclude>
      </whitelist>
   </filter>
   <listeners>
      <listener class="Magento\Framework\TestFramework\Unit\Listener\ReplaceObjectManager" />
   </listeners>
   <logging />
</phpunit>

HelloMessageTest.php
<? php
namespace TestStore\HelloMessage;
use TestStore\HelloMessage;

class HelloMessageTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var HelloMessage
     */
    protected $helloMessage;

    public function setUp()
    {
        $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
        $this->helloMessage = $objectManager->getObject('Magestore\HelloMagento\Model\HelloMessage');
        $this->expectedMessage = 'Hello Magento 2!  We will change the world!'; 
    }

    public function testGetMessage()
    {
         $this->assertEquals($this->expectedMessage, $this->helloMessage->getMessage());
    }
}

I'm using PHP 7.1.12, Magento 2.2.1. 
Any idea what might be causing this? 

A:

First of all create HelloMessage class \app\code\TestStore\Hello\Model\HelloMessage.php
<? php
namespace TestStore\Hello\Model;

class HelloMessage
{
    public function getMessage()
    {
        return 'Hello Magento 2!  We will change the world!'; 
    }
}

Create UnitTest Model \app\code\TestStore\Hello\Test\Unit\Model\HelloMessageTest.php
<? php
namespace TestStore\Hello\Test\Unit\Model;

use TestStore\Hello\Model\HelloMessage;

class HelloMessageTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var HelloMessage
     */
    protected $helloMessage;

    public function setUp()
    {
        $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
        $this->helloMessage = $objectManager->getObject('TestStore\Hello\Model\HelloMessage');
        $this->expectedMessage = 'Hello Magento 2!  We will change the world!'; 
    }

    public function testGetMessage()
    {
         $this->assertEquals($this->expectedMessage, $this->helloMessage->getMessage());
    }
}

Now, you have to add UnitTest in phpunit.xml by
<testsuite name="Magento Unit Tests">
    <directory suffix="Test.php">../../../app/code/TestStore/Hello/Test/Unit</directory>
</testsuite>

then simply run the Unit test from the console under magento folder
For reference you can also check How to write Unit Test in Magento 2

