Write Unit test for InputException Error Test in Magento 2.

You can write a  Unit test to check the InputException error text message matched or not using assertEquals() or assertStringMatchesFormat() method for an input exception error.

Let’s take a simple example with a function,

<?php
public function getMethodHtml($name)
    {
        $block = $this->getLayout()->getBlock($name);
        if (!$block) {
            throw new \Magento\Framework\Exception\InputException(__('Invalid block name: %1', $name));
        }
        return true;
    }

Now we have to verify Input exception string with PHP unit test,
We have to create a Test file inside the Test\Unit folder of the module.

For InputException, Value will be dynamically added using  ‘%1’ format.

Under the ClassnameTest.php file, add below function to verify Test case,

public function testInputExceptionMessage()
{
   $params = ['name' => 'ExtraBlock'];
    $inputException = new InputException(
        new Phrase('Invalid block name: %1', $params)
    );

    $this->assertEquals(
        'Invalid block name: %1',
        $inputException->getRawMessage()
    );
    $this->assertStringMatchesFormat(
        'Invalid block name: %1',
        $inputException->getMessage()
    );
    $this->assertEquals(
        'Invalid block name: %1',
        $inputException->getLogMessage()
    );
}

Based on the above test case you need to run the Unit test and Your unit test will be successful.

We have test the InputException with assertEquals() and  assertStringMatchesFormat() assertion.

For Unit test basic check link, What is a Unit test in Magento 2?