Data set

A data set contains data used by a test case and constraints. A data set can have several variations. Each variation has constraints that are called at the end of the test flow.

Example data set

Let’s see an example for CreateSimpleProductEntityTest. A data set and its corresponding test case must be placed in the <magento2_root_dir>/dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/Product directory.

The CreateSimpleProductEntityTest.xml data set contains:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../vendor/magento/mtf/etc/variations.xsd">
    <testCase name="Magento\Catalog\Test\TestCase\Product\CreateSimpleProductEntityTest" summary="Create Simple Product" ticketId="MAGETWO-23414">
        <variation name="CreateSimpleProductEntityTestVariation1" summary="Create product with custom options(fixed price)">
            <data name="product/data/url_key" xsi:type="string">simple-product-%isolation%</data>
            <data name="product/data/name" xsi:type="string">Simple Product %isolation%</data>
            <data name="product/data/sku" xsi:type="string">simple_sku_%isolation%</data>
            <data name="product/data/price/value" xsi:type="string">10000</data>
            <data name="product/data/price/dataset" xsi:type="string">drop_down_with_one_option_fixed_price</data>
            <data name="product/data/short_description" xsi:type="string">Simple Product short_description %isolation%</data>
            <data name="product/data/description" xsi:type="string">Simple Product description %isolation%</data>
            <data name="product/data/weight" xsi:type="string">50</data>
            <data name="product/data/quantity_and_stock_status/qty" xsi:type="string">657</data>
            <data name="product/data/custom_options/dataset" xsi:type="string">drop_down_with_one_option_fixed_price</data>
            <data name="product/data/checkout_data/dataset" xsi:type="string">simple_drop_down_with_one_option_fixed_price</data>
            <data name="tag" xsi:type="string">test_type:acceptance_test</data>
            <constraint name="Magento\Catalog\Test\Constraint\AssertProductSaveMessage" />
            <constraint name="Magento\Catalog\Test\Constraint\AssertProductInGrid" />
            <constraint name="Magento\Catalog\Test\Constraint\AssertProductInCategory" />
            <constraint name="Magento\Catalog\Test\Constraint\AssertProductPage" />
            <constraint name="Magento\Catalog\Test\Constraint\AssertProductInCart" />
        </variation>
    </testCase>
</config>

This is a data set that:

  • corresponds to the XSD schema <magento2_root_dir>/dev/tests/functional/vendor/magento/mtf/etc/variations.xsd
  • relates to the Magento\Catalog\Test\TestCase\Product\CreateSimpleProductEntityTest test case (performs creation of the simple product)
  • relates to the ticket MAGETWO-23414 in Jira
  • contains variation CreateSimpleProductEntityTestVariation1 that
    • contains data to create product with fixed price (see descriptions in the following table)
    • defines tag that can be used to customize the test suite run
    • defines constraints that will be performed after the test flow in the order they are presented in the data set

The CreateSimpleProductEntityTestVariation1 variation contains the following $product data:

Fixture fieldDescription
url_key field is assigned with simple-product-%isolation%. More info about %isolation%.
namefield is assigned with Simple Product %isolation%
skufield is assigned with simple_sku_%isolation%
price
  • product/data/price/value is processed by a data source Magento\Catalog\Test\Fixture\Product\Price and is assigned 10000
  • product/data/price/dataset is assigned with a data set drop_down_with_one_option_fixed_price from the Magento\Catalog\Test\Repository\CatalogProductSimple\Price repository. This data set is used by a constraint.
short_description field is assigned with Simple Product short_description %isolation%
description field is assigned with Simple Product description %isolation%
weight field is assigned with 50
quantity_and_stock_status/qty field is assigned with 657
custom_options field is processed by a data source Magento\Catalog\Test\Fixture\Product\CustomOptions using a data set drop_down_with_one_option_fixed_price from the repository Magento\Catalog\Test\Repository\Product\CustomOptions
checkout_data fields are assigned with a data set simple_drop_down_with_one_option_fixed_price from the Magento\Catalog\Test\Repository\CatalogProductSimple\CheckoutData repository

Data set structure

A data set is an XML file that contains test variations for a test case.

Each variation includes:

  • Data used during the test flow and assertions
  • Constraints that are called after the test flow

The following table shows structure of the data set:

Node Semantics Attributes
config The root element that defines an XML namespace and an XML Schema.
  • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  • xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/mtf/etc/variations.xsd"
testCase Contains a reference to the test case class in attribute and variations in child elements.
  • name - full name of a test case class. Required.
  • summary - description of a test case. Required.
  • ticketId - identifier of related ticket. Optional.
