2. What is unit testing?
“Testing smallest testable parts of an
application, called units, individually
and independently”.
3. Let’s start with a very simple example
Imagine a simple class called Calculator
class Calculator {
function add(a, b) {
return a + b
}
…….
function divide(a, b) {
return a / b
}
}
4. Let’s test the code
class TestCalculater extends TestCase {
variable calculator
function setup(){
calculator = Calculator()
}
….
function tearDown(){
calculator = Null
}
}
9. How to run test cases
1. IDE’s support running test cases directly from UI menu.
2. But running them in command line can be very powerful for obvious reasons.
3. You will get an output in either way as how many tests passed and failed.
10. Why bother at all?
1. There is a cost to writing tests. But it’s a long term investment that will benefit
in the long run.
2. In software industry product with internal quality will win in the long run
assuming other factors are same.
3. Build and maintain developer confidence in changing and refactoring code
constantly to make it look good and perform better.
4. Ability to do Continuous Integration (CI) which will test every commit of very
team member so the project could move forward in confidence.
11. 1. It’s a responsibility of the developer to write the unit tests, not QA.
2. As a rule of thumb, if you write a function it’s mainly your responsibility to
write unit tests for that function.
Who should write unit tests
12. It’s not always easy
1. Sometimes it’s not easy to write tests (For example testing view controllers in
mobile applications)
2. You have to think hard and come up with creative ways to test some
components.
3. Sometime dependency injection is required to test components separately.
13. Code coverage
What is code coverage?
Amount of code covered by tests. This metric is measured as a percentage.
Is 100% code coverage possible?
Not impossible but not practical and pragmatic for most situations.
Then how much is optimal?
About 70% would be a practical target.