KEMBAR78
Understanding Unit Testing | PPTX
Understanding
Unit Testing
INTRODUCTION TO AUTOMATED TESTING AND UNIT TESTING
Introduction
 Ikhwan Hayat (a.k.a 1kHz)
 9 years experience developing
software
 Freelance Software Developer
 ikhwanhayat@gmail.com
2
manual
3
Testing?
VS
automated
white box
VS
black box
acceptance test
integration test
unit test
Unit? 4
Unit testing is a method by which individual units of
source code are tested to determine if they are fit for
use.
One can view a unit as the smallest testable part
of an application.
Unit tests are created by programmers or
occasionally by white box testers during the
development process.
TDD
 Test Driven Design
 It’s NOT testing, but using tests to DRIVE the
design
 As a side-effect, you got unit tests!
With good level of coverage!
5
6
RED
GREEN
REFACTOR
Write a failing test. With empty class/method.
Fill in the class/method implementation. Make the tests pass.
Make code better.
Demo: Bank Account
 Requirements
 Can create bank accounts
 Can deposit money into account
 Can withdraw money from account
 Throw exception if balance is insufficient
 Can transfer money from one account to another
 Can insert and update accounts into database
7
8
IDE / Platform
http://www.microsoft.com/visualstudio/eng/pro
ducts/visual-studio-express-for-web
https://code.google.com/p/moq/
Unit Testing Framework
Mocking Framework
http://nunit.org/
Tools
Design 9
10
ARRANGE
ACT
ASSERT
Unit
11
Test
12
Design 2 13
14
Unit Test
Design 3 15
16
repository
VS
active record
???
17Unit Test
A good unit test is…
 Isolated/independent
 Test one thing at a time.
 Unit under test doesn’t depend on the other to make test runs.
 Repeatable
 Running multiple times yields the same result.
 Doesn’t rely on environment.
 Fast
 You want to repeat it again and again.
 You want it to be a pleasure to work with.
 Self-Documenting
 Test code and code under test clear and concise.
 Can be a reference for usage of your class/method/etc.
18
19
Dependency Injection
Separation of Interface
and Implementation
Single Responsibility Principal
20
Web Controller DI/IoC Setup
21
“Mocking” allow us to
isolate dependent
units.
Types of Test Doubles
 Dummy objects are passed around but never actually
used. Usually they are just used to fill parameter lists.
 Fake objects actually have working
implementations, but usually take some shortcut which
makes them not suitable for production.
 Stub objects provide canned answers to calls made
during the test, usually not responding at all to anything
outside what's programmed in for the test.
 Mock objects are pre-programmed with expectations
which form a specification of the calls they are
expected to receive.
22
23
Unit Test
Benefits
 Instant feedback
 Write test, write code, see instant result.
 Promote modularity in your design
 DI, SRP, Interface vs Implementation.
 Safety net
 Change/add code and check if business rules are still
honored.
 Free documentation
 Can be a reference for usage of your
class/method/etc.
24
Done!
 What is unit testing.
 TDD.
 How to write good unit tests.
 How unit testing can benefit us.
25
We have learned…
Get the codes and slides at…
https://github.com/ikhwanhayat/jomweb_unittest
Next?
 Learning BDD (Behavior Driven Design).
 Research CI (Continuous Integration).
 Learn on how to design testable systems.
 Use unit testing for your project!
(I mean, seriously, USE IT!)
26
You can go on with…
MOAR!
 Google+ MyDev
 http://www.mydev.my/automated-testing-
dalam-pembangunan-perisian.html
 ikhwanhayat@gmail.com
27
THANK YOU FOR LISTENING!

Understanding Unit Testing

  • 1.
    Understanding Unit Testing INTRODUCTION TOAUTOMATED TESTING AND UNIT TESTING
  • 2.
    Introduction  Ikhwan Hayat(a.k.a 1kHz)  9 years experience developing software  Freelance Software Developer  ikhwanhayat@gmail.com 2
  • 3.
  • 4.
    Unit? 4 Unit testingis a method by which individual units of source code are tested to determine if they are fit for use. One can view a unit as the smallest testable part of an application. Unit tests are created by programmers or occasionally by white box testers during the development process.
  • 5.
    TDD  Test DrivenDesign  It’s NOT testing, but using tests to DRIVE the design  As a side-effect, you got unit tests! With good level of coverage! 5
  • 6.
    6 RED GREEN REFACTOR Write a failingtest. With empty class/method. Fill in the class/method implementation. Make the tests pass. Make code better.
  • 7.
    Demo: Bank Account Requirements  Can create bank accounts  Can deposit money into account  Can withdraw money from account  Throw exception if balance is insufficient  Can transfer money from one account to another  Can insert and update accounts into database 7
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
    A good unittest is…  Isolated/independent  Test one thing at a time.  Unit under test doesn’t depend on the other to make test runs.  Repeatable  Running multiple times yields the same result.  Doesn’t rely on environment.  Fast  You want to repeat it again and again.  You want it to be a pleasure to work with.  Self-Documenting  Test code and code under test clear and concise.  Can be a reference for usage of your class/method/etc. 18
  • 19.
    19 Dependency Injection Separation ofInterface and Implementation Single Responsibility Principal
  • 20.
  • 21.
    21 “Mocking” allow usto isolate dependent units.
  • 22.
    Types of TestDoubles  Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.  Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production.  Stub objects provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.  Mock objects are pre-programmed with expectations which form a specification of the calls they are expected to receive. 22
  • 23.
  • 24.
    Benefits  Instant feedback Write test, write code, see instant result.  Promote modularity in your design  DI, SRP, Interface vs Implementation.  Safety net  Change/add code and check if business rules are still honored.  Free documentation  Can be a reference for usage of your class/method/etc. 24
  • 25.
    Done!  What isunit testing.  TDD.  How to write good unit tests.  How unit testing can benefit us. 25 We have learned… Get the codes and slides at… https://github.com/ikhwanhayat/jomweb_unittest
  • 26.
    Next?  Learning BDD(Behavior Driven Design).  Research CI (Continuous Integration).  Learn on how to design testable systems.  Use unit testing for your project! (I mean, seriously, USE IT!) 26 You can go on with…
  • 27.
    MOAR!  Google+ MyDev http://www.mydev.my/automated-testing- dalam-pembangunan-perisian.html  ikhwanhayat@gmail.com 27 THANK YOU FOR LISTENING!

Editor's Notes

  • #5 In procedural -> module/function, In OOP -> class/method. Jenis test paling mendalam. What you actually test is behavior.
  • #9 Create projects
  • #10 Go to codes
  • #14 Go to codes
  • #16 Copy files