variation Contains variation description in attributes and data with constraints in child elements.
  • name - a variation name. Required.
  • replace - a name of variation to be used instead of a variation indicated in name. Optional.
  • firstConstraint - a full name of constraint that is performed first. Optional.
  • method - a name of the test method from the test class. Optional.
  • summary - description of the variation. Optional.
  • ticketId - an identifier of the related ticket. Optional.
data Data to be used by a test case.
  • name - a name of variable with extra data. More details. Required.
  • xsi:type - a type of the value. The following data types are available:
    • array
    • string
    • boolean
    • object
    • number
    • null
  • Required.
constraint Reference to the constraint class performed after the test flow.
  • name - a full name of the constraint class. Required.
  • next - a full name of the constraint class performing next in a queue. Optional.
  • prev - a full name of the constraint class performing previous in a queue. Optional.
  

A variation should contain only data that is required for its flow and constraints.

A data set should be placed in the same directory with a corresponding test case.

Data set merging

The FTF enables you to merge data sets from different modules. For example, if you create a new module that adds a menu option to an existing module, the FTF allows you to merge the new data with the existing data sets. As a result, you don’t have to edit the existing module to include the new information, and your tests continue to work. If you decide to later remove the same new module, you don’t need to clean the data sets in other modules.

There are two options to merge data sets in the FTF:

HowTos

A data set is a flexible FTF entity that allows to perform different tasks. Learn their descriptions in the following sections.

Define name in the <data> node

As you can see in the structure table, the name data has a specific structure. Why? To make your test more flexible.

Data mapping by name is performed for the test methods in test case and processAssert() method in constraints. Let’s see the logic of the <data> processing.

Slash / means array nesting. For example:

  • <data name=var/index1>value</data> is converted as var[index1 => value]
  • <data name=var/index1/index2>value</data> is converted as var[index1 => [index2 => value]]

where var is a name of an argument of a test case or a constraint.

If a variable is assigned more than one value:

<data name="price/shopping_cart/total" xsi:type="string">50</data>
<data name="price/product_page/special_price/excluding_tax" xsi:type="string">6</data>

the value is processed as an array:

$price = [
    'data' => [
        'shopping_cart' => [
            'total' => '50'
        ],
        'product_page' => [
            'special_price' => [
                'excluding_tax' => '6'
            ]
        ]
    ]
]

Also, in similar cases you can use array type in a data set, like:

<data name="price" xsi:type="array">
    <item name="shopping_cart" xsi:type="array">
        <item name="total" xsi:type="string">50</item>
    <item name="product_type" xsi:type="array">
        <item name="special_price" xsi:type="array">
            <item name="excluding_tax" xsi:type="string">6</item>
        </item>
    </item>
</data>

Set a simple variable

For example, if a test case or constraint has an argument $price, then the test case takes from the data set all the <data> nodes with a name price. Assume a method with the $price argument.

<?php
public function testCreate($price)
{
    //
}
?>

To assign it with 10 in one of the variations, add the following field to a variation of the corresponding data set:

<data name="price" xsi:type="string">10</data>

Set data to a fixture field

In your test you often need to use injectable fixture instances. For example:

<?php
public function testCreate(\Magento\Catalog\Test\Fixture\CatalogProductSimple $product)
{
    //
}
?>

In this case, the ObjectManager sends data to the InjectableFixture constructor. It declares that your data can be passed to the fixture in $data variable as an array. For example, to assign the existing fixture field weight with 50 you can use the following notation:

 <data name="product/data/weight" xsi:type="string">50</data>

Set data to a fixture from a repository

The InjectableFixture class enables you to use a fixture repository. It can be injected in a $dataset variable. For example, to use dataset = product_with_special_symbols_in_name from the repository assigned in the fixture, you can use:

<data name="product/dataset" xsi:type="string">product_with_special_symbols_in_name</data>

Set data to a fixture field from a repository

You can assign data to a fixture field from its repository.

Let’s see an example:

<data name="product/data/price/dataset" xsi:type="string">drop_down_with_one_option_fixed_price</data>
<data name="product/data/checkout_data/dataset" xsi:type="string">simple_drop_down_with_one_option_fixed_price</data>

A CatalogProductSimple.xml fixture contains the following declarations:

<field name="price" is_required="1" group="product-details" source="Magento\Catalog\Test\Fixture\Product\Price" repository="Magento\Catalog\Test\Repository\CatalogProductSimple\Price" />
<field name="checkout_data" group="null" repository="Magento\Catalog\Test\Repository\CatalogProductSimple\CheckoutData" />

The price fixture field contains the data source that assigns values from a repository. After the InjectableFixture class has passed data to the CatalogProductSimple fixture, Magento\Catalog\Test\Fixture\Product\Price data source receives ['dataset' => 'drop_down_with_one_option_fixed_price'] and assigns values from the ['dataset' => 'drop_down_with_one_option_fixed_price'] of the Magento\Catalog\Test\Repository\CatalogProductSimple\Price repository.

