Skip to content

Integration Test not working in Custom Module Magento 2

I am creating the Integration test in my custom module. I have installed the module in vendor folder structure is like as vendor/[ModuleVendor]/[ModuleName]/Src

I am using below command to run the integration test for custom module only

php vendor/phpunit/phpunit/phpunit -c dev/tests/integration/phpunit.xml vendor/[ModuleVendor]/[ModuleName]/src/Test/Integration/

I am getting below error while run this command

PHPUnit 9.5.28 by Sebastian Bergmann and contributors.
Could not use “MagentoTestFrameworkSuiteLoader” as loader.

I have changed the phpunit.xml file like as

<testsuites>
        <!--<testsuite name="Magento Integration Tests">
            <file>testsuite/Magento/IntegrationTest.php</file>
        </testsuite>-->
        <!-- Memory tests run first to prevent influence of other tests on accuracy of memory measurements -->
        <!--<testsuite name="Memory Usage Tests">
            <file>testsuite/Magento/MemoryUsageTest.php</file>
        </testsuite>
        <testsuite name="Magento Integration Tests Real Suite">
            <directory>testsuite</directory>
            <directory>../../../app/code/*/*/Test/Integration</directory>
            <exclude>testsuite/Magento/MemoryUsageTest.php</exclude>
            <exclude>testsuite/Magento/IntegrationTest.php</exclude>
        </testsuite>-->
        <testsuite name="my integration">
            <directory suffix="Test.php">../../../vendor/[ModuleVendor]/[ModuleName]/src/Test/Integration</directory>
            <exclude>../../../app/code/Magento</exclude>
            <exclude>../../../vendor/magento</exclude>
        </testsuite>
    </testsuites>

Added the product fixture in file vendor/[ModuleVendor]/[ModuleName]/src/Test/Integration/_files/product_simple.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);
use MagentoCatalogApiDataProductInterface;
use MagentoCatalogApiProductRepositoryInterface;
use MagentoCatalogModelIndexerProductPriceProcessor;
use MagentoCatalogModelProductAttributeSourceStatus;
use MagentoCatalogModelProductVisibility;
use MagentoTestFrameworkHelperBootstrap;

/** @var $product MagentoCatalogModelProduct */
$product = Bootstrap::getObjectManager()
    ->create(MagentoCatalogModelProduct::class);

/*
 * Creation of Quote on Test Websites
 */
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(ProductRepositoryInterface::class);

/** @var ProductInterface $product */
$product = $objectManager->create(ProductInterface::class);
$product->setTypeId('simple')
    ->setName('Simple Product Integration test')
    ->setSku('simpleintegration')
    ->setWebsiteIds([0,1,2,3])
    ->setPrice(60)
    ->setMetaTitle('meta title')
    ->setMetaKeyword('meta keyword')
    ->setMetaDescription('meta description')
    ->setVisibility(Visibility::VISIBILITY_BOTH)
    ->setStatus(Status::STATUS_ENABLED)
    ->setStockData(['use_config_manage_stock' => 0])
    ->setAttributeSetId(4)
    ->setIsSalable(true)
    ->setSalable(true);
$product = $productRepository->save($product);

I also created an controller to check the integration test.
vendor/[ModuleVendor]/[ModuleName]/src/Test/Integration/Controller/CustomTest.php

<?php
namespace [ModuleVendor][ModuleName]TestIntegrationController;
use MagentoCatalogApiDataProductInterface;
use MagentoCatalogApiProductRepositoryInterface;
use MagentoCatalogModelSession;
use MagentoFrameworkRegistry;
use MagentoTestFrameworkCatalogModelProductLayoutUpdateManager;
use MagentoTestFrameworkHelperBootstrap;
use MagentoTestFrameworkHelperXpath;
use MagentoTestFrameworkTestCaseAbstractController;
/**
 * Checks product view on storefront
 *
 * @see MagentoCatalogControllerProduct
 *
 * @magentoDbIsolation enabled
 */
class CustomTest extends MagentoTestFrameworkTestCaseAbstractController
{
    /** @var Registry */
    private $registry;

    /** @var ProductRepositoryInterface */
    private $productRepository;

    /** @var Session */
    private $session;

    /**
     * @magentoDataFixture [ModuleVendor]/ModuleName]/controllers/_files/product_simple.php
     * @magentoAppArea frontend
     * @return void
     */
    public function testViewAction()
    {
        $product = $this->productRepository->get('simpleintegration');
        $this->dispatch(sprintf('catalog/product/view/id/%s', $product->getEntityId()));
        $currentProduct = $this->registry->registry('current_product');
        $this->assertInstanceOf(ProductInterface::class, $currentProduct);
        $this->assertEquals($product->getEntityId(), $currentProduct->getEntityId());
        $this->assertEquals($product->getEntityId(), $this->session->getLastViewedProductId());

        $responseBody = $this->getResponse()->getBody();
        /* Product info */
        $this->assertStringContainsString($product->getName(), $responseBody);
        $this->assertStringContainsString($product->getDescription(), $responseBody);
        $this->assertStringContainsString($product->getShortDescription(), $responseBody);
        $this->assertStringContainsString($product->getSku(), $responseBody);

        /* Stock info */
        $this->assertStringContainsString('$1,234.56', $responseBody);
        $this->assertStringContainsString('In stock', $responseBody);
        $this->assertStringContainsString((string)__('Add to Cart'), $responseBody);
        /* Meta info */
        $this->assertStringContainsString('<title>Simple Product 1 Meta Title</title>', $responseBody);
        $this->assertEquals(
            1,
            Xpath::getElementsCountForXpath(
                '//meta[@name="keywords" and @content="Simple Product 1 Meta Keyword"]',
                $responseBody
            )
        );
        $this->assertEquals(
            1,
            Xpath::getElementsCountForXpath(
                '//meta[@name="description" and @content="Simple Product 1 Meta Description"]',
                $responseBody
            )
        );
    }
}