test driven development using qunit
TRANSCRIPT
Test Driven Development (TDD)
using QUnitBy Satej Kumar Sahu
Mindfire Solutions
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
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.
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.
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
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.
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.
• 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.
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/
QUnit user interface and it's various parts.
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()
TDD using unit tests with 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.
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
References
• http://hubpages.com/business/Test-First-A-Real-Life-Example
• http://qunitjs.com/
• http://qunitjs.com/cookbook/
• http://qunitjs.com/plugins/
Conclusion
• TDD Rocks :)
Any?