Magento 2 모듈에 대한 통합 테스트 생성


27

지금까지 Magento 2 테스트 요구 사항에 대해 PHP Unit을 수용 테스터로 사용했습니다. 모듈이 설치된 시스템에서 서버 및 HTML 요청의 결과를 테스트했습니다. 내 자체 통합 테스트를 만들고 싶습니다. Magento 2와 함께 제공되는 테스트 도구를 사용하면 타사 개발자가 Magento의 테스트 프레임 워크 코드를 활용하는 자체 통합 테스트를 만들 수 있습니까? 아니면 우리 모두 자신 만의 부트 스트랩을 굴릴까요?

그건

  1. 저는 마 젠토 개발자입니다
  2. 통합 테스트를 만들고 싶습니다
  3. 통합 테스트를 위해 Magento 환경을 완전히 갖추기 위해 통합 테스트를 수행했습니다 (예 : 개체 관리자 및 / 또는 종속성 주입을 사용).
  4. 통합 테스트에서 테스트를 확장하여 Magento\TestFramework\TestCase\AbstractControllerMagento 테스트와 동일한 도우미가 필요합니다.
  5. 테스트 스위트의 나머지 부분과 별도로 테스트를 실행할 수 있기를 원합니다 (즉, 15 초 동안 테스트를 실행하기 위해 2 시간을 기다리지 않아도 됨).
  6. Magento의 테스트와 별도로 테스트를 저장하고 싶습니다.

dev 문서 사이트에는 테스트에 대한 초보자 기사가 있지만 Magento와 함께 제공되는 테스트를 실행하고 자체 테스트를 작성하고 실행하지 않는 데 중점을 둡니다. 거기에 기존의 샘플 모듈 ,하지만 그들은 모두 확장 PHPUnit_Framework_TestCase클래스를하고 것 같다 단위 테스트 합니다 (마 젠토 프레임 워크에 의존하지 않는 즉, 테스트 코드)

Magento가 제공하는 방법이 있습니까?

그렇지 않은 경우 Magento 개발자 커뮤니티의 테스트에서 표준으로 채택 할 수있는 방식으로 자체 설정을 실행 한 사람이 있습니까?

답변:


20

이것은 우리에게 효과적이지만 아직 6을 해결하기 위해 별도의 위치로 옮기지 않았습니다.)

1.) 통합 테스트를 dev/tests/integration/testsuite/Vendor
2)에 배치하십시오 dev/tests/integration/phpunit.dist.xml
.
dev/tests/integration/phpunit.xml

교체

        <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>

와

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

3.) ../../../vendor/bin/phpunit또는 ../../../vendor/bin/phpunit path/to/testsdev / test / integration 폴더에서 실행하십시오.

통합 테스트는 기본적으로 Magento를 설치하기 때문에 적어도 처음 실행시 15 초 이상 걸립니다. 당신이 사용하는 경우 후속 실행에 저장할 수 있습니다

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

당신의 phpunit.xml


11

통합 테스트를 별도의 디렉토리에 성공적으로 배치했습니다 src/My/Module/test/integration. 같은 다른 디렉토리 일 수도 있습니다 app/code/My/Module/Test.

Magento 통합 테스트에 새 테스트 스위트로 추가하십시오. 노드에 다음을 복사 dev/tests/integration/phpunit.xml.dist하여 dev/tests/integration/phpunit.xml추가하십시오 <testsuites>.

<testsuite name="My_Module">
    <directory suffix="Test.php">../../../src/My/Module/test</directory>
</testsuite>

그런 다음 dev/tests/integration디렉토리 에서 다음과 같은 테스트를 실행하십시오 .

../../../vendor/bin/phpunit --testsuite "My_Module"

--testsuite매개 변수를 사용하면 이름별로 하나의 테스트 스위트를 선택할 수 있으므로 모든 통합 테스트가 한 번에 실행되는 것은 아닙니다.

업데이트 : 비품

Magento\TestFramework\Annotation픽스쳐베이스 디렉토리가 전역 적으로 정의 되어 있기 때문에, 픽스쳐를 사용하려면 약간의 해결 방법이 필요 합니다. 그러나 다행스럽게도 Magento는 메서드 이름을 조명기로도 사용할 수 있으므로 다음과 같이 작동합니다.

/**
 * @magentoDataFixture loadFixture
 */
public function testSomething()
{
}

public static function loadFixture()
{
    include __DIR__ . '_files/something_fixture.php';
}

1
@magentoDataFixture를 사용할 때 여기 github.com/magento/magento2/blob/develop/dev/tests/integration/… 에 문제가 발생하지 않습니까? 특히 모듈의 사용자 지정 픽스처를 핵심 모듈과 결합 할 때?
— Kristof at Fooman

1
tbh 아직 시도하지 않았지만 문제처럼 보입니다. 이러한 조명기가 작동하도록 포함 경로를 설정해야 할 수도 있습니다.
— Fabian Schmengler

