ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
© 2017 Magento, Inc. Page | 1 ‘17
Automated tests in Magento
© 2017 Magento, Inc. Page | 2 ‘17
Software Engineer at Magento
Ievgen Sentiabov
© 2017 Magento, Inc. Page | 3 ‘17
The tests pyramid in Magento
© 2017 Magento, Inc. Page | 4 ‘17
© 2017 Magento, Inc. Page | 5 ‘17
Why we need tests?
• Never be sure that everything work
• Show how bad architecture
• Facilitate refactoring
• Show how system works
• Tests as documentation
© 2017 Magento, Inc. Page | 6
Unit Tests
© 2017 Magento, Inc. Page | 7 ‘17
Example #1
© 2017 Magento, Inc. Page | 8 ‘17
© 2017 Magento, Inc. Page | 9 ‘17
© 2017 Magento, Inc. Page | 10 ‘17
© 2017 Magento, Inc. Page | 11 ‘17
Example #2
© 2017 Magento, Inc. Page | 12 ‘17
Unit test types
https://martinfowler.com/bliki/UnitTest.html
© 2017 Magento, Inc. Page | 13 ‘17
© 2017 Magento, Inc. Page | 14 ‘17
Mocking...
© 2017 Magento, Inc. Page | 15 ‘17
Testing class dependency
© 2017 Magento, Inc. Page | 16 ‘17
Or not???
© 2017 Magento, Inc. Page | 17 ‘17
BUT
2 Units and 0 Integration
© 2017 Magento, Inc. Page | 18 ‘17
Are unit tests useless?
• Describe unit behavior
• Early show code smells
• Code coverage shows corner cases
© 2017 Magento, Inc. Page | 19
Integration Tests
© 2017 Magento, Inc. Page | 20 ‘17
Unit vs. Integration
© 2017 Magento, Inc. Page | 21 ‘17
Benefits of Integration Tests
• Verifying interaction between components
• Catch errors between interfaces
• Show "real" components usage
• Check store dependent configurations
• Based on a system specification
© 2017 Magento, Inc. Page | 22 ‘17
Different Approaches
© 2017 Magento, Inc. Page | 23 ‘17
Does TDD work?
© 2017 Magento, Inc. Page | 24 ‘17
Pitfalls
• Interaction with 3rd party systems
• Complicated fixtures
• Fixtures without rollbacks
© 2017 Magento, Inc. Page | 25 ‘17
Solutions
• Mocking
• Atomic fixtures
• Annotations
@magentoAppIsolation
@magentoDbIsolation
@magentoAppArea
© 2017 Magento, Inc. Page | 26
WEB API
Functional Tests
© 2017 Magento, Inc. Page | 27 ‘17
© 2017 Magento, Inc. Page | 28 ‘17
© 2017 Magento, Inc. Page | 29
Functional Tests
© 2017 Magento, Inc. Page | 30 ‘17
© 2017 Magento, Inc. Page | 31 ‘17
Disadvantages
• Expensive "cost"
• Very slow
• Hard to debug
BUT
• Cover whole system
• Can be used for 3rd party integrations
• Imitate real customer's behavior
© 2017 Magento, Inc. Page | 32
Best Practices
© 2017 Magento, Inc. Page | 33 ‘17
Code coverage
© 2017 Magento, Inc. Page | 34 ‘17
Atomic fixtures
Quote
Items
Address
Payment
Items
Address
Payment
Quote
© 2017 Magento, Inc. Page | 35 ‘17
Clean the state
© 2017 Magento, Inc. Page | 36 ‘17
In general...
• Choose testing strategy
• Write rollbacks for fixtures (WEB API Functional)
• Minimum assertions per test
• Avoid loops and conditions in tests
• Test public code
• Cover corner cases
© 2017 Magento, Inc. Page | 37
In the end
© 2017 Magento, Inc. Page | 38 ‘17
Summary
• Writing effective tests is hard
• Tests help find possible bugs earlier
• Improve an architecture
• Allow refactor code safely
• Can be used as system documentation

More Related Content

