Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Testing in Magento 2
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
1. Running Tests
2. Writing Tests
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@Vin...
Running all the tests
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
$ bin/magento dev:tests:run [all | unit | integration]
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp...
Running specific tests
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
Unit Tests
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
$ vendor/bin/phpunit 
--config "dev/tests/unit/phpunit.xml.dist" 
["app/code/My/Module/Test/Unit"]
Testing in Magento 2 - ...
$ alias unit-test="$(pwd)/vendor/bin/phpunit
-c $(pwd)/dev/tests/unit/phpunit.xml.dist"
Testing in Magento 2 - #MageTestFe...
$ unit-test
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
$ unit-test "app/code/My/Module/Test/Unit"
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitt...
Integration Test
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
What is the difference?
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
Real Objects Interact
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
Integration Tests need the
Runtime Environment
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - t...
Integration Test Framework Bootstrap:
1. If TESTS_CLEANUP is set
4 clear test environment cache
4 uninstall Magento
2. If ...
Separate DB
cp dev/tests/integration/etc/install-config-mysql.php.dist 
dev/tests/integration/etc/install-config-mysql.php...
Tweak settings
cp dev/tests/integration/phpunit.xml.dist 
dev/tests/integration/phpunit.xml
Testing in Magento 2 - #MageTe...
dev/tests/integration/phpunit.xml.dist
<phpunit>
...
<php>
...
<const name="TESTS_CLEANUP" value="enabled"/>
<!--<const na...
TESTS_CLEANUP == disabled
speeds things up
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitt...
TESTS_CLEANUP == disabled
manual cleanup required
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com ...
Enable TESTS_EXTRA_VERBOSE_LOG
in phpunit.xml to see what is going on
during bootstrap.
Testing in Magento 2 - #MageTestFe...
Every run gets it's own
etc, pub and var dirs
dev/tests/integration/tmp/sandbox-0-
b79c13eb842cf3211459b11c775bfbde/
Testi...
rm -r integration/tmp/sandbox-*
fresh start!
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twi...
Executing Integration Tests
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
Executing integration tests
!= unit tests
$ vendor/bin/phpunit -c dev/tests/integration/phpunit.xml
Could not read "dev/te...
$ cd dev/tests/integration
$ ../../../vendor/bin/phpunit
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaiko...
Running Tests in
PHPStorm
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
1. Set up PHP interpreter
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
2. Set up PHPUnit (1/2)
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
3. Set up PHPUnit (2/2)
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
4. Create run configuration
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
5. Run tests with one of
^ F10
^ F9 (debug)
or click:
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp....
Writing Tests
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
Writing Unit Tests
Nothing Magento 2 specific
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - tw...
Writing Integration Tests
Very Magento 2 specific
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com ...
Magento 2 Testing Framework
dev/tests/integration/framework
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vina...
ObjectManager is a'okay!
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
Test Framework ObjectManager
MagentoTestFrameworkObjectManager::getInstance()
Testing in Magento 2 - #MageTestFest, Nov. 2...
Fixtures
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
Fixtures
Fix the system into a known state
before a test is executed.
Testing in Magento 2 - #MageTestFest, Nov. 2017 - co...
/**
* @magentoDataFixture Magento/Sales/_files/order.php
* @magentoDataFixture Magento/Customer/_files/customer.php
* @mag...
Complex Fixtures:
(e.g. Sessions, Order Placement, Customer...)
https://github.com/tddwizard/magento2-fixtures
Testing in ...
Config Fixtures
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
Store Scope
/**
* @magentoConfigFixture current_store catalog/price/scope 1
*/
public function testDoesSomeoneReadThisHelp...
Global Scope
/**
* @magentoConfigFixture currency/options/allow USD
*/
public function testTastesLikeMashAndPie()
{
// ......
Test Isolation
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
DataBase Isolation
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
Transactions
/**
* @magentoDbIsolation enabled
*/
public function testWithSideEffects
{
// ...
Testing in Magento 2 - #Mag...
Transactions
/**
* @magentoDataFixture Magento/Foo/_files/bar.php
*/
public function testTheFixtureIsAppliedWithinATransac...
Application Isolation
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
Reset Magento after test
/**
* @magentoAppIsolation enabled
*/
public function testTestWithSingletons()
{
Testing in Magen...
Selecting a application area
/**
* @magentoAppArea frontend
*/
public function testRendersTheCustomerNameInWelcomeMessage(...
Most important!
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
Choose the right test granularity based
on
4 Business Value
4 Personal Skill
4 Available Time
Testing in Magento 2 - #Mage...
Don't test for testing's sake
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKo...
Make the tests valuable
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
Time for a sip of
☕
before we dive in!
Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter:/...
Upcoming SlideShare
Loading in …5
×

Testing Magento 2

712 views

Published on

How to set up Magento to for running tests and basics on using the Magento 2 integration test framework.
This presentation was given at #MageTestFest in Amersfoort in on November 17th, 2017.

Published in: Technology
  • Be the first to comment

Testing Magento 2

  1. 1. Testing in Magento 2 Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  2. 2. 1. Running Tests 2. Writing Tests Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  3. 3. Running all the tests Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  4. 4. $ bin/magento dev:tests:run [all | unit | integration] Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  5. 5. Running specific tests Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  6. 6. Unit Tests Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  7. 7. $ vendor/bin/phpunit --config "dev/tests/unit/phpunit.xml.dist" ["app/code/My/Module/Test/Unit"] Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  8. 8. $ alias unit-test="$(pwd)/vendor/bin/phpunit -c $(pwd)/dev/tests/unit/phpunit.xml.dist" Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  9. 9. $ unit-test Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  10. 10. $ unit-test "app/code/My/Module/Test/Unit" Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  11. 11. Integration Test Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  12. 12. What is the difference? Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  13. 13. Real Objects Interact Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  14. 14. Integration Tests need the Runtime Environment Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  15. 15. Integration Test Framework Bootstrap: 1. If TESTS_CLEANUP is set 4 clear test environment cache 4 uninstall Magento 2. If Magento is not installed 4 install Magento & dump the DB 4 on consecutive runs: import the dump before install Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  16. 16. Separate DB cp dev/tests/integration/etc/install-config-mysql.php.dist dev/tests/integration/etc/install-config-mysql.php vi dev/tests/integration/etc/install-config-mysql.php Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  17. 17. Tweak settings cp dev/tests/integration/phpunit.xml.dist dev/tests/integration/phpunit.xml Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  18. 18. dev/tests/integration/phpunit.xml.dist <phpunit> ... <php> ... <const name="TESTS_CLEANUP" value="enabled"/> <!--<const name="TESTS_EXTRA_VERBOSE_LOG" value="1"/>--> ... </php> ... </phpunit> Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  19. 19. TESTS_CLEANUP == disabled speeds things up Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  20. 20. TESTS_CLEANUP == disabled manual cleanup required Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  21. 21. Enable TESTS_EXTRA_VERBOSE_LOG in phpunit.xml to see what is going on during bootstrap. Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  22. 22. Every run gets it's own etc, pub and var dirs dev/tests/integration/tmp/sandbox-0- b79c13eb842cf3211459b11c775bfbde/ Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  23. 23. rm -r integration/tmp/sandbox-* fresh start! Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  24. 24. Executing Integration Tests Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  25. 25. Executing integration tests != unit tests $ vendor/bin/phpunit -c dev/tests/integration/phpunit.xml Could not read "dev/tests/integration/phpunit.xml". Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  26. 26. $ cd dev/tests/integration $ ../../../vendor/bin/phpunit Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  27. 27. Running Tests in PHPStorm Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  28. 28. 1. Set up PHP interpreter Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  29. 29. 2. Set up PHPUnit (1/2) Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  30. 30. 3. Set up PHPUnit (2/2) Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  31. 31. 4. Create run configuration Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  32. 32. 5. Run tests with one of ^ F10 ^ F9 (debug) or click: Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  33. 33. Writing Tests Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  34. 34. Writing Unit Tests Nothing Magento 2 specific Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  35. 35. Writing Integration Tests Very Magento 2 specific Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  36. 36. Magento 2 Testing Framework dev/tests/integration/framework Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  37. 37. ObjectManager is a'okay! Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  38. 38. Test Framework ObjectManager MagentoTestFrameworkObjectManager::getInstance() Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  39. 39. Fixtures Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  40. 40. Fixtures Fix the system into a known state before a test is executed. Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  41. 41. /** * @magentoDataFixture Magento/Sales/_files/order.php * @magentoDataFixture Magento/Customer/_files/customer.php * @magentoDataFixture myCustomFixture */ public function testSomethingWithAnOrderAndACustomer() { // ... } public static function myCustomFixture() { // ... Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  42. 42. Complex Fixtures: (e.g. Sessions, Order Placement, Customer...) https://github.com/tddwizard/magento2-fixtures Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  43. 43. Config Fixtures Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  44. 44. Store Scope /** * @magentoConfigFixture current_store catalog/price/scope 1 */ public function testDoesSomeoneReadThisHelpMeLetMeOut() { // ... Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  45. 45. Global Scope /** * @magentoConfigFixture currency/options/allow USD */ public function testTastesLikeMashAndPie() { // ... Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  46. 46. Test Isolation Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  47. 47. DataBase Isolation Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  48. 48. Transactions /** * @magentoDbIsolation enabled */ public function testWithSideEffects { // ... Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  49. 49. Transactions /** * @magentoDataFixture Magento/Foo/_files/bar.php */ public function testTheFixtureIsAppliedWithinATransaction { // ... Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  50. 50. Application Isolation Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  51. 51. Reset Magento after test /** * @magentoAppIsolation enabled */ public function testTestWithSingletons() { Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  52. 52. Selecting a application area /** * @magentoAppArea frontend */ public function testRendersTheCustomerNameInWelcomeMessage() { Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  53. 53. Most important! Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  54. 54. Choose the right test granularity based on 4 Business Value 4 Personal Skill 4 Available Time Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  55. 55. Don't test for testing's sake Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  56. 56. Make the tests valuable Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp
  57. 57. Time for a sip of ☕ before we dive in! Testing in Magento 2 - #MageTestFest, Nov. 2017 - contact@vinaikopp.com - twitter://@VinaiKopp

×