Introduction to Unit Testing using QUnit

20
Software Testing by Varun Vaddiparty

description

An Introduction to Unit testing, what it is, why is it needed and how it is done. Using QUnit.

Transcript of Introduction to Unit Testing using QUnit

Page 1: Introduction to Unit Testing using QUnit

Software Testing by Varun Vaddiparty

Page 2: Introduction to Unit Testing using QUnit

● User Interface Development● Source Code● Quality Assurance (software testing)

Page 3: Introduction to Unit Testing using QUnit

Need for software testing:

● Meets the requirements ● works as expected● satisfies the need of customer

Different Methods:● Static Testing● Dynamic Testing

Page 4: Introduction to Unit Testing using QUnit
Page 5: Introduction to Unit Testing using QUnit
Page 6: Introduction to Unit Testing using QUnit

Unit Testing

● Verify the functionality of specific section of the code.

● Usually at the function level.● To ensure that a specific function is working

as expected.

Page 7: Introduction to Unit Testing using QUnit

Qunit

● A Javascript unit testing framework.● It can be used to test any generic Javascript

code.● You can test your code in the browser itself.

Page 8: Introduction to Unit Testing using QUnit
Page 9: Introduction to Unit Testing using QUnit

A minimal Qunit test setup.

Page 10: Introduction to Unit Testing using QUnit

The contents of test.js :

Page 11: Introduction to Unit Testing using QUnit

Result of the test:

Page 12: Introduction to Unit Testing using QUnit

Asserting Results

● Essential elements of any unit tests.● You express the result expected and have the

testing framework compare them to the actual values that the functions output.

Qunit provides three assertions:● ok()● equal()● deepEqual()

Page 13: Introduction to Unit Testing using QUnit

ok()

● ok(truthy,[message])● most basic assertion● requires only one argument, in addition you

can send a string to show as a message

examples:ok(true,”true succeeds”);ok(1==1,”test passed”);

Page 14: Introduction to Unit Testing using QUnit

equal()

● equal(actual,expected,[message]);● compares your expected value with the

actual value

examples:equal(1,1,”1 equals 1, test passes”);equal(“”,0,”zero equals empty, test passes”);

Page 15: Introduction to Unit Testing using QUnit

deepEqual()

● deepEqual(actual,expected,[message]);● can be used just like equal, is a better choice● uses more accurate comparison operator

(===) instead of simple (==)

examples:deepEqual(0,””,”test fails, zero not equal to empty”);

Page 16: Introduction to Unit Testing using QUnit

expect()

● used to check whether all the assertions have been executed or not

Page 17: Introduction to Unit Testing using QUnit

asyncTest()

● used for asynchronous testing

Practical Example:

Page 18: Introduction to Unit Testing using QUnit
Page 19: Introduction to Unit Testing using QUnit
Page 20: Introduction to Unit Testing using QUnit

Thank You