Automated tests in Magento

  • 1. © 2017 Magento, Inc. Page | 1 ‘17 Automated tests in Magento
  • 2. © 2017 Magento, Inc. Page | 2 ‘17 Software Engineer at Magento Ievgen Sentiabov
  • 3. © 2017 Magento, Inc. Page | 3 ‘17 The tests pyramid in Magento
  • 4. © 2017 Magento, Inc. Page | 4 ‘17
  • 5. © 2017 Magento, Inc. Page | 5 ‘17 Why we need tests? • Never be sure that everything work • Show how bad architecture • Facilitate refactoring • Show how system works • Tests as documentation
  • 6. © 2017 Magento, Inc. Page | 6 Unit Tests
  • 7. © 2017 Magento, Inc. Page | 7 ‘17 Example #1
  • 8. © 2017 Magento, Inc. Page | 8 ‘17
  • 9. © 2017 Magento, Inc. Page | 9 ‘17
  • 10. © 2017 Magento, Inc. Page | 10 ‘17
  • 11. © 2017 Magento, Inc. Page | 11 ‘17 Example #2
  • 12. © 2017 Magento, Inc. Page | 12 ‘17 Unit test types https://martinfowler.com/bliki/UnitTest.html
  • 13. © 2017 Magento, Inc. Page | 13 ‘17
  • 14. © 2017 Magento, Inc. Page | 14 ‘17 Mocking...
  • 15. © 2017 Magento, Inc. Page | 15 ‘17 Testing class dependency
  • 16. © 2017 Magento, Inc. Page | 16 ‘17 Or not???
  • 17. © 2017 Magento, Inc. Page | 17 ‘17 BUT 2 Units and 0 Integration
  • 18. © 2017 Magento, Inc. Page | 18 ‘17 Are unit tests useless? • Describe unit behavior • Early show code smells • Code coverage shows corner cases
  • 19. © 2017 Magento, Inc. Page | 19 Integration Tests
  • 20. © 2017 Magento, Inc. Page | 20 ‘17 Unit vs. Integration
  • 21. © 2017 Magento, Inc. Page | 21 ‘17 Benefits of Integration Tests • Verifying interaction between components • Catch errors between interfaces • Show "real" components usage • Check store dependent configurations • Based on a system specification
  • 22. © 2017 Magento, Inc. Page | 22 ‘17 Different Approaches
  • 23. © 2017 Magento, Inc. Page | 23 ‘17 Does TDD work?
  • 24. © 2017 Magento, Inc. Page | 24 ‘17 Pitfalls • Interaction with 3rd party systems • Complicated fixtures • Fixtures without rollbacks
  • 25. © 2017 Magento, Inc. Page | 25 ‘17 Solutions • Mocking • Atomic fixtures • Annotations @magentoAppIsolation @magentoDbIsolation @magentoAppArea
  • 26. © 2017 Magento, Inc. Page | 26 WEB API Functional Tests
  • 27. © 2017 Magento, Inc. Page | 27 ‘17
  • 28. © 2017 Magento, Inc. Page | 28 ‘17
  • 29. © 2017 Magento, Inc. Page | 29 Functional Tests
  • 30. © 2017 Magento, Inc. Page | 30 ‘17
  • 31. © 2017 Magento, Inc. Page | 31 ‘17 Disadvantages • Expensive "cost" • Very slow • Hard to debug BUT • Cover whole system • Can be used for 3rd party integrations • Imitate real customer's behavior
  • 32. © 2017 Magento, Inc. Page | 32 Best Practices
  • 33. © 2017 Magento, Inc. Page | 33 ‘17 Code coverage
  • 34. © 2017 Magento, Inc. Page | 34 ‘17 Atomic fixtures Quote Items Address Payment Items Address Payment Quote
  • 35. © 2017 Magento, Inc. Page | 35 ‘17 Clean the state
  • 36. © 2017 Magento, Inc. Page | 36 ‘17 In general... • Choose testing strategy • Write rollbacks for fixtures (WEB API Functional) • Minimum assertions per test • Avoid loops and conditions in tests • Test public code • Cover corner cases
  • 37. © 2017 Magento, Inc. Page | 37 In the end
  • 38. © 2017 Magento, Inc. Page | 38 ‘17 Summary • Writing effective tests is hard • Tests help find possible bugs earlier • Improve an architecture • Allow refactor code safely • Can be used as system documentation