SlideShare a Scribd company logo
1 of 34
Download to read offline
11th of December 2014
How to write good tests?
by Alexis von Glasow
11th of December 2014
Table of contents
● Why write tests?
● What should we test?
● With what and how?
11th of December 2014
Why write tests
11th of December 2014
● Why write tests?
● What should we test?
● With what and how?
11th of December 2014
11th of December 2014
LOOSE TIME
11th of December 2014
NO BUGS
TESTS
11th of December 2014
11th of December 2014
What should we test
11th of December 2014
ISOLATED!!!
11th of December 2014
Black box
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
On ne se préoccupe que des entrée sorties.
11th of December 2014
White box
11th of December 2014
Unit Tests keep code
consistency!
11th of December 2014
With what and how
11th of December 2014
Php Tools
11th of December 2014
Assert
public function testFailure()
{
$this->assertEquals($expected, $value, ‘message’);
$this->assertFalse(FALSE);
$this->assertTrue(TRUE);
$this->assertNull(NULL);
//…
}
11th of December 2014
Data Provider
/**
* @dataProvider additionProvider
*/
public function testAdd($a, $b, $expected)
{
$this->assertEquals($expected, $a + $b);
}
public function additionProvider()
{
return array(
array(0, 0, 0), //success
array(0, 1, 1), //success
array(1, 0, 1), //success
array(1, 1, 3) //fail
);
}
11th of December 2014
Generators
● https://github.com/fzaninotto/Faker
● https://github.com/nelmio/alice
● http://hoa-project.net/En/Literature/Hack/Compiler.html
● …
11th of December 2014
Faker
// use the factory to create a FakerGenerator instance
$faker = FakerFactory::create();
// generate data by accessing properties
echo $faker->name;
// 'Lucy Cechtelar';
echo $faker->address;
// "426 Jordy Lodge
// Cartwrightshire, SC 88120-6700"
echo $faker->text;
// Sint velit eveniet. Rerum atque repellat voluptatem quia rerum. Numquam excepturi
// beatae sint laudantium consequatur. Magni occaecati itaque sint et sit tempore. Nesciunt
// amet quidem. Iusto deleniti cum autem ad quia aperiam.
// A consectetur quos aliquam. In iste aliquid et aut similique suscipit. Consequatur qui
// quaerat iste minus hic expedita. Consequuntur error magni et laboriosam. Aut aspernatur
// voluptatem sit aliquam. Dolores voluptatum est.
// Aut molestias et maxime. Fugit autem facilis quos vero. Eius quibusdam possimus est.
// Ea quaerat et quisquam. Deleniti sunt quam. Adipisci consequatur id in occaecati.
// Et sint et. Ut ducimus quod nemo ab voluptatum.
11th of December 2014
Alice
NelmioEntityUser:
user0:
username: bob
fullname: Bob
birthDate: 1980-10-10
email: bob@example.org
favoriteNumber: 42
user1:
username: alice
fullname: Alice
birthDate: 1978-07-12
email: alice@example.org
favoriteNumber: 27
NelmioEntityGroup:
group1:
name: Admins
11th of December 2014
Grammar PP
%skip space s
// Scalars.
%token true true
%token false false
%token null null
// Strings.
%token quote_ " -> string
%token string:string [^"]+
%token string:_quote " -> default
// Objects.
%token brace_ {
%token _brace }
// Arrays.
%token bracket_ [
%token _bracket ]
// Rest.
%token colon :
%token comma ,
%token number d+
11th of December 2014
Grammar PP
value:
<true> | <false> | <null> | string() | object() | array() | number()
string:
::quote_:: <string> ::_quote::
number:
<number>
#object:
::brace_:: pair() ( ::comma:: pair() )* ::_brace::
#pair:
string() ::colon:: value()
#array:
::bracket_:: value() ( ::comma:: value() )* ::_bracket::
11th of December 2014
CLASS A
CLASS C
CLASS B
Mock Me If you can
11th of December 2014
CLASS A
CLASS C
MOCK B
CLASS A
MOCK C
CLASS B
Mock Me If you can
11th of December 2014
Mock Me If you can
class A {
public function doSomething(B $b, C $c)
{
try {
$d = $b->doOther($c);
$d->doSomething();
} catch (Exception $e) {
return false;
}
return $d;
}
}
class B {
public function doOther(C $c)
{
if ($c->isTrue()) {
throw new Exception(‘Oups!’);
}
return new D;
}
}
11th of December 2014
Mock Me If you can
public function test()
{
$a = new A();
$b = new B();
$mockC = $this->getMock(‘C’, array(‘isTrue’));
$mockC->expects($this->any())->method(‘isTrue’)->will($this->returnValue(false));
$a->doSomething($b, $mockC);
}
public function testRaiseException()
{
$a = new A();
$b = new B();
$mockC = $this->getMock(‘C’, array(‘isTrue’));
$mockC->expects($this->any())->method(‘isTrue’)->will($this->returnValue(true));
$a->doSomething($b, $mockC);
}
11th of December 2014
Whenever you are tempted to
type something into a print
statement or a debugger
expression, write it as a test
instead.
-- Martin Fowler
11th of December 2014
Bug! What to do?
11th of December 2014
Test Driven Development
TDD
11th of December 2014
Summary
● A good test is an existing test
● Test with provider, mock, generators!
● Use all asserters available
● Test sooner is better, easier!
11th of December 2014
To go further
11th of December 2014
Questions ?
For online questions, please leave a comment on the article.
11th of December 2014
Join the community !
(in Paris)
Social networks :
● Follow us on Twitter : https://twitter.com/steamlearn
● Like us on Facebook : https://www.facebook.com/steamlearn
SteamLearn is an Inovia initiative : inovia.fr
You wish to be in the audience ? Join the meetup group!
http://www.meetup.com/Steam-Learn/
11th of December 2014
Sources
● https://github.com/atoum/atoum
● https://phpunit.de/
● http://www.simpletest.org/
● http://martinfowler.com/
● http://thenounproject.com/
Credits photos:
● useiconic.com from the Noun Project
● iconsmind.com from the Noun Project
● Luis Prado from the Noun Project
● Darren Barone from the Noun Project
● SuperAtic LABS from the Noun Project
● Julia Stoffer from the Noun Project
● CommitStrip.com
● Alexander Pretschner

More Related Content

Viewers also liked (7)

Steam Learn: Speedrun et TAS
Steam Learn: Speedrun et TASSteam Learn: Speedrun et TAS
Steam Learn: Speedrun et TAS
 
Steam Learn: Zen SQL
Steam Learn: Zen SQLSteam Learn: Zen SQL
Steam Learn: Zen SQL
 
Steam Learn: Composer
Steam Learn: ComposerSteam Learn: Composer
Steam Learn: Composer
 
2015.02.05 alexis von glasow - faster php testing process with atoum
2015.02.05   alexis von glasow - faster php testing process with atoum2015.02.05   alexis von glasow - faster php testing process with atoum
2015.02.05 alexis von glasow - faster php testing process with atoum
 
Steam Learn: Email deliverability
Steam Learn: Email deliverabilitySteam Learn: Email deliverability
Steam Learn: Email deliverability
 
Steam Learn: An introduction to Redis
Steam Learn: An introduction to RedisSteam Learn: An introduction to Redis
Steam Learn: An introduction to Redis
 
Steam Learn: Javascript and OOP
Steam Learn: Javascript and OOPSteam Learn: Javascript and OOP
Steam Learn: Javascript and OOP
 

Similar to Steam Learn: How to write good tests

Code Fast, Die Young, Throw Structured Exceptions
Code Fast, Die Young, Throw Structured ExceptionsCode Fast, Die Young, Throw Structured Exceptions
Code Fast, Die Young, Throw Structured Exceptions
John Anderson
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
Writing Maintainable Perl
Writing Maintainable PerlWriting Maintainable Perl
Writing Maintainable Perl
tinypigdotcom
 
Why Perl, when you can use bash+awk+sed? :P
Why Perl, when you can use bash+awk+sed? :PWhy Perl, when you can use bash+awk+sed? :P
Why Perl, when you can use bash+awk+sed? :P
Luciano Rocha
 

Similar to Steam Learn: How to write good tests (20)

Modern Perl
Modern PerlModern Perl
Modern Perl
 
Code Fast, die() Early, Throw Structured Exceptions
Code Fast, die() Early, Throw Structured ExceptionsCode Fast, die() Early, Throw Structured Exceptions
Code Fast, die() Early, Throw Structured Exceptions
 
Code Fast, Die Young, Throw Structured Exceptions
Code Fast, Die Young, Throw Structured ExceptionsCode Fast, Die Young, Throw Structured Exceptions
Code Fast, Die Young, Throw Structured Exceptions
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?
 
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
 
Dumping Perl 6 (French Perl Workshop)
Dumping Perl 6 (French Perl Workshop)Dumping Perl 6 (French Perl Workshop)
Dumping Perl 6 (French Perl Workshop)
 
Value objects
Value objectsValue objects
Value objects
 
Top 10 php classic traps confoo
Top 10 php classic traps confooTop 10 php classic traps confoo
Top 10 php classic traps confoo
 
PHP Tips & Tricks
PHP Tips & TricksPHP Tips & Tricks
PHP Tips & Tricks
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Dealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottDealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter Scott
 
Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?
 
groovy & grails - lecture 2
groovy & grails - lecture 2groovy & grails - lecture 2
groovy & grails - lecture 2
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
Top 10 php classic traps DPC 2020
Top 10 php classic traps DPC 2020Top 10 php classic traps DPC 2020
Top 10 php classic traps DPC 2020
 
Writing Maintainable Perl
Writing Maintainable PerlWriting Maintainable Perl
Writing Maintainable Perl
 
Why Perl, when you can use bash+awk+sed? :P
Why Perl, when you can use bash+awk+sed? :PWhy Perl, when you can use bash+awk+sed? :P
Why Perl, when you can use bash+awk+sed? :P
 
Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium
 

More from inovia

More from inovia (18)

10 tips for Redux at scale
10 tips for Redux at scale10 tips for Redux at scale
10 tips for Redux at scale
 
10 essentials steps for kafka streaming services
10 essentials steps for kafka streaming services10 essentials steps for kafka streaming services
10 essentials steps for kafka streaming services
 
Redux at scale
Redux at scaleRedux at scale
Redux at scale
 
DocuSign's Road to react
DocuSign's Road to reactDocuSign's Road to react
DocuSign's Road to react
 
API Gateway: Nginx way
API Gateway: Nginx wayAPI Gateway: Nginx way
API Gateway: Nginx way
 
Kafka: meetup microservice
Kafka: meetup microserviceKafka: meetup microservice
Kafka: meetup microservice
 
Microservice: starting point
Microservice:  starting pointMicroservice:  starting point
Microservice: starting point
 
Correlation id (tid)
Correlation id (tid)Correlation id (tid)
Correlation id (tid)
 
Meetic back end redesign - Meetup microservices
Meetic back end redesign - Meetup microservicesMeetic back end redesign - Meetup microservices
Meetic back end redesign - Meetup microservices
 
Security in microservices architectures
Security in microservices architecturesSecurity in microservices architectures
Security in microservices architectures
 
Building a Secure, Performant Network Fabric for Microservice Applications
Building a Secure, Performant Network Fabric for Microservice ApplicationsBuilding a Secure, Performant Network Fabric for Microservice Applications
Building a Secure, Performant Network Fabric for Microservice Applications
 
Microservices vs SOA
Microservices vs SOAMicroservices vs SOA
Microservices vs SOA
 
CQRS, an introduction by JC Bohin
CQRS, an introduction by JC BohinCQRS, an introduction by JC Bohin
CQRS, an introduction by JC Bohin
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Oauth2, open-id connect with microservices
Oauth2, open-id connect with microservicesOauth2, open-id connect with microservices
Oauth2, open-id connect with microservices
 
You probably don't need microservices
You probably don't need microservicesYou probably don't need microservices
You probably don't need microservices
 
Api Gateway - What's the use of an api gateway?
Api Gateway - What's the use of an api gateway?Api Gateway - What's the use of an api gateway?
Api Gateway - What's the use of an api gateway?
 
Steam Learn: Asynchronous Javascript
Steam Learn: Asynchronous JavascriptSteam Learn: Asynchronous Javascript
Steam Learn: Asynchronous Javascript
 

Steam Learn: How to write good tests

  • 1. 11th of December 2014 How to write good tests? by Alexis von Glasow
  • 2. 11th of December 2014 Table of contents ● Why write tests? ● What should we test? ● With what and how?
  • 3. 11th of December 2014 Why write tests
  • 4. 11th of December 2014 ● Why write tests? ● What should we test? ● With what and how?
  • 6. 11th of December 2014 LOOSE TIME
  • 7. 11th of December 2014 NO BUGS TESTS
  • 9. 11th of December 2014 What should we test
  • 10. 11th of December 2014 ISOLATED!!!
  • 11. 11th of December 2014 Black box On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties. On ne se préoccupe que des entrée sorties.
  • 12. 11th of December 2014 White box
  • 13. 11th of December 2014 Unit Tests keep code consistency!
  • 14. 11th of December 2014 With what and how
  • 15. 11th of December 2014 Php Tools
  • 16. 11th of December 2014 Assert public function testFailure() { $this->assertEquals($expected, $value, ‘message’); $this->assertFalse(FALSE); $this->assertTrue(TRUE); $this->assertNull(NULL); //… }
  • 17. 11th of December 2014 Data Provider /** * @dataProvider additionProvider */ public function testAdd($a, $b, $expected) { $this->assertEquals($expected, $a + $b); } public function additionProvider() { return array( array(0, 0, 0), //success array(0, 1, 1), //success array(1, 0, 1), //success array(1, 1, 3) //fail ); }
  • 18. 11th of December 2014 Generators ● https://github.com/fzaninotto/Faker ● https://github.com/nelmio/alice ● http://hoa-project.net/En/Literature/Hack/Compiler.html ● …
  • 19. 11th of December 2014 Faker // use the factory to create a FakerGenerator instance $faker = FakerFactory::create(); // generate data by accessing properties echo $faker->name; // 'Lucy Cechtelar'; echo $faker->address; // "426 Jordy Lodge // Cartwrightshire, SC 88120-6700" echo $faker->text; // Sint velit eveniet. Rerum atque repellat voluptatem quia rerum. Numquam excepturi // beatae sint laudantium consequatur. Magni occaecati itaque sint et sit tempore. Nesciunt // amet quidem. Iusto deleniti cum autem ad quia aperiam. // A consectetur quos aliquam. In iste aliquid et aut similique suscipit. Consequatur qui // quaerat iste minus hic expedita. Consequuntur error magni et laboriosam. Aut aspernatur // voluptatem sit aliquam. Dolores voluptatum est. // Aut molestias et maxime. Fugit autem facilis quos vero. Eius quibusdam possimus est. // Ea quaerat et quisquam. Deleniti sunt quam. Adipisci consequatur id in occaecati. // Et sint et. Ut ducimus quod nemo ab voluptatum.
  • 20. 11th of December 2014 Alice NelmioEntityUser: user0: username: bob fullname: Bob birthDate: 1980-10-10 email: bob@example.org favoriteNumber: 42 user1: username: alice fullname: Alice birthDate: 1978-07-12 email: alice@example.org favoriteNumber: 27 NelmioEntityGroup: group1: name: Admins
  • 21. 11th of December 2014 Grammar PP %skip space s // Scalars. %token true true %token false false %token null null // Strings. %token quote_ " -> string %token string:string [^"]+ %token string:_quote " -> default // Objects. %token brace_ { %token _brace } // Arrays. %token bracket_ [ %token _bracket ] // Rest. %token colon : %token comma , %token number d+
  • 22. 11th of December 2014 Grammar PP value: <true> | <false> | <null> | string() | object() | array() | number() string: ::quote_:: <string> ::_quote:: number: <number> #object: ::brace_:: pair() ( ::comma:: pair() )* ::_brace:: #pair: string() ::colon:: value() #array: ::bracket_:: value() ( ::comma:: value() )* ::_bracket::
  • 23. 11th of December 2014 CLASS A CLASS C CLASS B Mock Me If you can
  • 24. 11th of December 2014 CLASS A CLASS C MOCK B CLASS A MOCK C CLASS B Mock Me If you can
  • 25. 11th of December 2014 Mock Me If you can class A { public function doSomething(B $b, C $c) { try { $d = $b->doOther($c); $d->doSomething(); } catch (Exception $e) { return false; } return $d; } } class B { public function doOther(C $c) { if ($c->isTrue()) { throw new Exception(‘Oups!’); } return new D; } }
  • 26. 11th of December 2014 Mock Me If you can public function test() { $a = new A(); $b = new B(); $mockC = $this->getMock(‘C’, array(‘isTrue’)); $mockC->expects($this->any())->method(‘isTrue’)->will($this->returnValue(false)); $a->doSomething($b, $mockC); } public function testRaiseException() { $a = new A(); $b = new B(); $mockC = $this->getMock(‘C’, array(‘isTrue’)); $mockC->expects($this->any())->method(‘isTrue’)->will($this->returnValue(true)); $a->doSomething($b, $mockC); }
  • 27. 11th of December 2014 Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead. -- Martin Fowler
  • 28. 11th of December 2014 Bug! What to do?
  • 29. 11th of December 2014 Test Driven Development TDD
  • 30. 11th of December 2014 Summary ● A good test is an existing test ● Test with provider, mock, generators! ● Use all asserters available ● Test sooner is better, easier!
  • 31. 11th of December 2014 To go further
  • 32. 11th of December 2014 Questions ? For online questions, please leave a comment on the article.
  • 33. 11th of December 2014 Join the community ! (in Paris) Social networks : ● Follow us on Twitter : https://twitter.com/steamlearn ● Like us on Facebook : https://www.facebook.com/steamlearn SteamLearn is an Inovia initiative : inovia.fr You wish to be in the audience ? Join the meetup group! http://www.meetup.com/Steam-Learn/
  • 34. 11th of December 2014 Sources ● https://github.com/atoum/atoum ● https://phpunit.de/ ● http://www.simpletest.org/ ● http://martinfowler.com/ ● http://thenounproject.com/ Credits photos: ● useiconic.com from the Noun Project ● iconsmind.com from the Noun Project ● Luis Prado from the Noun Project ● Darren Barone from the Noun Project ● SuperAtic LABS from the Noun Project ● Julia Stoffer from the Noun Project ● CommitStrip.com ● Alexander Pretschner