Getting your hands dirty testing Magento 2 (at MageTitansIT)
5. Feb 2016•0 gefällt mir
10 gefällt mir
Sei der Erste, dem dies gefällt
Mehr anzeigen
•8,512 Aufrufe
Aufrufe
Aufrufe insgesamt
0
Auf Slideshare
0
Aus Einbettungen
0
Anzahl der Einbettungen
0
Downloaden Sie, um offline zu lesen
Melden
Software
Introduction into automated testing in Magento 2 with a focus on integration tests.
The presentation was held at the MageTitans conference in Milano, Italy on 2016-02-05.
I re-uploaded a newer version of the slide deck that contains more details.
WHAT IS THE
DIFFERENCE?
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
REAL OBJECTS INTERACT
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
INTEGRATION TESTS NEED THE
RUNTIME ENVIRONMENT
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
A failing unit test tells me
exactly where my code is broken.
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
A failing integration test only
tells me that something is broken.
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
$ vendor/bin/phpunit -c dev/tests/integration/phpunit.xml
Could not read "dev/tests/integration/phpunit.xml".
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
$ cd dev/tests/integration
$ ../../../vendor/bin/phpunit
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
Every run gets it's own
etc, pub and var dirs
dev/tests/integration/tmp/
sandbox-0-b79c13eb842cf3211459b11c775bfbde/
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
WHAT IS TDD?
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
THE 3 RULES
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
I. No production code without
a failing unit test
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
II. Once there is a failing unit test,
stop and make it pass
by writing production code
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
III. Once the test passes,
stop writing production code
until you have a failing unit test again
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
TDD FOR MAGENTO 2
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
NO DIFFERENCE
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
</TDD>
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
To learn more about TDD,
check out the Clean Coders
Videos from Robert C. Martin
http://cleancoders.com
(or do a training)
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
INTEGRATION TESTS
FOR MAGENTO 2
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
VERY SPECIFIC
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
BUZZWORD IV
FIXTURES
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
Fix the system into a known state
before a test is executed.
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
Simple PHP Scripts
Relative to
dev/tests/integration/testsuite
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
/**
* @magentoDataFixture Magento/Sales/_files/order.php
* @magentoDataFixture Magento/Customer/_files/customer.php
*/
public function testSomethingWithAnOrderAndACustomer()
{
// ...
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
Store Scope
/**
* @magentoConfigFixture current_store catalog/price/scope 1
*/
public function testDoesSomeoneReadThisHelpMeLetMeOut()
{
// ...
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
Global Scope
/**
* @magentoConfigFixture currency/options/allow USD
*/
public function testHowMashAndPieTastes()
{
// ...
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
BUZZWORD V
TEST ISOLATION
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
FRESH INSTALL
FOR EACH TEST RUN
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
II. TESTING THE OBSERVER
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
O_0(NOTHING TO TEST)
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
III. INTEGRATION TEST
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
Our observer has no side effects.
We have to improvise...
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
/**
* @magentoDataFixture Magento/Sales/_files/order.php
*/
public function testSalesOrderPlaceAfterObserverIsCalled()
{
$this->injectMockObserverWithExpectation();
$order = $this->getOrderFixture();
$this->objectManager
->create(OrderManagementInterface::class)
->place($order);
}
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
public function injectMockObserverWithExpectation()
{
$mockObserver = $this->getMock(SalesOrderPlaceAfter::class);
$mockObserver->expects($this->once())->method('execute');
$this->registerObserverTestDouble($mockObserver);
}
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp