3. Unit testing and Module testing
• In unit testing, developers create small tests
during development to check correct behavior
of software
• In module testing, dedicated testers test
software thoroughly after development
4. What is TDD?
• Test Driven Development
• In TDD, we write unit test before writing
actual code, and then we write the code so
that they pass the test
• This process is known as Red-Green-Refactor
process
6. What is BDD?
• Behavior Driven Development
• BDD focuses on the behaviors of software that
are most important to the customers. In BDD,
customers are actively involved in the
development process by means of User stories
7. BDD User Stories
• BDD user stories are usually written in this
format
Story: [Title]
As [Role]
I want [Feature]
To [Benefit]
Story: [I buy juice]
As [a thirsty person]
I want [to buy juice]
To [drink it and stay hydrated]
Scenario1: [Title]
Given [Context]
AND [Other Context]
When [Event]
Then [Outcome]
AND [Other Outcome]
Scenario1: [Enough money & near vending machine]
Given [I have enough money]
AND [I have access to vending machine]
When [I put money and select juice]
Then [I should get the juice]
AND [I should be able to drink it]
8. What is Mocha.JS?
• Mocha.js is a testing framework for javascript
that runs on both browser and Node.js server
• Using Mocha, we usually write test code for
each functions of our source code. We can tell
mocha to report error when the output of a
test is a certain value
9. Mocha.JS client example
(test.html)
<html>
<head>
<title>Mocha Tests</title>
<link rel="stylesheet" href="mocha.css" /> <!– load the mocha css file -->
</head>
<body>
<div id="mocha"></div>
<script src="jquery.js"></script>
<script>function assert(expr, msg) {if (!expr) throw new Error(msg || 'failed');}
</script> <!– assertion method -->
<script src="mocha.js"></script> <!– load the mocha.js library -->
<script>mocha.setup('bdd')</script> <!– setup mocha.js to use BDD style -->
<script src="test.array.js"></script> <!– an example test that tests array -->
<script src="test.object.js"></script> <!– an example test that tests objects -->
<script src="test.xhr.js"></script> <!– an example test that tests xhr -->
<script>
mocha.checkLeaks();
mocha.globals(['jQuery']);
mocha.run(); <!– start the test -->
</script>
</body>
</html>
10. How to write a Mocha.js test
(test.array.js)
describe('Array', function(){ //test suite for array object
describe('#pop()', function(){ //test suite for pop method
it('should remove and return the last value', function(){ //a test case
var arr = [1,2,3];
assert(arr.pop() == 3); //pop method removes the last element and returns it
assert(arr.pop() == 2);
assert(arr.pop() == -1); //this will generate error since it should be 1 not -1
})
it('should adjust .length', function(){ //another test case
var arr = [1,2,3];
arr.pop();
assert(arr.length == 2);
})
})
})
11. Mocha.js and assertion libraries
• The assert function in the previous works like
this:
function assert(expr, msg) {
//if expr is false, throw error
if (!expr) throw new Error(msg || 'failed');
}
• But we can also use assertion libraries like
should.js, expect.js, chai.js etc
12. The test (test.array.js) rewritten
using chai.js
describe('Array', function(){ //test suite for array object
describe('#pop()', function(){ //test suite for pop method
it('should remove and return the last value', function(){ //a test case
var arr = [1,2,3];
arr.pop().should.equal(3);
arr.pop().should.equal(2);
arr.pop().should.equal(-1); //generate error
})
it('should adjust .length', function(){ //another test case
var arr = [1,2,3];
arr.pop();
arr.length.should.equal(2)
})
})
})
13. UI Testing
• When we develop a software with graphical
user interface, it also becomes necessary to
test that the interface behaves correctly in
addition to testing the behaviors of functions
• However, since it becomes a tedious task to
manually operate and test intereface manually
everytime you change code, automated
testing frameworks are created