Automated tests with PHPunit is a puzzle. – Continuous integration is the missing piece



Automated tests with PHPunit is a puzzle. – Continuous integration is the missing piece

0 0


kswsbs


On Github sachit-singh / kswsbs

Automated tests with PHPunit is a puzzle.

Continuous integration is the missing piece

sachit

youngInnovations

Automated Unit Testing

the "What"

- just code testing another code

-- means using an automation tool to execute your test suite

--- a process using software tools to execute pre-scripted tests on an application before releasing into production

the "Why"

- manual testing of all work flows, all scenarios and everything else is time and cost consuming

-- doesn't require human intervention

--- increases speed and accuracy of test execution

---- a lot of code coverage

Types of tests - mainly

Unit Tests

Functional Tests

Integration Tests

Unit Tests

testing a small independent part of code

database independent

minimal or no file system dependence

isolated

deterministic

so it is fast

Why unit tests ?

find problems early in the dev cycle

they facilitate change

they are good documentations

its fun..

Martin Fowler's Test Pyramid

source http://martinfowler.com

how do we write unit tests?

setup everything for testing

mock dependencies

??

mockery

php mock object framework for mocking dependencies

why mockery?

can mock static methods, unlike Prophecy

integrates easily with PHPUnit

how do we write unit tests?

setup everything for testing

mock dependencies

trigger the method being tested

verify the results, that they are correct

class ExampleClass extends someClass
{
    protected $person;

    public function __construct(Person $person)
    {
        $this->person = $person;
    }

    public function create(array $details)
    {
        return $this->person->create($details);
    }
}
class exampleTest extends someTestCase
{
    protected $person;
    public function setup()
    {
        parent::setUp();
        $this->person = \Mockery::mock('\Person');
        $this->exampleClass = new ExampleClass($this->person);
    }

    public function tearDown()
    {
        parent::tearDown();
        m::close();
    }
}
public function testItShouldCreateAPerson()
{
    $this->person
              ->shouldReceive('create')
              ->once()
              ->with(['name' => 'HisName', 'age' => 30])
              ->andReturnSelf();

    $this->assertInstanceOf('\Person',
          $this->exampleClass->create(
              ['name' => 'HisName', 'age' => 30])
          );
}

public function tearDown()

how do we write

good

unit tests..??

structure is important

consistent naming

mock everything you need

and write code that can be mocked

testable code

readable tests

test and verify only one specific part (single unit of work)

self sufficiency

lot of testing frameworks available....

like..

phpunit

phpspec

codeception

simpletest

phpunit is the most popular and the de facto unit testing framework in PHP

source Google

green is good

red is not

the missing piece

Continuous Integration

a development practice that requires developers to integrate code several times a day

allowing teams to detect problems early

each push is verified by an automated build

the goal is to provide rapid feedback incase a defect is introduced

CI is important because....

ensures on a continuous basis that you have a working product

help detect bugs very early

lot of services

Why shippable?

it's free for private repo

supports docker

has unlimited builds

a lot of other cool features

What does it do?

pulls changes from your repo and runs a build every time you push anything

notify you of the success/failure of the build

you can even set it to notify you on hipchat

to do it

shippable.yml

- set up a couple of configurations

- choose what to do and when

your push turns out red...

you go and buy titaura

culture

source http://geshan.com.np

encourage

explore

...

thank

specially geshan dai

help

incitement

thank you slides @yipl blog

Automated tests with PHPunit is a puzzle. Continuous integration is the missing piece sachit youngInnovations