The checkout_data doesn’t contain source and is assigned with values from the Magento\Catalog\Test\Repository\CatalogProductSimple\CheckoutData directly.

Add a new variation

To add a new variation using merging, you should simply use the name of a test case that you want to merge with. For example, you want to add a new variations from the Magento_ProductVideo module to the Magento\Catalog\Test\TestCase\Product\UpdateSimpleProductEntityTest that is placed in the Magento_Catalog module. You can create data set in the Magento_ProductVideo module, containing variations you need, and paste the test case name that you want to merge with:

  • Create <magento2_root_dir>/dev/tests/functional/tests/app/Magento/ProductVideo/Test/TestCase/Product/UpdateSimpleProductEntityTest.xml with the following code:
<?xml version="1.0" encoding="utf-8"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
 -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/mtf/etc/variations.xsd">
    <testCase name="Magento\Catalog\Test\TestCase\Product\UpdateSimpleProductEntityTest" summary="Add Video to PCF" ticketId="PV-1">
        <variation name="DeleteVideoFromPCFTestVariation1" summary="Delete video youtube">
            <data name="initialProduct/dataset" xsi:type="string">product_with_video_youtube</data>
            <data name="product/data/sku" xsi:type="string">sku_simple_product_with_video_%isolation%</data>
            <data name="product/data/media_gallery/images" xsi:type="string" />
            <constraint name="Magento\Catalog\Test\Constraint\AssertProductNoImageInGrid" />
            <constraint name="Magento\Catalog\Test\Constraint\AssertProductForm" />
            <constraint name="Magento\ProductVideo\Test\Constraint\AssertNoVideoCategoryView" />
            <constraint name="Magento\ProductVideo\Test\Constraint\AssertNoVideoProductView" />
        </variation>
        <variation name="DeleteVideoFromPCFTestVariation2" summary="Delete video vimeo">
            <data name="initialProduct/dataset" xsi:type="string">product_with_video_vimeo</data>
            <data name="product/data/sku" xsi:type="string">sku_simple_product_with_video_%isolation%</data>
            <data name="product/data/media_gallery/images" xsi:type="string" />
            <constraint name="Magento\Catalog\Test\Constraint\AssertProductNoImageInGrid" />
            <constraint name="Magento\Catalog\Test\Constraint\AssertProductForm" />
            <constraint name="Magento\ProductVideo\Test\Constraint\AssertNoVideoCategoryView" />
            <constraint name="Magento\ProductVideo\Test\Constraint\AssertNoVideoProductView" />
        </variation>
    </testCase>
</config>

Variations DeleteVideoFromPCFTestVariation1 and DeleteVideoFromPCFTestVariation2 will be used by the Magento\Catalog\Test\TestCase\Product\UpdateSimpleProductEntityTest class during the test run.

Extend a variation with data

If you want to extend variation in another module using merging, you should use a test case name that you want to merge with and a variation name that you want to extend.

For example, see how in Magento/Catalog/Test/TestCase/Product/ValidateOrderOfProductTypeTest.xml

 <?xml version="1.0" encoding="utf-8"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
 -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../vendor/magento/mtf/etc/variations.xsd">
    <testCase name="Magento\Catalog\Test\TestCase\Product\ValidateOrderOfProductTypeTest" summary="Validate product types order on product grid page" ticketId="MAGETWO-37146">
        <variation name="ValidateOrderOfProductTypeTestVariation1">
            <data name="menu/0" xsi:type="string">Simple Product</data>
            <data name="menu/3" xsi:type="string">Virtual Product</data>
            <constraint name="Magento\Catalog\Test\Constraint\AssertProductTypeOrderOnCreate" />
        </variation>
    </testCase>
</config>

 

the variation ValidateOrderOfProductTypeTestVariation1 is extended by the Magento_Bundle module:

 <?xml version="1.0" encoding="utf-8"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
 -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../vendor/magento/mtf/etc/variations.xsd">
    <testCase name="Magento\Catalog\Test\TestCase\Product\ValidateOrderOfProductTypeTest">
        <variation name="ValidateOrderOfProductTypeTestVariation1">
            <data name="menu/4" xsi:type="string">Bundle Product</data>
        </variation>
    </testCase>
</config>

  

Replace a variation

You can replace one variation with another using a replace attribute in the variation node. The replace attribute contains variation that must be replaced by a variation from a name attribute.

<variation name="CreateSuperNewCustomerBackendEntityTestVariation1" replace="CreateCustomerBackendEntityTestVariation1" summary="Variation that replaces default CreateCustomerBackendEntityTestVariation1">

After a merge of a data set with the variation that is mentioned, a test will use CreateSuperNewCustomerBackendEntityTestVariation1 instead of CreateCustomerBackendEntityTestVariation1.