Magento 2.1.18 is the final 2.1.x release. After June 2019, Magento 2.1.x will no longer receive security patches, quality fixes, or documentation updates.
To maintain your site's performance, security, and PCI compliance, upgrade to the latest version of Magento.

Isolation management

Isolation management feature enables you to isolate a test suite, a test case, or a test using custom logic coded in a script. You can use it to return your system to its initial state (drop a database, clean cache, reset filesystem) or perform any other procedures that you need.

In general, to manage isolation:

Isolation strategy parameter defines when the isolation script must be run relatively to a test suite, a test case, or a test: before, after, both (that is run both before and after), none (never run).

The following example demonstrates how you can use isolation management.

Step 1: Create an isolation script

Assume that we want to return a database, dumped to /var/www/magento/magento.dump.sql, to its initial state. You can implement it using the following code:

1
2
3
<?php
exec('mysql -umagento -pmagento -e"DROP DATABASE magento; CREATE DATABASE magento CHARACTER SET utf8;"');
exec('mysql -umagento -pmagento magento < /var/www/magento/magento.dump.sql');

By default, isolation configuration points to dev/tests/functional/isolation.php.

Add the code to <magento root dir>/dev/tests/functional/isolation.php.

It means that during test run the FTF would call http://magento2ce.com/dev/tests/functional/isolation.php (<baseUrl> is set to http://magento2ce.com/) according to selected isolation strategy.

Isolation script is run in a web browser and must be accessible by a web server.

Step 2: Set isolation script

You can set isolation script globally, in configuration file, or locally, directly in a test case. The following examples show different options.

Isolation management for a certain test or test case has higher priority than global.

Step 2(a): Globally set isolation script to be run after each test case

  • Open <magento root dir>/dev/tests/functional/config.xml.
  • In <isolation>, set <testCase>after</testCase>, for example:
1
2
3
4
5
6
<isolation>
    <resetUrlPath>dev/tests/functional/isolation.php</resetUrlPath>
    <testSuite>none</testSuite>
    <testCase>after</testCase>
    <test>none</test>
</isolation>

Step 2(b): Globally set isolation script to be run before each test

  • Open <magento root dir>/dev/tests/functional/config.xml.
  • In <isolation>, set <test>before</test>, for example:
1
2
3
4
5
6
<isolation>
    <resetUrlPath>dev/tests/functional/isolation.php</resetUrlPath>
    <testSuite>none</testSuite>
    <testCase>none</testCase>
    <test>before</test>
</isolation>

Step 2(c): Globally set isolation script to be run before and after a test suite

  • Open <magento root dir>/dev/tests/functional/config.xml.
  • In <isolation>, set <testSuite>both</testSuite>, for example:
1
2
3
4
5
6
<isolation>
    <resetUrlPath>dev/tests/functional/isolation.php</resetUrlPath>
    <testSuite>both</testSuite>
    <testCase>none</testCase>
    <test>none</test>
</isolation>

Step 2(d): Locally set isolation script to be run after a test case

Example test case: \Magento\Checkout\Test\TestCase\OnePageCheckoutTest.

  • Open <magento root dir>/dev/tests/functional/tests/app/Magento/Checkout/Test/TestCase/OnePageCheckoutTest.php.
  • Add * @isolation after to the class annotation, for example:
1
2
3
4
5
6
/**
 * ...
 * @isolation after
 */
class OnePageCheckoutTest extends Scenario
...

Step 2(e): Locally set isolation script to be run after each test of a test case

Example test case: \Magento\Checkout\Test\TestCase\OnePageCheckoutTest.

  • Open <magento root dir>/dev/tests/functional/tests/app/Magento/Checkout/Test/TestCase/OnePageCheckoutTest.php.
  • Add * @isolation test after to the class annotation, for example:
1
2
3
4
5
6
/**
 * ...
 * @isolation test after
 */
class OnePageCheckoutTest extends Scenario
...

Step 2(f): Locally set isolation script to be run before test of a test case

Example test case: \Magento\Checkout\Test\TestCase\OnePageCheckoutTest. Example test: test().

  • Open <magento root dir>/dev/tests/functional/tests/app/Magento/Checkout/Test/TestCase/OnePageCheckoutTest.php
  • Add * @isolation before to the test() method annotation, for example:
1
2
3
4
5
6
7
    /**
     * ...
     * @isolation before
     */
    public function test()
    ...

Step 2(g): Locally set isolation script to be run before a test case and after a test

Example test case: \Magento\Checkout\Test\TestCase\OnePageCheckoutTest. Example test: test().

  • Open <magento root dir>/dev/tests/functional/tests/app/Magento/Checkout/Test/TestCase/OnePageCheckoutTest.php.
  • Add * @isolation before to the class annotation and * @isolation after to the test() method annotation, for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * ...
 * @isolation before
 */
class OnePageCheckoutTest extends Scenario
    {
    /**
     * ...
     * @isolation after
     */
    public function test()
    ...
    }

Step 2(h): Locally set isolation script to be excluded for a test case

Example test case: \Magento\Checkout\Test\TestCase\OnePageCheckoutTest.

  • Open <magento root dir>/dev/tests/functional/tests/app/Magento/Checkout/Test/TestCase/OnePageCheckoutTest.php.
  • Add * @isolation none to the class annotation, for example:
1
2
3
4
5
6
/**
 * ...
 * @isolation none
 */
class OnePageCheckoutTest extends Scenario
...

Step 2(i): Locally set isolation script to be excluded for a test

Example test case: \Magento\Checkout\Test\TestCase\OnePageCheckoutTest. Example test: test().

  • Open <magento root dir>/dev/tests/functional/tests/app/Magento/Checkout/Test/TestCase/OnePageCheckoutTest.php.
  • Add * @isolation none to the test() method annotation, for example:
1
2
3
4
5
6
7
    /**
     * ...
     * @isolation none
     */
    public function test()
    ...
Updated