LEAVE A COMMENT
Send message
Reply
How to work with Integration Tests in Magento 2 

How to work with Integration Tests in Magento 2 

  • Our Blog
  • How to work with Integration Tests in Magento 2 
Table of contents
    Alexander Shuklin
    Alexander Shuklin
    Senior Magento / Magento 2 developer

    How to work with Integration Tests in Magento 2 

    Integration testing is a type of testing where the real dependencies are used, to verify that the modules or components when integrated work individually as expected without any issues.

    Prepare environment:

    1. Use an existing or install a new magento project

    2. Create a separate empty database for tests

    3. Copy the file dev/tests/integration/etc/install-config-mysql.php.dist

    to dev/tests/integration/etc/install-config-mysql.php and adjust the access data

    to your newly created database. Attention: don’t change the backend-frontname setting!

    4. Copy the file dev/tests/integration/phpunit.xml.dist to

    dev/tests/integration/phpunit.xml. If you don’t adjust the file after that, 

    you’ll be running the Magento 2 core testsuite which will take a few hours. 

    Change the part inside <testsuites>…</testsuites>

    overwrite VENDOR_NAME to yours.

    5. To run your tests use command: bin/magento dev:tests:run integration

    TestCase class

    Main class which is used for testing is PHPUnit\Framework\TestCase.

    This class consists of many different methods to compare the actual result with the expected result.

    Your test classes will be always extended from this class directly or through a parent class 

    Testing method names start from assert prefix.

    For example: TestCase::assertStringContainsString

    checks if some substring is included in the string, similar to PHP strpos function.

    List of all methods you can view in TestCase class

    Two important TestCase class methods are as follows:

    1. TestCase::setUp – this method is used to create additional objects or data. Something similar to __construct, but without parameters. This method will be called automatically before your test will execute

    2. TestCase::tearDown – this method is used to destroy/remove/reset all data in your class/db/filesystem. Something similar to __destruct. This method will be called automatically after your test execution finished not depends of test result 

    Test class structure and example

    Recommendations:

    1. Class name should have the suffix Test. Example: ProductPriceTest 

    2. Method which will be called for testing should be public, should have test

       suffix in its name or @test string in method docblock

    3. Try to use one class for one test instead of multiple test methods in one.

    4. I prefer to use structures which should be clear for other developers.

    So I create separate methods for each part of the test.

    Methods to prepare data (given….), methods to execute functionality (when…) and

    methods to check the result (then…). You can create a couple methods for each

    test part. The structure can be: given…, when…, then1…. and then2…

    Try to use structure for test method something like this:

    In givenSomething method you can prepare your data or configuration or even request params.

    In whenDoSomething method you execute your functionality which should be tested.

    In thenResultShouldBe method you can check the execution result.

    Full test example with comments: 

    Explanation:

    In this class I want to test if product name setter and getter works as expected.

    1. Prepare additional class objects in setUp method to use them for testing

    2. Create a checkProductName method which will be called when you run the tests command.

    I added @test docblock to this method for declaring it as a test method 

    4. In test method I create a product object (method givenProduct)

    5. After I set product name with test data (method whenSetProductName)

    6. Then I get product name and compare it with previously set test data (method thenCheckProductName

    7. Reset product object in tearDown method

    0 Comments
    The field is required.
    The field is required.
    The field is required.

    Every successful venture is rooted in a bold decision once made.

    Peter Drucker

    Fill out the form

    tell us about your needs. together we’ll find the best path to take.

    By clicking on send you will confirm that you’ve read and accepted our Privacy Notice.

    Your message has been sent