QUnit

About the Tutorial QUnit is a framework for JavaScript programming language. QUnit has been important in the field of test-driven development, and is used by jQuery, jQuery UI, and jQuery Mobile projects. QUnit is capable of testing any generic JavaScript codebase.

This tutorial explains the fundamental concepts of QUnit and how to use QUnit in day-to- day life of any unit testing project while working with JavaScript.

Audience This tutorial has been prepared for the beginners to help them understand the basic functionality of QUnit tool. After completing this tutorial, you will find yourself at a moderate level of expertise in using QUnit testing framework from where you can take yourself to the next level.

Prerequisites We assume you are going to use QUnit to handle all levels of JavaScript projects development. Hence, it will be good if you have knowledge of website development using any Scripting language, especially JavaScript Scripting and website testing process.

Copyright & Disclaimer  Copyright 2018 by Tutorials Point (I) Pvt. Ltd.

All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher.

We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at [email protected]

i

QUnit

Table of Contents

About the Tutorial ...... i

Audience ...... i

Prerequisites ...... i

Copyright & Disclaimer ...... i

Table of Contents ...... ii

1. QUNIT ─ OVERVIEW...... 1

What is QUnit ? ...... 1

Features of QUnit ...... 2

What is a Unit Test Case? ...... 2

2. QUNIT ─ ENVIRONMENT SETUP ...... 3

Local Installation ...... 3

CDN Based Version ...... 4

3. QUNIT ─ BASIC USAGE ...... 6

4. QUNIT ─ API ...... 9

5. QUNIT ─ USING ASSERTIONS ...... 12

6. QUNIT ─ EXECUTION PROCEDURE ...... 15

7. QUNIT ─ SKIP TEST ...... 17

8. QUNIT ─ ONLY TEST ...... 19

9. QUNIT ─ ASYNC CALL ...... 21

10. QUNIT ─ EXPECT ASSERTIONS ...... 23

11. QUNIT ─ CALLBACKS ...... 25

12. QUNIT ─ NESTED MODULES ...... 29

ii

QUnit 1. QUnit ─ Overview

Testing is the process of checking the functionality of the application whether it is working as per the requirements and to ensure that at the developer level, unit testing comes into picture. Unit testing is the testing of a single entity (class or method). Unit testing is very essential for every software organization to offer quality products to their clients.

Unit testing can be done in two ways as mentioned in the following table.

Manual Testing Automated Testing

Executing the test cases manually without Taking tool support and executing the test any tool support is known as manual cases using automation tool is known as testing. automation testing.

Time consuming and tedious. Since the Fast Automation. Runs test cases test cases are executed by human significantly faster than human resources. resources, it is very slow and tedious.

Huge investment in human resources. As Less investment in human resources. Test test cases need to be executed manually, cases are executed using automation tool more number of testers are required. hence, less number of testers are required.

Less reliable, as tests may not be More reliable. Automation tests perform performed with precision each time due to precisely the same operation each time they human errors. are run.

Non-programmable. No programming can Programmable. Testers can program be done to write sophisticated tests, sophisticated tests to bring out hidden which fetch hidden information. information.

What is QUnit ? QUnit is a unit testing framework for JavaScript programming language. It is important in the test driven development, and is used by jQuery, jQuery UI, and jQuery Mobile projects. QUnit is capable of testing any generic JavaScript codebase.

QUnit promotes the idea of "first testing then coding", which emphasizes on setting up the test data for a piece of code, which can be tested first and then implemented. This approach is like "test a little, code a little, test a little, code a little..." which increases the programmer’s productivity and the stability of program code reducing the programmer’s stress and the time spent on debugging.

1

QUnit

Features of QUnit QUnit is an open source framework used for writing and running tests. Following are its most prominent features:

 QUnit provides Assertions for testing expected results.

 QUnit provides Test fixtures for running tests.

 QUnit tests allow to write code faster, which increases the quality.

 QUnit is elegantly simple. It is less complex and takes less time.

 QUnit tests can be run automatically and they check their own results and provide immediate feedback. There's no need to manually comb through a report of test results.  QUnit tests can be organized into test suites containing test cases and even other test suites.

 QUnit shows test progress in a bar that is green if the test is going fine, and it turns red when a test fails.

What is a Unit Test Case?

A Unit Test Case is a part of code which ensures that another part of the code (method) works as expected. To achieve the desired results quickly, test framework is required. QUnit is a perfect unit test framework for JavaScript programming language.

A formal written unit test case is characterized by a known input and by an expected output, which is worked out before the test is executed. The known input should test a precondition and the expected output should test a post-condition.

There must be at least two unit test cases for each requirement: one positive test and one negative test. If a requirement has sub-requirements, each sub-requirement must have at least two test cases as positive and negative.

2

QUnit 2. QUnit ─ Environment Setup

There are two ways to use QUnit.

 Local Installation − You can download QUnit library on your local machine and include it in your HTML code.

 CDN Based Version − You can include QUnit library into your HTML code directly from Content Delivery Network (CDN).

Local Installation  Go to the https://code.jquery.com/qunit/ to download the latest version available.

 Place the downloaded qunit-git.js and qunit-git. file in a directory of your website, e.g. /.

Example You can include qunit-git.js and qunit-git.css files in your HTML file as follows −

QUnit basic example

3

QUnit

This will produce the following result -

CDN Based Version You can include QUnit library into your HTML code directly from Content Delivery Network (CDN).

We are using jQuery CDN version of the library throughout this tutorial.

Example Let us rewrite the above example using QUnit library from jQuery CDN.

QUnit basic example

4

QUnit

This will produce following result –

5

QUnit

End of ebook preview

If you liked what you saw…

Buy it from our store @ https://store.tutorialspoint.com

6