We will do simple module which will write Hello world to frontend. See https://michalmachovic.github.io/php,%20magento2/2019/02/07/magento2-basic-module.html. We added Test/Unit/SampleTest.php file, you can see here assertEquals testing of getHelloWorldTxt method of Helloworld class defined in Block/Helloworld.php.

Block
 |
 --Helloworld.php
Controller
 |
 Index
   |
   --Index.php
etc
 |
 module.xml
 frontend
    |
    --routes.xml
view
 |
 layout
  |
  --helloworld_index_index.xml
  |
  templates
  |
  --helloworld.phtml
Test
 |
 Unit
  |
  --SampleTest.php  
registration.php
//Test/Unit/SampleTest.php

<?php
 
namespace MichalMachovic\Helloworld\Test\Unit;
 
use MichalMachovic\Helloworld\Block\Helloworld;
 
class SampleTest extends \PHPUnit\Framework\TestCase
{
    
    protected $sampleClass;
    protected $expectedMessage;
 
    public function setUp()
    {
        $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
        $this->sampleClass = $objectManager->getObject('MichalMachovic\Helloworld\Block\Helloworld');
        $this->expectedMessage = 'Hello world!';
    }
 
    public function testGetMessage()
    {
        $this->assertEquals($this->expectedMessage, $this->sampleClass->getHelloWorldTxt());
    }
 
}



There is PHPUnit configuration file shipped with Magento 2 – {ROOT}/dev/tests/unit/phpunit.xml.dist. Create phpunit.xml file in the same location and your test location (you can comment out other tests defined there, if you dont want to test everything):

//dev/tests/unit/phpunit.xml
<testsuite name="Magento Unit Tests">
        <directory suffix="Test.php">../../../app/code/MichalMachovic/Helloworld/Test/Unit</directory>
</testsuite>



To run test, do

./vendor/bin/phpunit -c dev/tests/unit/phpunit.xml