27
Test Driven Development & Automation (PHP) “All code is guilty until proven innocence”

Test Driven Development and Automation

Embed Size (px)

DESCRIPTION

"All code is guilty until proven innocence”

Citation preview

Page 1: Test Driven Development and Automation

Test Driven Development &

Automation (PHP)“All code is guilty until proven innocence”

Page 2: Test Driven Development and Automation

Things to cover

• What is TDD?• Why should we do TDD?• Where should we run Tests to make sure everything is

working fine?• Who should adopt TDD?• Why Unit Testing, Code coverage, code sniffer and Selenium

are important?• Tools for PHP Test Driven Development.• How we automated tests with every build generation?• Quick Demo and QA.

Page 3: Test Driven Development and Automation

What is TDD?

• TDD is a programming technique that requires you to write actual code and automated test code simultaneously. This ensures that you test your code—and enables you to retest your code quickly and easily, since it’s automated.

• All code is guilty until proven innocence

Page 4: Test Driven Development and Automation

TDD

• TDD is an agile development technique practice which combines Refactoring and Test-First Development.

• Beck's concept of test-driven development centers on two basic rules: - Never write a single line of code unless you have a failing automated test. - Eliminate duplication.

Page 5: Test Driven Development and Automation

Why we should do TDD?

“I don’t want to test because:”•As a Developer I feel lazy to test everything again and again. Oops!•I wanted to release my product as soon as possible to make it available to stake holders.•I feel it’s a waste of time for me.•I prefer manual testing (Black Box Testing), just to save myself.

Page 6: Test Driven Development and Automation

Why we should do TDD…• TDD allows us to make changes and test quickly

and efficiently.• To release the product as soon as possible to

make it available to stake holders without bugs.• To Reduce Product release life cycle.• To make sure product is working as expected

without any bugs that we are aware of.• To make sure that end customers are getting

good quality and a mature product.• To avoid silly mistakes.

Page 7: Test Driven Development and Automation

Where should we run Tests to make sure everything is working fine?

• Each developer should setup an environment to make sure that his code gets tested with every compilation or he can test cases anytime.

• Integrate Unit Testing, Code Coverage and Code Sniffer with IDE to make sure your code is innocent and well written.

Page 8: Test Driven Development and Automation

Make it automated on server side.

• Setup test server to do all the demo installation and testing stuff.

• Package your code.• Create Self installation script.• Run installation with Unit Test Cases.• Check Code coverage and Sniff your code.• Run PHP Selenium test cases to test your UI.• Generate Public Build if your code passes all the tests.• If test cases fail then do not generate build and raise

RED Signal to developers.

Page 9: Test Driven Development and Automation

Who should adopt to TDD?

• Every one, Either it’s a product development company or Services based company.

“Management support is essential. Without the entire organization believing that test-driven development is going to improve the product, management may

feel that time spent writing tests is wasted.”

• Set Strict code and testing standards if you really want painless deployment.

Page 10: Test Driven Development and Automation

Why Unit Testing, Code coverage, code sniffer and Selenium are important?

• Unit Tests check Code level test cases for Classes and functions with defined assertions.

• Code Coverage ensures that you covered whole code in test cases.

• Code Sniffer makes sure that you follow strict coding standards.

• Selenium Tests make sure that your UI is working as expected.

Above things ensures that you are going to deliver 100% working and a Quality Product.

Page 11: Test Driven Development and Automation

Tools for PHP Test Driven Development.

• Xdebug (Code Coverage)• PHPUnit• Selenium (PHPUnit_Selenium)• PHP_CodeSniffer• PHPUnderControl• IDEs with integrated TDD:– NetBeans– ZendStudio– PHPStorm

Page 12: Test Driven Development and Automation

How to write test cases?

• Fail To WIN “First fail the test cases. The idea is to ensure that the test

really works and can catch an error. Once this is shown, the underlying functionality can be implemented. This has been

coined the "test-driven development mantra", known as red/green/refactor where red means fail and green is pass.”

• Answer your test by writing code.• Refactor and Refine your code.• Rinse and Repeat.Source: http://net.tutsplus.com/

Page 13: Test Driven Development and Automation

Why we needed TDD for our Organization

• Big Question on Big Code Base.– More than 1500 PHP Files.– More than 5 Lac lines of code.– More than 15 Developers working on same codebase with their

different coding styles.– More than 300 integrated features.– Multiple channels of data communication (XML based Chat, VoIP,

Emails, Database queries, REST APIs, Session Based APIs)

“Everything is Hard to test on every release cycle. We always miss and our customers come back to us which makes us feel bad.”

• We are still struggling to convince everyone to write test cases and make our builds based on 100% TDD.

Page 14: Test Driven Development and Automation

How we automated tests with every build generation?

• Build System Integration with GitHub.• Unit Test Server with:

– Auto Product Installation.– PHPUnit for all test cases.– Code Coverage check in IDE only (Not Strict on this yet)– Code Quality Check using Code Sniffer (Less errors due to good IDE and same coding

templates and standards)– Next Step we are planning to add Selenium (Already done with test deployments)– Then we deploy on our own live installation and tell our team to keep eyes on any error or

exceptions.

If everything goes perfectly fine then release builds. Else send error messages in Internal Jabber bases IM clients and via email.

It’s a complete test eco system to make sure we provide a quality product.

This is how we deploy our product to more than 30k customers including automated SaaS upgrade system.

Page 15: Test Driven Development and Automation

Code to fail

Page 16: Test Driven Development and Automation

Failure Output in NetBeans

Page 17: Test Driven Development and Automation

Correct Code

Page 18: Test Driven Development and Automation

Success Output with Code Coverage

Page 19: Test Driven Development and Automation

Unreadable code with silly mistakes

Page 20: Test Driven Development and Automation

Well written code

Page 21: Test Driven Development and Automation

Code Sniffer

Page 22: Test Driven Development and Automation

Code Sniffer…

Page 23: Test Driven Development and Automation

Our Final Build Status

Page 24: Test Driven Development and Automation

Our Final Build Logs

Page 25: Test Driven Development and Automation

Insane programmer who do not tests code.

Source: http://www.kavistechnology.com/

Page 26: Test Driven Development and Automation

Super-Duper Programmer who tests his code

Page 27: Test Driven Development and Automation

Mahesh [email protected]

http://twitter.com/salaria

Thank You