Unit test case for custom module REST API
-
10-10-2020 - |
Question
I wanted to write unit test case for custom REST API in magento2.
I have already gone through the : http://devdocs.magento.com/guides/v2.0/get-started/web-api-functional-testing.html
I am getting confused as the code example in the link above is written in Magento's namespace. Can anyone tell me how I can write unit test for testing the API's?
Solution
Write the tests in your own module namespace in a subdirectory like Test/Api
After you copied phpunit.xml.dist
to phpunit.xml
, as described in the docs, change the following:
<!-- Test suites definition -->
<testsuites>
<testsuite name="Magento Web API Functional Tests">
<directory suffix="Test.php">testsuite</directory>
</testsuite>
</testsuites>
to
<!-- Test suites definition -->
<testsuites>
<testsuite name="Your Project Web API Functional Tests">
<directory suffix="Test.php">../../../app/code/*/*/Test/Api</directory>
</testsuite>
</testsuites>
../../../app/code/*/*/Test/Api
is a relative path to your tests. Adjust it if your directory structure differs.
Now running phpunit
within dev/tests/api-functional
runs your own tests instead of the core test suite.