steam learn: how to write good tests

34
11th of December 2014 How to write good tests? by Alexis von Glasow

Upload: inovia

Post on 18-Jul-2015

54 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Steam Learn: How to write good tests

11th of December 2014

How to write good tests?by Alexis von Glasow

Page 2: Steam Learn: How to write good tests

11th of December 2014

Table of contents

● Why write tests?● What should we test?● With what and how?

Page 3: Steam Learn: How to write good tests

11th of December 2014

Why write tests

Page 4: Steam Learn: How to write good tests

11th of December 2014

● Why write tests?● What should we test?● With what and how?

Page 5: Steam Learn: How to write good tests

11th of December 2014

Page 6: Steam Learn: How to write good tests

11th of December 2014

LOOSE TIME

Page 7: Steam Learn: How to write good tests

11th of December 2014

NO BUGS

TESTS

Page 8: Steam Learn: How to write good tests

11th of December 2014

Page 9: Steam Learn: How to write good tests

11th of December 2014

What should we test

Page 10: Steam Learn: How to write good tests

11th of December 2014

ISOLATED!!!

Page 11: Steam Learn: How to write good tests

11th of December 2014

Black boxOn 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.

Page 12: Steam Learn: How to write good tests

11th of December 2014

White box

Page 13: Steam Learn: How to write good tests

11th of December 2014

Unit Tests keep code consistency!

Page 14: Steam Learn: How to write good tests

11th of December 2014

With what and how

Page 15: Steam Learn: How to write good tests

11th of December 2014

Php Tools

Page 16: Steam Learn: How to write good tests

11th of December 2014

Assert

public function testFailure()

{

$this->assertEquals($expected, $value, ‘message’);

$this->assertFalse(FALSE);

$this->assertTrue(TRUE);

$this->assertNull(NULL);

//…

}

Page 17: Steam Learn: How to write good tests

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

);

}

Page 18: Steam Learn: How to write good tests

11th of December 2014

Generators

● https://github.com/fzaninotto/Faker● https://github.com/nelmio/alice● http://hoa-project.net/En/Literature/Hack/Compiler.html● …

Page 19: Steam Learn: How to write good tests

11th of December 2014

Faker// use the factory to create a Faker\Generator instance

$faker = Faker\Factory::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.

Page 20: Steam Learn: How to write good tests

11th of December 2014

AliceNelmio\Entity\User:

user0:

username: bob

fullname: Bob

birthDate: 1980-10-10

email: [email protected]

favoriteNumber: 42

user1:

username: alice

fullname: Alice

birthDate: 1978-07-12

email: [email protected]

favoriteNumber: 27

Nelmio\Entity\Group:

group1:

name: Admins

Page 21: Steam Learn: How to write good tests

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+

Page 22: Steam Learn: How to write good tests

11th of December 2014

Grammar PPvalue: <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::

Page 23: Steam Learn: How to write good tests

11th of December 2014

CLASS A

CLASS C

CLASS B

Mock Me If you can

Page 24: Steam Learn: How to write good tests

11th of December 2014

CLASS A

CLASS C

MOCK B

CLASS A

MOCK C

CLASS B

Mock Me If you can

Page 25: Steam Learn: How to write good tests

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;}

}

Page 26: Steam Learn: How to write good tests

11th of December 2014

Mock Me If you canpublic 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);}

Page 27: Steam Learn: How to write good tests

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

Page 28: Steam Learn: How to write good tests

11th of December 2014

Bug! What to do?

Page 29: Steam Learn: How to write good tests

11th of December 2014

Test Driven Development

TDD

Page 30: Steam Learn: How to write good tests

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!

Page 31: Steam Learn: How to write good tests

11th of December 2014

To go further

Page 32: Steam Learn: How to write good tests

11th of December 2014

Questions ?For online questions, please leave a comment on the article.

Page 33: Steam Learn: How to write good tests

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/

Page 34: Steam Learn: How to write good tests

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