Magento 2 – How to Create Integration Tests for Modules

integration-testmagento2PHPphpunit

So far for my Magento 2 testing needs, I've been using PHP Unit as (more or less) an acceptance tester — testing results of server and HTML requests made into a system with my module(s) installed. I'd like to be able to create my own integration tests. Do the testing tools that ship with Magento 2 allow third party developers to create their own integration tests that leverage Magento's test framework code? Or will we all be rolling our own bootstrap?

That is

  1. I am a Magento developer
  2. I'd like to create an integration test
  3. I'd my integration test to have a fully bootstrap Magento environment to play in (i.e object manager and/or dependency injection to use)
  4. I'd like my integration test to extend the Magento\TestFramework\TestCase\AbstractController test so I have the same helpers as Magento tests
  5. I'd like to be able to run my tests in isolation from the rest of the test suite (i.e. not have to wait 2 hours to run my 15 seconds of tests)
  6. I'd like my tests stored separately from Magento's tests

The dev docs site has some starter articles on testing, but they seem oriented at running the tests that ship with Magento and not creating and running your own tests. There's the old sample modules, but they all extend the PHPUnit_Framework_TestCase class and seem to be unit tests (i.e. testing code that doesn't rely on the Magento framework)

Is there a Magento provided way of doing this?

If not, has anyone rolled their own setup in such a way that the test of the Magento developer community could adopt it as a standard?

Best Answer

This works for us but we haven't yet looked into moving them to a separate location to address 6.)

1.) Place your integration tests under dev/tests/integration/testsuite/Vendor
2.) copy dev/tests/integration/phpunit.dist.xml
to
dev/tests/integration/phpunit.xml

and replace

        <directory suffix="Test.php">testsuite</directory>
        <directory suffix="Test.php">../../../update/dev/tests/integration/testsuite</directory>
        <exclude>testsuite/Magento/Test/Integrity</exclude>
        <exclude>testsuite/Magento/MemoryUsageTest.php</exclude>

with

        <directory suffix="Test.php">testsuite/Vendor</directory>

3.) run it ../../../vendor/bin/phpunit or with ../../../vendor/bin/phpunit path/to/tests from the dev/test/integration folder

Please note that the integration tests take longer than 15 seconds, at least on first run as it essentially installs Magento. You can save on subsequent runs if you use

<const name="TESTS_CLEANUP" value="disabled"/>

in your phpunit.xml

Related Topic