Test-Driven Development is een kernwaarde van TYPO3 Flow en Neos development - zie hoe, en waarom, we dit doen. De complexiteit van frameworks zoals TYPO3 Flow en applicaties als TYPO3 Neos is een goede test coverage essentieel, of de kwaliteit en ontwikkeling zal langzaam stil komen te liggen.
Deze sessie laat zien hoe we unit en functionele tests maken voor PHP en JavaScript code. En aangezien het maken van tests zonder het uitvoeren niet veel helpt zal ook Continues integrations (CI) zijdelings genoemd worden.
Rens Admiraal
Rens Admiraal is een freelance webdeveloper / coach en Senior Software Architect bij Beech Applications uit Venlo.
Rond zijn 14e is Rens begonnen met 'programmeren', toen nog door het analizeren van code snippets op prehistorische computers. Door telkens opnieuw nieuwe uitdagingen aan te gaan heeft Rens meerdere talen geleerd, en heeft hij meegewerkt aan een breed scala aan web projecten. Nu is Rens TYPO3 Flow / TYPO3 Neos core lid en werkt hier vooral aan de Javascript kant van TYPO3 Neos.
Bij verschillende bedrijven heeft Rens gezien hoe ontwikkeling in verschillende mates van structuur gedaan kan worden. Langzaamaan heeft dit geleid tot een grote interesse voor ontwikkel structuur, continues integration, automatisch testen en code kwaliteit. Nu helpt Rens bij het opzetten van ontwikkelomgevingen en het trainen van ontwikkelaars om in zo'n omgeving te werken.
Rens woont in Ede met zijn vrouw en 3 zoons, speelt basgitaar en houdt ervan om af en toe wat druk van de ketel te halen op een (beach)volleybalveld.
2. Rens Admiraal
Flow / Neos core developer
@renst3
rens.admiraal@typo3.org
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
3. Demo package
git clone --recursive git://github.com/radmiraal/Flow-and-Neos-Testing.git
cd Flow-and-Neos-Testing
curl -s https://getcomposer.org/installer | php
php composer.phar install --dev
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
4. Bekendheid met testen
100% coverage en CI!
in control, goede coverage
starten goed, houden niet bij
te weinig, helaas...
... tests?
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
5. Crap in === crap out
Ontwerp
Developers
Klant Eindproduct
Tools
...
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
6. Team Profiel - De enthousiaste developer
Yeah, nieuw
project!
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
7. Team Profiel - De enthousiaste developer
Lekker vaartje!
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
8. Team Profiel - De enthousiaste developer
Eeeh, waar ben ik?
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
9. Team Profiel - De enthousiaste developer
Ooeeeeeei!
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
10. Team Profiel - De enthousiaste developer
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
11. Team Profiel - De bouwer
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
12. Team Profiel - De bouwer
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
13. Team Profiel - De bouwer
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
14. Team Profiel - De bouwer
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
15. Klant - De vrek
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
16. Klant - De vrek
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
17. Test Driven Development?
Schrijf eerst test === denk eerst
Snelste weg naar slagende test
Refactor later
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
18. Test Driven Development Quotes
“Cease dependence on mass inspection to achieve quality.
Improve the process and build quality into the product in
the first place.”
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
19. PHP Code Testen
Flow testing framework gebruikt PHPUnit
BaseTest class biedt helpers
Configuratie standaard aanwezig
Unit & Functionele tests
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
20. Tests Uitvoeren
Unit tests:
phpunit -c Build/buildessentials/PhpUnit/UnitTests.xml
Functionele tests:
phpunit -c Build/buildessentials/PhpUnit/FunctionalTests.xml
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
21. Unit / Functional tests
Een unit test...
test een class buiten zijn context
test kleine ‘units’ code
Een functionele test...
test de class in zijn context
tests dekken het volledige systeem
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
22. UnitTestCase Helpers
Mock objecten
getAccessibleMock()
getAccessibleMockForAbstractClass()
Dependency Injection
inject()
Protected / private
_set()
_call()
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
23. Dependency Injection
Gebruik injection method indien mogelijk
$mockFoo->injectBar($mockBar);
inject()
$this->inject($mockFoo, ‘bar’, $mockBar);
AccessibleMock
$mockFoo->_set(‘bar’, $mockBar);
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
24. FunctionalTestCase
$testableSecurityEnabled
$testableHttpEnabled
$router
$testablePersistenceEnabled
$browser
$objectManager
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
25. Persistence Test
/**
* @test
*/
public function aBlogCanBeCreatedAndPersisted() {
$blog = new TYPO3T3CONTestDomainModelBlog();
$blog->setTitle('foo');
$this->blogRepository->add($blog);
$this->persistenceManager->persistAll();
$this->persistenceManager->clearState();
$this->assertEquals(1, $this->blogRepository->countAll());
}
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
26. HTTP Test
/**
* @test
*/
public function getControllerObjectNameIsEmptyIfNoRouteMatchesCurrentRequest() {
$this->router = new TYPO3FlowMvcRoutingRouter();
$request = TYPO3FlowHttpRequest::create(
new TYPO3FlowHttpUri('http://localhost')
);
$actionRequest = $this->router->route($request);
$this->assertEquals('', $actionRequest->getControllerObjectName());
}
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
27. Security Test
/**
* @test
*/
public function administratorsAreAllowedToSeeHiddenRestrictableEntities() {
$this->authenticateRoles(array('Administrator'));
$defaultEntity = new FixturesRestrictableEntity('default');
$hiddenEntity = new FixturesRestrictableEntity('hiddenEntity');
$hiddenEntity->setHidden(TRUE);
// ...
$result = $this->restrictableEntityRepository->findAll();
$this->assertTrue(count($result) === 2);
$this->assertNotNull($this->persistenceManager
->getObjectByIdentifier($defaultEntityIdentifier,
'TYPO3T3CONTestTestsFunctionalSecurityFixturesRestrictableEntity'));
$this->assertNotNull($this->persistenceManager
->getObjectByIdentifier($hiddenEntityIdentifier,
'TYPO3T3CONTestTestsFunctionalSecurityFixturesRestrictableEntity'));
$this->restrictableEntityRepository->removeAll();
$this->persistenceManager->persistAll();
$this->persistenceManager->clearState();
}
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
28. “Browser” Test
/**
* @test
*/
public function formIsRedisplayedIfValidationErrorsOccur() {
$this->browser->request('http://localhost/test/fluid/formobjects’);
$form = $this->browser->getForm();
$form['post']['email']->setValue('test_noValidEmail');
$this->browser->submit($form);
$form = $this->browser->getForm();
$this->assertSame('test_noValidEmail', $form['post']['email']->getValue());
$this->assertSame('f3-form-error', $this->browser->getCrawler()
->filterXPath('//*[@id="email"]')->attr('class'));
$form['post']['email']->setValue('another@email.org');
$response = $this->browser->submit($form);
$this->assertSame('Egon Olsen|another@email.org', $response->getContent());
}
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
29. Test runner demo movie
TODO
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
30. Test Driven Development Quotes
“During writing my tests, I recognize most of the
flaws in my initial design.”
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
31. JavaScript Testen
...
QUnit
Jasmine
JSTestDriver ...
JsUnit ...
...
YUI Test
...
Behat
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
32. Vereisten
Compatible met Flow folder structuur
Compatible met requirejs
Headless testrunner
XML rapporten
‘Multi browser’ tests
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
33. Buster.js
Browser attaching
Resource publishing
AMD Support
Meerdere ‘reporters’
Headless testing (Phantomjs)
Nog beta, wel serieuze activiteit
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
35. Test Syntax
Testcases
assertions
expectations
BDD
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
36. Meerdere browsers tegelijkertijd testen
Successfully connected Phantomjs
PhantomJS 1.7.0, OS X: ........
Opera 12.02, OS X: ........
Safari 6.0.1, OS X: ........
Firefox 15.0.1, OS X: ........
12 test cases, 32 tests, 140 assertions, 0 failures, 0 errors, 0 timeouts
Finished in 0.047s
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
37. Test runner demo movie
TODO
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
38. Test Driven Development Quotes
“Mijn bug reproduceren met een test? Kan dat? Maar... dan
kan ik dus regressions voorkomen!”
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
39. Continues Integration
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
40. Tests uitvoeren met Jenkins
Gerrit trigger op patches
Unit tests (evt. functioneel)
Resultaat post op IRC
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12
41. presentationShouldBePrepared()
presentationShouldBeGiven()
questionsShouldBeAnswered()
Inspiring people to
http://tinyurl.com/9j4uhj3 share
zondag 4 november 12