2. What is Magento?
One of the most popular Ecommerce platforms
All the stuff that one web shop needs
Used on 1.3% of all websites (http://w3techs.com/technologies/overview/content_management/all)
Based on PHP, MySQL….
Free and paid versions
Modular approach (Catalog, Category, Checkout, Sales…)
3. Magento grew over years
Huge number of extensions
Huge number of features
Huge community
4. Magento 2
Announced 2010
Refactor / rewrite
Goals:
Modern tech stack
Improved performance and scalability
Streamline customizations
Simplify external integrations
Easier installation and upgrades
High code quality and testing
More info: http://alankent.me/2014/05/17/magento-2-goals/
6. Automated testing vs manual testing
Executed more quickly
Reusable
Everyone can see results
Costs less
More reliable
More interesting
More info: http://www.base36.com/2013/03/automated-vs-manual-testing-the-pros-and-cons-of-each/
7. Automated tests in Magento 2
Unit tests
Integration tests
Functional tests
Other tests (performance, legacy...):
More info: http://alankent.me/2014/06/28/magento-2-test-automation/
9. “Integration tests show that the major parts of a system work
well together”
- The Pragmatic Programmer Book
10. The official Magento automated testing standard
Integration tests:
Deployed on close to real environment.
Integration tests verify interaction of components between each other and with system
environment (database, file system).
The goals are to tackle cross-dependencies between components/modules and environment-
related issues.
Integration test can also be considered as a "small" functional test, so its goal to preserve
functionality
More info: https://github.com/magento/magento2/wiki/Magento-Automated-Testing-Standard
11. Difference from unit tests
Unit tests test the smallest units of code, integration tests test how those units
work together
Integration tests touch more code
Integration tests have less mocking and less isolation
Unit tests are faster
Unit tests are easier to debug
12. Difference from functional tests
Functional tests compare against the specification
Functional tests are slower
Functional tests can tell us if something visible to the customer broke
13. Integration tests in Magento 2
Based on PHPUnit
Can touch the database and filesystem
Have separate database
Separated into modules
Magento core code is tested with integration tests
Make updates easier
From my experience most of the common mistakes would be picked up by
integration tests
14. Setup
bin/magento dev:test:run integration
cd dev/tests/integration && phpunit -c phpunit.xml
Separate database: dev/tests/integration/etc/install-config-mysql.php.dist
Configuration in dev/tests/integration/phpunit.xml.dist
TESTS_CLEANUP
TESTS_MAGENTO_MODE
TESTS_ERROR_LOG_LISTENER_LEVEL
15. Annotations
/**
* Test something
*
* @magentoConfigFixture currency/options/allow USD
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
*/
public function testSomething()
{
}
16. Annotations
/**
* Test something
*
* @magentoConfigFixture currency/options/allow USD
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
*/
public function testSomething()
{
}
17. What are annotations
Meta data used to inject some behaviour
Placed in docblocks
Change test behaviour
PHPUnit_Framework_TestListener
onTestStart, onTestSuccess, onTestFailure, etc
More info: http://dusanlukic.com/annotations-in-magento-2-integration-tests
18. @magentoDbIsolation
when enabled wraps the test in a transaction
enabled - makes sure that the tests don’t affect each other
disabled - useful for debugging as data remains in database
can be applied on class level and on method level
23. Test that that non existing category goes to 404
class CategoryTest extends
MagentoTestFrameworkTestCaseAbstractController
{
/*...*/
public function testViewActionInactiveCategory()
{
$this->dispatch('catalog/category/view/id/8');
$this->assert404NotFound();
}
/*...*/
}
25. Real life example, Fooman_EmailAttachments
Most of the stores have terms and agreement
An email is sent to the customer after each successful order
Goal: Provide the option of attaching terms and agreement to order
success email
31. Test if block is added correctly
<?php
namespace LDusanSampleController;
class ActionTest extends MagentoTestFrameworkTestCaseAbstractController
{
/**
* @magentoDataFixture Magento/Catalog/_files/product_special_price.php
*/
public function testBlockAdded()
{
$this->dispatch('catalog/product/view/id/' . $product->getId());
$layout = MagentoTestFrameworkHelperBootstrap::getObjectManager()->get(
'MagentoFrameworkViewLayoutInterface'
);
$this->assertArrayHasKey('ldusan-sample-block', $layout->getAllBlocks());
}
}
32. To summarize
Integration tests are not:
The only tests that you should write
Integration tests are:
A way to check if something really works as expected
Proof that our code works well with the environment