test driven development using qunit

19
Test Driven Development (TDD) using QUnit By Satej Kumar Sahu Mindfire Solutions

Upload: satejsahu

Post on 23-Feb-2017

76 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Test Driven Development using QUnit

Test Driven Development (TDD)

using QUnitBy Satej Kumar Sahu

Mindfire Solutions

Page 2: Test Driven Development using QUnit

Contents• What is TDD?

• A look into BDD

• Real life example

• TDD concepts

• What is QUnit?

• QUnit API sample use cases with examples

• TDD using unit tests with QUnit

• Conclusion

Page 3: Test Driven Development using QUnit

Simple Testing• What is Testing?

• Who does it? Is it solely confined as a role of QA?

• Sometimes gives you a feeling of being Sherlock Holmes (The famous detective you know) in a mystery.

• Some examples of simple testing may include:- Manual browser testing- Echo, exit testing- Debug tools like xDebug, IDE integrated debugging, breakpoints etc.

• Pros:- Simple, easy to debug methods and tools.- Sometimes and in some ways are quick in showing results.

• Cons:- Does not cover every aspect.- Time taking and risk prone.- Monotonous and repetitive.- In long term, not productive.

Page 4: Test Driven Development using QUnit

What is TDD?• Test Driven Development

• It’s a practice that embodies a test first methodology.

• In either real world or the virtual world, in software industry or any other industry, TDD can / is being followed. Building a house or a room. Seminar planning.

• You have requirements or cases or stories for a project. With TDD, you first start from writing test cases for requirements and then followup with development code to pass those test cases.

Page 5: Test Driven Development using QUnit
Page 6: Test Driven Development using QUnit
Page 7: Test Driven Development using QUnit

A look into BDD• In contrast to TDD, BDD is when we write behaviour & specification that

then drives our software development.

• Behaviour & specification might seem awfully similar to tests but the difference is very subtle and important.

• The main difference is just the wording. BDD uses a more verbose style so that it can be read almost like a sentence.

• The ability to read your tests like a sentence is a cognitive shift in how you will think about your tests. The argument is that if you can read your tests fluidly, you will naturally write better and more comprehensive tests.

• BDD tests should be more focused on the features, not the actual results.

• https://github.com/joshuaclayton/specit

Page 8: Test Driven Development using QUnit

Real life example• As the saying goes for a tailor “Measure twice, cut

once”

• A Tailor -> Software Developer

• Notes the cloth requirement -> Project requirement

• Starts with measurement, prototypes using sketches, software models -> Test cases

• Starts cutting cloth with every minute precision of measurement being compared with tools against specifications -> Code development and running test cases reiteration.

Page 9: Test Driven Development using QUnit

TDD concepts• Steps of TDD:

- Test first- “Make it run”- Refactor / Make it better.

• Refactoring: Act of restructuring, rewriting, renaming and removing code in order to improve:- Design- Readability- Maintainability

• 3 A’s of Unit Testing:- Assemble- Act- Assert

• Unit Test: Focus is on individual components.

• From unit tests, one can proceed on to end to end testing where focus is on complete behaviour of a feature.

Page 10: Test Driven Development using QUnit

• A simple test case written using console.log

• Need of unit test frameworks for testing

• Provide tools and APIs to test.

• A framework that provides a test runner, as well as some utilities to write the actual tests.

Page 11: Test Driven Development using QUnit

What is QUnit?• QUnit is a powerful, easy-to-use JavaScript

unit testing framework.

• It's used by the jQuery, jQuery UI and jQuery Mobile projects and is capable of testing any generic JavaScript code, including itself!

• http://qunitjs.com/

Page 12: Test Driven Development using QUnit

QUnit user interface and it's various parts.

Page 13: Test Driven Development using QUnit

QUnit API sample use cases with examples

• ok( truthy [, message ] )

• equal( actual, expected [, message ] )

• strictEqual()

• deepEqual( actual, expected [, message ] )

• expect() -> testing callbacks

• async()

• QUnit.module()

Page 14: Test Driven Development using QUnit

TDD using unit tests with QUnit

Page 15: Test Driven Development using QUnit

• Advantages:- Your application is test covered.- Customers, clients and developers are more confident about their application robustness.- Tests are automated. You just need to run it. Can be part of a CI process.- Productivity increases.- You implement features and fixes without worrying about what you have broken.- Testing can also drive the design of software. Unit block codes are present and are decoupled and independent. TDD thinking helps in better API design.- It acts as a documentation for your application.

Page 16: Test Driven Development using QUnit

Some practical use cases of QUnit

• https://github.com/jquery/jquery/tree/master/test/unit

• https://github.com/jquery/jquery-ui/tree/master/tests/unit

• https://github.com/jzaefferer/jquery-validation/tree/master/test

Page 17: Test Driven Development using QUnit

References

• http://hubpages.com/business/Test-First-A-Real-Life-Example

• http://qunitjs.com/

• http://qunitjs.com/cookbook/

• http://qunitjs.com/plugins/

Page 18: Test Driven Development using QUnit

Conclusion

• TDD Rocks :)

Page 19: Test Driven Development using QUnit

Any?