Magento 2 introduced a set of annotations to help with the configuration of integration tests. These annotations are used to define the configuration of a test class or method, specifying aspects such as database transactions, the area code, and other settings. Here’s an example of how you can use these annotations in a Magento 2 integration test:
namespace Vendor\Module\Test\Integration;
use Magento\TestFramework\TestCase\AbstractController;
/**
* @magentoAppArea frontend
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
* @magentoDbIsolationPerTest enabled
* @magentoAppIsolationPerTest enabled
*/
class YourIntegrationTest extends AbstractController
{
/**
* Your test logic here
*/
public function testSomething()
{
// Your test logic
}
}
Here’s a brief explanation of the annotations used in the example:
-
@magentoAppArea: Specifies the application area for the test. It can be
frontend
oradminhtml
. This is important because certain Magento features are only available in specific areas. -
@magentoDbIsolation: Enables or disables database isolation for the entire test class. When enabled, it ensures that any changes made to the database during the test are rolled back at the end of the test.
-
@magentoAppIsolation: Enables or disables application isolation for the entire test class. Application isolation ensures that the state of the application is restored to its initial state after the test is executed.
-
@magentoDbIsolationPerTest: Similar to
@magentoDbIsolation
, but it applies on a per-test basis. It enables or disables database isolation for each individual test method. -
@magentoAppIsolationPerTest: Similar to
@magentoAppIsolation
, but it applies on a per-test basis. It enables or disables application isolation for each individual test method.
You can use these annotations based on your testing requirements. For example, if you want to test a specific module’s functionality in the frontend area with database isolation for each test, you might have annotations like this:
/**
* @magentoAppArea frontend
* @magentoDbIsolationPerTest enabled
* @magentoAppIsolationPerTest enabled
*/
class YourModuleFrontendTest extends AbstractController
{
// Test methods
}
Remember that proper annotations help in setting up the test environment correctly and make your tests more predictable and maintainable. Adjust these annotations according to the needs of your specific tests.
Comments