Β© 2018 Magento, Inc. Page | 1
Automated Testing in
Magento 2
Igor Miniailo
Magento 2 Architect
Β© 2018 Magento, Inc. Page | 2
Β© 2018 Magento, Inc. Page | 3
Β© 2018 Magento, Inc. Page | 4
*Automated Testing in Magento 2
based on examples from Multi-Source
Inventory
Β© 2018 Magento, Inc. Page | 5
MSI is designed to enable stock management in multiple
locations so that merchants can properly reflect their physical
warehouses in Magento system without external extensions or
customization
Multi-Source Inventory (MSI)
https://github.com/magento-engcom/msi/
Β© 2018 Magento, Inc. Page | 6
Split the inventory
between the
sources within one
Magento installation
Β© 2018 Magento, Inc. Page | 7
Replace current CatalogInventory with MSI
Β© 2018 Magento, Inc. Page | 8
Β© 2018 Magento, Inc. Page | 9
Β© 2018 Magento, Inc. Page | 10
Unit Testing
Β© 2018 Magento, Inc. Page | 11
Unit test types
https://martinfowler.com/bliki/UnitTest.html
Β© 2018 Magento, Inc. Page | 12
Test Doubles
https://martinfowler.com/bliki/TestDouble.html
Β© 2018 Magento, Inc. Page | 13
Β© 2018 Magento, Inc. Page | 14
Let’s see how typical Unit test for this code looks like
Β© 2018 Magento, Inc. Page | 15
Solitary Unit Test with Mocks and Expects
Β© 2018 Magento, Inc. Page | 16
TTDD - Tautological Test Driven Development
Tautological: - needless, redundant repetition of an idea - repetition of
same sense in different words; - the phrase "a beginner who has just
started" is tautological
Β© 2018 Magento, Inc. Page | 17
TTDD - Tautological Test Driven Development
Unit tests currently do not check that code works
properly, but check that code works the same way
as we implemented it.
Coupling between Production and Testing code.
Test duplicates the business logic code.
Β© 2018 Magento, Inc. Page | 18
http://fabiopereira.me/blog/2010/05/27/ttdd-tautological-
test-driven-development-anti-pattern/
Β© 2018 Magento, Inc. Page | 19
β€’ Asserts more interactions with collaborators than the outputs
β€’ It doesn’t really test the behavior of the class, but only its
implementation
β€’ The test is too white box
β€’ Too much mock setup deviates the intent of the test
β€’ Adding a new feature or changing an existing one requires
changing mock expectations
β€’ Such tests don’t help to find bugs in application code
Β© 2018 Magento, Inc. Page | 20
The problem is not in Unit tests
The problem is in Transient State of objects
under the test
(especially in legacy code)
Β© 2018 Magento, Inc. Page | 21
Β© 2018 Magento, Inc. Page | 22
Two Unit tests passed. No Integration tests
Β© 2018 Magento, Inc. Page | 23
Two Unit tests passed. No Integration tests
Β© 2018 Magento, Inc. Page | 24
Whether Unit tests are useless?
NO!
Unit tests could be the first β€œSmell” informing that
something wrong with your application code
Β© 2018 Magento, Inc. Page | 25
β€’ Readability
– Test should be easy to follow
– Test should be simple to update
– Code can be reused to test all similar cases
β€’ Stability
– Test should be stable by execution
– Test should be stable to code changes
– Test should focus on result not on the path
β€’ Speed
Qualities of a Test
Β© 2018 Magento, Inc. Page | 26
Integration Testing
Β© 2018 Magento, Inc. Page | 27
Reservation - the entity is used when the order is placed and
we need to reserve some product quantity in stock.
Reservations are append only operations and help us to prevent
blocking operations and race conditions at the time of checkout.
Reservation mechanism
https://github.com/magento-engcom/msi/wiki/Reservations
Β© 2018 Magento, Inc. Page | 28
Order Placement – Step 1
France Warehouse
SKU-1: 5qty
EU Stock
SKU-1: 15qty
Italy Warehouse
SKU-1: 10qty
Raw data Index Reservations
No records
Available Qty: 15qty (data from index, empty reservations)
Β© 2018 Magento, Inc. Page | 29
Order Placement – Step 2
Action: Customer buys 5 products on frontend
Β© 2018 Magento, Inc. Page | 30
Order Placement – Step 3
France Warehouse
SKU-1: 5qty
EU Stock
SKU-1: 15qty
Italy Warehouse
SKU-1: 10qty
Raw data Index Reservations
SKU-1: -5
Available Qty: 10qty (data from index 15, apply all reservations -5)
Data is NOT changed Reservation has been
added
Β© 2018 Magento, Inc. Page | 31
Order Placement – Step 4
Action: Admin makes a re-order, 3 products out of 5 returned,
and new order consists of 2 products
Β© 2018 Magento, Inc. Page | 32
Order Placement – Step 5
France Warehouse
SKU-1: 5qty
EU Stock
SKU-1: 15qty
Italy Warehouse
SKU-1: 10qty
Raw data Index Reservations
SKU-1: -5
SKU-1: +3
Available Qty: 13qty (data from index 15, apply reservations -5+3)
Data is NOT changed
Reservation has been added
Β© 2018 Magento, Inc. Page | 33
Order Placement – Step 6
Action: Admin completes order. Re-index was run.
Β© 2018 Magento, Inc. Page | 34
Order Placement – Step 7
France Warehouse
SKU-1: 3qty
EU Stock
SKU-1: 13qty
Italy Warehouse
SKU-1: 10qty
Raw data Index Reservations
SKU-1: -5
SKU-1: +3
SKU-1: +2
Available Qty: 13qty (data from index 13, apply reservations -5+3+2=0)
Data is CHANGED Compensation Reservation
has been added
Β© 2018 Magento, Inc. Page | 35
Order Placement – Step 8
Action: Reservation cleaning
Looping through these reservations we could find reservations
which in sum gives 0 (Zero) and remove them.
Β© 2018 Magento, Inc. Page | 36
Order Placement – Step 9 (like Step 1)
France Warehouse
SKU-1: 3qty
EU Stock
SKU-1: 13qty
Italy Warehouse
SKU-1: 10qty
Raw data Index Reservations
Available Qty: 13qty (data from index, empty reservations)
Data is NOT changed Reservations have been removed
No records
Β© 2018 Magento, Inc. Page | 37
API interface to test
Β© 2018 Magento, Inc. Page | 38
So, what’s about testing?
Β© 2018 Magento, Inc. Page | 39
What about fixtures?
Β© 2018 Magento, Inc. Page | 40
Β© 2018 Magento, Inc. Page | 41
Tests Isolation
Β© 2018 Magento, Inc. Page | 42
Using DocBlock Annotations
http://devdocs.magento.com/guides/v2.2/test/integration/annotations.html
Β© 2018 Magento, Inc. Page | 43
Β© 2018 Magento, Inc. Page | 44
Clean the state after your tests!
Β© 2018 Magento, Inc. Page | 45
Cleaning up the state. Rollback Fixtures
Β© 2018 Magento, Inc. Page | 46
Cleaning up the state. Rollback Fixtures
Β© 2018 Magento, Inc. Page | 47
Cleaning up the state
Β© 2018 Magento, Inc. Page | 48
β€’ Readability
– Test should be easy to follow
– Test should be simple to update
– Code can be reused to test all similar cases
β€’ Stability
– Test should be stable by execution
– Test should be stable to code changes
– Test should focus on result not on the path
β€’ Speed
Qualities of a Test
Β© 2018 Magento, Inc. Page | 49
Web API Testing
Β© 2018 Magento, Inc. Page | 50
Β© 2018 Magento, Inc. Page | 51
Β© 2018 Magento, Inc. Page | 52
Β© 2018 Magento, Inc. Page | 53
Headless Magento
Β© 2018 Magento, Inc. Page | 54
RESTful API
Β© 2018 Magento, Inc. Page | 55
Swagger
http://devdocs.magento.com/guides/v2.2/rest/generate-local.html
Β© 2018 Magento, Inc. Page | 56
Web API tests
https://github.com/magento-engcom/msi/wiki/How-to-create-Web-API-and-How-To-cover-them-with-Functional-Testing
Β© 2018 Magento, Inc. Page | 57
Database Generated IDs
Β© 2018 Magento, Inc. Page | 58
Why can’t we just use predefined IDs?
Β© 2018 Magento, Inc. Page | 59
Predefined ID approach (used in MSI)
Β© 2018 Magento, Inc. Page | 60
Predefined IDs matter
Just for this particular case (Source to Stock assignment) - we got rid of more
than 300 lines of boilerplate code in our Integration tests using Pre-Generated
IDs approach.
https://github.com/magento-engcom/msi/wiki/The-first-step-towards-pre-generated-
IDs.-And-how-this-will-improve-your-Integration-tests
Β© 2018 Magento, Inc. Page | 61
Be Responsible or train your dog tests to be!
Β© 2018 Magento, Inc. Page | 62
Static Testing
Β© 2018 Magento, Inc. Page | 63
Strict Typing
Β© 2018 Magento, Inc. Page | 64
declare(strict_types=1)
Β© 2018 Magento, Inc. Page | 65
Β© 2018 Magento, Inc. Page | 66
LiveCodeTest
Β© 2018 Magento, Inc. Page | 67
Functional Testing
Β© 2018 Magento, Inc. Page | 68
http://devdocs.magento.com/guides/v2.2/mtf/mtf_introduction.html
Β© 2017 Magento, Inc. Page | 69
How to join us? Send an email to
engcom@magento.com
@iminyaylo
Thank y’all!