1
@KristofatFooman 비품에 대한 솔루션을 찾았습니다. 업데이트 참조
— Fabian Schmengler

훌륭한 솔루션. 여기에 몇 가지 결함이있을 수 있습니다. 우선, 오타 __DIR__가 있습니다 - 슬래시 ( /_files) 가 와야합니다 . 둘째, 픽스쳐는 TestFramework 내에서로드되므로 __DIR__실제로는 사용자 모듈이 아닌 TestFramework 디렉토리를 가리 킵니다. 이것을 ComponentRegistrar위해 사용될 수 있습니다 :require $ObjectManager::getInstance()->get(ComponentRegistrar::class)->getPath('module', 'Foo_Bar').'/Test/Integration/_files/example.php';
— Jisse Reitsma

10

나는 통합 테스트로 약간의 연주를 해왔으며, 이것이 지금까지 찾은 것입니다.

기본적으로 통합 테스트를 내 모듈의 일부로 만들기 위해 Fooman과 비슷한 단계를 밟았습니다.

다음은 내가 따르는 단계입니다.

1- 통합 테스트를 app/code/Vendor/CustomModule/Test/Integration

2 복사 dev/tests/integration/phpunit.dist.xml에dev/tests/integration/phpunit.xml

교체

<testsuite name="Magento Integration Tests">
    <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>
</testsuite>

와

<testsuite name="Magento Integration Tests">
    <directory suffix="Test.php">../../../app/code/Vendor/CustomModule/Test/Integration</directory>
</testsuite>

3- 그런 다음 CLI 도구를 사용하여 실행합니다. bin/magento dev:test:run integration

"TESTS_CLEANUP"에 대해 Fooman이 말한 내용과 정리 사용을 설정 한 경우 통합 테스트를 설정하는 데 걸리는 시간을 염두에 두어야합니다.

추가 참조를 위해 기능적 예제를 추가합니다. Magento 픽스처를 사용하는 것뿐만 아니라 객체 관리자에 액세스하고 Magento 클래스의 인스턴스를 생성하는 방법을 알 수 있습니다.

app / code / Vendor / CustomModule / Controller / Order / Info.php

namespace Vendor\CustomModule\Controller\Order;

use Magento\Framework\Controller\ResultFactory;

class Info
    extends \Magento\Framework\App\Action\Action
{
    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
    )
    {
        $this->orderRepository = $orderRepository;
        parent::__construct($context);
    }

    /**
     * Return Json OrderInfo
     *
     * @return \Magento\Framework\Controller\Result\Json $this
     */
    public function execute()
    {
        $orderId = $this->getRequest()->getParam('id');
        $order = $this->orderRepository->get($orderId);
        $orderInfo = [
            'total' => $order->getBaseGrandTotal()
        ];

        /** @var \Magento\Framework\Controller\Result\Json $result */
        $result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
        return $result->setData($orderInfo);
    }

}

app / code / Vendor / CustomModule / etc / frontend / routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="vendor_custommodule" frontName="vendor_custommodule">
            <module name="Vendor_CustomModule"/>
        </route>
    </router>
</config>

app / code / Vendor / CustomModule / etc / module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_CustomModule" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Sales" />
        </sequence>
    </module>
</config>

app / code / Vendor / CustomModule / Test / Integration / Controller / Order / InfoTest.php

namespace Vendor\CustomModule\Controller\Order;

use Magento\TestFramework\TestCase\AbstractController;

class InfoTest extends AbstractController
{
    public function getOrderInfoActionDataProvider()
    {
        return [
            'order with one simple item' => [
                'incrementId' => '100000001',
                'contentType' => 'application/json',
                'orderTotal' => 100
            ]
        ];
    }

    /**
     * @dataProvider getOrderInfoActionDataProvider
     * @magentoDataFixture Magento/Sales/_files/order.php
     */
    public function testOrderInfoAction($incrementId, $expectedContentType, $expectedOrderTotal)
    {
        /** @var $objectManager \Magento\TestFramework\ObjectManager */
        $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

        /** @var \Magento\Sales\Model\OrderFactory $orderFactory */
        $orderFactory = $objectManager->get('Magento\Sales\Model\OrderFactory');
        $order = $orderFactory->create();
        $order->loadByIncrementId($incrementId);

        $this->dispatch("vendor_custommodule/order/info/id/{$order->getId()}");

        $contentType = $this->getResponse()->getHeader('Content-Type');
        $this->assertEquals($expectedContentType, $contentType->getFieldValue());

        $responseJson = $this->getResponse()->getBody();
        $responseArray = json_decode($responseJson, true);
        $this->assertEquals($expectedOrderTotal, $responseArray['total']);
    }
}

app / code / Vendor / CustomModule / registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_CustomModule',
    __DIR__
);

Magento 코어의 고정물을 사용하는 한 자신의 솔루션이 훌륭하고 작동한다는 점에 유의하십시오. 자신의 비품을 사용하려면 앞에서 설명한 문제에 부딪치게됩니다.
— Jisse Reitsma
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.