Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Unit Testing Usage

Unit Testing Usage

Alexey Novakov

October 26, 2012
Tweet

More Decks by Alexey Novakov

Other Decks in Programming

Transcript

  1. Targets & Tasks • Show that we are able to

    apply Unit Test in LogNet • Tasks: – Show where to use – How to use – Short overview of several tools
  2. Agenda 1) Bases of Unit Testing 2) Applicable platforms 3)

    PL/SQL example 4) Java example - code 5) Java Script example - code 6) Anti-patterns
  3. About Unit testing: testing of individual software components or modules.

    Typically done by the programmer and not by testers, as it requires detailed knowledge of the internal program design and code. May require developing test driver modules or test harnesses.
  4. Good unit test • Automatic, run fast • Wide code

    coverage including edge cases • Repeatable • Independent from execution order • Using real and understandable test data
  5. Test writing 1) Create Fixture, replace dependency 2) Call ‘asserts’

    to verify with wrong input data (pre-requirements) 3) Call ‘asserts’ to verify initial requirements, i.e. purpose of module (post-requirements)
  6. Scope of Unit Testing Need to test this No need

    to test including whole chain Business Module 1 Repository API Email client File API WS- Client Data source Provider Modules Web-service File system Email server Repository Database Business Module 2
  7. Defining of Unit Business Module 1 Repository API Email client

    File API WS- Client Data source Provider Modules(Classes) to be replaced as they are dependencies Modules to be used for Unit Testing Business Module 2
  8. Where to apply • PL/SQL – DB Kernel – Customization:

    packages • Java & C++ – IFS, WebKernel, OCS – MaxRate, MaxGen • Web: – Smart client-side GUI: JavaScript – XSL templates LogNet
  9. PL/SQL concerns • It is already in database, commonly there

    are external dependencies • No complex OOP encapsulations, as it is procedure language • Unit may change data storage physically • Test must be repeatable as anywhere else
  10. PL/SQL Tools • utPLSQL (free; package driven, modeled on the

    Junit, last update in 2005 ☹) • Quest Code Tester (commercial GUI based, test builder) • pl/unit(free, similar to utPLSQL, it has simple GUI) • Oracle SQL Developer (free, GUI based) • Pluto: PL/SQL Unit Testing (Object based) • Ruby-plsql-spec (need to know Ruby a bit) • DBFit (web-based GUI, scenario/pseudo-language)
  11. Data change case • To test the contents of a

    datasets, you need to define: – The table, view or query that is changed by the program. – The table, view or query that is your expected result. • To emulate outcome: – Select <outcome..> from dual; – create table/view as Select <outcome...> from <table_under_SUT>
  12. Data change: Approach • Execute each test in a separate

    transaction • use the Setup procedure to insert our data, and the Teardown procedure to rollback the insert Example: PROCEDURE setup IS BEGIN INSERT INTO rooms VALUES (1, 'Dining Room'); INSERT INTO rooms VALUES (2, 'Living Room'); INSERT INTO rooms VALUES (3, 'Office'); END; PROCEDURE teardown IS BEGIN ROLLBACK; END; • Your program must not have ‘commit’ statement inside (System should not have presence of executed tests)
  13. Example from project • Public procedure • Purpose is CHO

    • pkg_sim_portal.change_services ➢ pmvnoref, varchar2, in /*Tenant name*/ ➢ pphoneno, varchar2, in /*Resource number*/ ➢ pservices, varchar2, in /*List of item names*/ ➢ perrmsg, varchar2, out /*Error text message*/ ➢ pimmediate, number, in, default – yes /*Flag, to do an order or not*/ • Define test cases in order to: 1) Test pre-requirements 2) Test post-requirements
  14. Set Up 1) Create tenant-customer (GTC) 2) Create customer, link

    to created tenant 3) Create Service Package, services (Service 1) 4) Instantiate Service Package as Entity Package on customer level 5) Create and assign MSISDN (493383390202) and other required resources to entity package
  15. Pre-requirements cases Parameters Case1 Case2 Case3 Case4 pMVNORef null GTC

    GTC GTC pPhoneNo 493383390202 <Fake number > or null 493383390202 493383390202 pServices Service A Service A null Service B Expected pErrmsg <Not empty> <Not empty> <Not empty> <Not empty> pImmediate null null null null ….and other fake/negative-values tests
  16. Post-requirements cases 1) Income: pMVNO=GTC, pPhoneno=493383390202, pServices=‘Service 1’, pImmediate=null Outcome:

    pErrmsg, expected value=<empty> Behavior check: expected: followup record insertion, provisioning request insertion 2) Income: pMVNO=GTC, pPhoneno=493383390202, pServices=‘Service 1’, pImmediate=1 Outcome: pErrmsg, expected value=“Not implemeted” Behavior check: expected: no followup record insertion, no record provisioning request insertion
  17. Example from project Description: • Device agent pattern • Get

    something from database • Process some data and store into database • Processor class has business logic to be tested Tools used: • • Go to IDE….
  18. Actions What I had to do: • Mockup of dependency

    via method replace • Refactoring of SUT and custom Mock object Result: • Several test cases just for single class • Two existing files(classes) were changed • Using of anti-pattern Public Morozov :-(
  19. Java Script features • Commonly, Internet browser is an interpreter

    • Code has deal with DOM • It has OOP paradigm as well
  20. QUnit • Made by jQuery team • Tests run in

    a browser • Able to test DOM oriented and asynchronous modules • Custom assertions • Excluding of passed tests
  21. Example 1. <!DOCTYPE html> 2. <html> 3. <head> 4. <title>QUnit

    Test</title> 5. <link rel="stylesheet" href="qunit.css"> 6. <script src="qunit.js"></script> 7. <script src="tests.js"></script> 8. </head> 9. <body> 10. <h1 id="qunit-header">QUnit Test</h1> 11. <h2 id="qunit-banner"></h2> 12. <div id="qunit-testrunner-toolbar"></div> 13. <h2 id="qunit-userAgent"></h2> 14. <ol id="qunit-tests"></ol> 15. <div id="qunit-fixture">test markup</div> 16. </body> 17. </html> Report markup include HTML source
  22. SUT and Unit test code 1. function format(string, values) {

    2. for (var key in values) { 3. string = string.replace(new RegExp("\{" + key + "}"), values[key]); 4. } 5. return string; 6. } 7. 8. test("basics", function() { 9. var values = { 10. name: "World" 11. }; 12. equal( format("Hello, {name}", values), "Hello, World", "single use" ); 13. equal( format("Hello, {name}, how is {name} today?", values), 14. "Hello, World, how is World today?", "multiple" ); 15. }); Java script source
  23. Anti-patterns TDD Article: http://habrahabr.ru/blogs/development/43761/ 1) Giant (long) 2) Generous Leftovers

    (reuse) 3) The Local hero (works here, doesn’t work there) 4) The Loudmouth (too much log messages) 5) The Enumerator (poor test method names) 6) The Slow Poke (too slow)