Automated Testing in Magento 2

  • 1.
    Β© 2018 Magento,Inc. Page | 1 Automated Testing in Magento 2 Igor Miniailo Magento 2 Architect
  • 2.
    Β© 2018 Magento,Inc. Page | 2
  • 3.
    Β© 2018 Magento,Inc. Page | 3
  • 4.
    Β© 2018 Magento,Inc. Page | 4 *Automated Testing in Magento 2 based on examples from Multi-Source Inventory
  • 5.
    Β© 2018 Magento,Inc. Page | 5 MSI is designed to enable stock management in multiple locations so that merchants can properly reflect their physical warehouses in Magento system without external extensions or customization Multi-Source Inventory (MSI) https://github.com/magento-engcom/msi/
  • 6.
    Β© 2018 Magento,Inc. Page | 6 Split the inventory between the sources within one Magento installation
  • 7.
    Β© 2018 Magento,Inc. Page | 7 Replace current CatalogInventory with MSI
  • 8.
    Β© 2018 Magento,Inc. Page | 8
  • 9.
    Β© 2018 Magento,Inc. Page | 9
  • 10.
    Β© 2018 Magento,Inc. Page | 10 Unit Testing
  • 11.
    Β© 2018 Magento,Inc. Page | 11 Unit test types https://martinfowler.com/bliki/UnitTest.html
  • 12.
    Β© 2018 Magento,Inc. Page | 12 Test Doubles https://martinfowler.com/bliki/TestDouble.html
  • 13.
    Β© 2018 Magento,Inc. Page | 13
  • 14.
    Β© 2018 Magento,Inc. Page | 14 Let’s see how typical Unit test for this code looks like
  • 15.
    Β© 2018 Magento,Inc. Page | 15 Solitary Unit Test with Mocks and Expects
  • 16.
    Β© 2018 Magento,Inc. Page | 16 TTDD - Tautological Test Driven Development Tautological: - needless, redundant repetition of an idea - repetition of same sense in different words; - the phrase "a beginner who has just started" is tautological
  • 17.
    Β© 2018 Magento,Inc. Page | 17 TTDD - Tautological Test Driven Development Unit tests currently do not check that code works properly, but check that code works the same way as we implemented it. Coupling between Production and Testing code. Test duplicates the business logic code.
  • 18.
    Β© 2018 Magento,Inc. Page | 18 http://fabiopereira.me/blog/2010/05/27/ttdd-tautological- test-driven-development-anti-pattern/
  • 19.
    Β© 2018 Magento,Inc. Page | 19 β€’ Asserts more interactions with collaborators than the outputs β€’ It doesn’t really test the behavior of the class, but only its implementation β€’ The test is too white box β€’ Too much mock setup deviates the intent of the test β€’ Adding a new feature or changing an existing one requires changing mock expectations β€’ Such tests don’t help to find bugs in application code
  • 20.
    Β© 2018 Magento,Inc. Page | 20 The problem is not in Unit tests The problem is in Transient State of objects under the test (especially in legacy code)
  • 21.
    Β© 2018 Magento,Inc. Page | 21
  • 22.
    Β© 2018 Magento,Inc. Page | 22 Two Unit tests passed. No Integration tests
  • 23.
    Β© 2018 Magento,Inc. Page | 23 Two Unit tests passed. No Integration tests
  • 24.
    Β© 2018 Magento,Inc. Page | 24 Whether Unit tests are useless? NO! Unit tests could be the first β€œSmell” informing that something wrong with your application code
  • 25.
    Β© 2018 Magento,Inc. Page | 25 β€’ Readability – Test should be easy to follow – Test should be simple to update – Code can be reused to test all similar cases β€’ Stability – Test should be stable by execution – Test should be stable to code changes – Test should focus on result not on the path β€’ Speed Qualities of a Test
  • 26.
    Β© 2018 Magento,Inc. Page | 26 Integration Testing
  • 27.
    Β© 2018 Magento,Inc. Page | 27 Reservation - the entity is used when the order is placed and we need to reserve some product quantity in stock. Reservations are append only operations and help us to prevent blocking operations and race conditions at the time of checkout. Reservation mechanism https://github.com/magento-engcom/msi/wiki/Reservations
  • 28.
    Β© 2018 Magento,Inc. Page | 28 Order Placement – Step 1 France Warehouse SKU-1: 5qty EU Stock SKU-1: 15qty Italy Warehouse SKU-1: 10qty Raw data Index Reservations No records Available Qty: 15qty (data from index, empty reservations)
  • 29.
    Β© 2018 Magento,Inc. Page | 29 Order Placement – Step 2 Action: Customer buys 5 products on frontend
  • 30.
    Β© 2018 Magento,Inc. Page | 30 Order Placement – Step 3 France Warehouse SKU-1: 5qty EU Stock SKU-1: 15qty Italy Warehouse SKU-1: 10qty Raw data Index Reservations SKU-1: -5 Available Qty: 10qty (data from index 15, apply all reservations -5) Data is NOT changed Reservation has been added
  • 31.
    Β© 2018 Magento,Inc. Page | 31 Order Placement – Step 4 Action: Admin makes a re-order, 3 products out of 5 returned, and new order consists of 2 products
  • 32.
    Β© 2018 Magento,Inc. Page | 32 Order Placement – Step 5 France Warehouse SKU-1: 5qty EU Stock SKU-1: 15qty Italy Warehouse SKU-1: 10qty Raw data Index Reservations SKU-1: -5 SKU-1: +3 Available Qty: 13qty (data from index 15, apply reservations -5+3) Data is NOT changed Reservation has been added
  • 33.
    Β© 2018 Magento,Inc. Page | 33 Order Placement – Step 6 Action: Admin completes order. Re-index was run.
  • 34.
    Β© 2018 Magento,Inc. Page | 34 Order Placement – Step 7 France Warehouse SKU-1: 3qty EU Stock SKU-1: 13qty Italy Warehouse SKU-1: 10qty Raw data Index Reservations SKU-1: -5 SKU-1: +3 SKU-1: +2 Available Qty: 13qty (data from index 13, apply reservations -5+3+2=0) Data is CHANGED Compensation Reservation has been added
  • 35.
    Β© 2018 Magento,Inc. Page | 35 Order Placement – Step 8 Action: Reservation cleaning Looping through these reservations we could find reservations which in sum gives 0 (Zero) and remove them.
  • 36.
    Β© 2018 Magento,Inc. Page | 36 Order Placement – Step 9 (like Step 1) France Warehouse SKU-1: 3qty EU Stock SKU-1: 13qty Italy Warehouse SKU-1: 10qty Raw data Index Reservations Available Qty: 13qty (data from index, empty reservations) Data is NOT changed Reservations have been removed No records
  • 37.
    Β© 2018 Magento,Inc. Page | 37 API interface to test
  • 38.
    Β© 2018 Magento,Inc. Page | 38 So, what’s about testing?
  • 39.
    Β© 2018 Magento,Inc. Page | 39 What about fixtures?
  • 40.
    Β© 2018 Magento,Inc. Page | 40
  • 41.
    Β© 2018 Magento,Inc. Page | 41 Tests Isolation
  • 42.
    Β© 2018 Magento,Inc. Page | 42 Using DocBlock Annotations http://devdocs.magento.com/guides/v2.2/test/integration/annotations.html
  • 43.
    Β© 2018 Magento,Inc. Page | 43
  • 44.
    Β© 2018 Magento,Inc. Page | 44 Clean the state after your tests!
  • 45.
    Β© 2018 Magento,Inc. Page | 45 Cleaning up the state. Rollback Fixtures
  • 46.
    Β© 2018 Magento,Inc. Page | 46 Cleaning up the state. Rollback Fixtures
  • 47.
    Β© 2018 Magento,Inc. Page | 47 Cleaning up the state
  • 48.
    Β© 2018 Magento,Inc. Page | 48 β€’ Readability – Test should be easy to follow – Test should be simple to update – Code can be reused to test all similar cases β€’ Stability – Test should be stable by execution – Test should be stable to code changes – Test should focus on result not on the path β€’ Speed Qualities of a Test
  • 49.
    Β© 2018 Magento,Inc. Page | 49 Web API Testing
  • 50.
    Β© 2018 Magento,Inc. Page | 50
  • 51.
    Β© 2018 Magento,Inc. Page | 51
  • 52.
    Β© 2018 Magento,Inc. Page | 52
  • 53.
    Β© 2018 Magento,Inc. Page | 53 Headless Magento
  • 54.
    Β© 2018 Magento,Inc. Page | 54 RESTful API
  • 55.
    Β© 2018 Magento,Inc. Page | 55 Swagger http://devdocs.magento.com/guides/v2.2/rest/generate-local.html
  • 56.
    Β© 2018 Magento,Inc. Page | 56 Web API tests https://github.com/magento-engcom/msi/wiki/How-to-create-Web-API-and-How-To-cover-them-with-Functional-Testing
  • 57.
    Β© 2018 Magento,Inc. Page | 57 Database Generated IDs
  • 58.
    Β© 2018 Magento,Inc. Page | 58 Why can’t we just use predefined IDs?
  • 59.
    Β© 2018 Magento,Inc. Page | 59 Predefined ID approach (used in MSI)
  • 60.
    Β© 2018 Magento,Inc. Page | 60 Predefined IDs matter Just for this particular case (Source to Stock assignment) - we got rid of more than 300 lines of boilerplate code in our Integration tests using Pre-Generated IDs approach. https://github.com/magento-engcom/msi/wiki/The-first-step-towards-pre-generated- IDs.-And-how-this-will-improve-your-Integration-tests
  • 61.
    Β© 2018 Magento,Inc. Page | 61 Be Responsible or train your dog tests to be!
  • 62.
    Β© 2018 Magento,Inc. Page | 62 Static Testing
  • 63.
    Β© 2018 Magento,Inc. Page | 63 Strict Typing
  • 64.
    Β© 2018 Magento,Inc. Page | 64 declare(strict_types=1)
  • 65.
    Β© 2018 Magento,Inc. Page | 65
  • 66.
    Β© 2018 Magento,Inc. Page | 66 LiveCodeTest
  • 67.
    Β© 2018 Magento,Inc. Page | 67 Functional Testing
  • 68.
    Β© 2018 Magento,Inc. Page | 68 http://devdocs.magento.com/guides/v2.2/mtf/mtf_introduction.html
  • 69.
    Β© 2017 Magento,Inc. Page | 69 How to join us? Send an email to engcom@magento.com @iminyaylo Thank y’all!