Slide 1

Slide 1 text

Unit Test - Usage Where/How to use Alexey Novakov

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

Good unit test • Automatic, run fast • Wide code coverage including edge cases • Repeatable • Independent from execution order • Using real and understandable test data

Slide 6

Slide 6 text

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)

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

PL/SQL Unit Testing

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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)

Slide 13

Slide 13 text

Quest Code Tester

Slide 14

Slide 14 text

Oracle SQL Developer

Slide 15

Slide 15 text

PL/Unit

Slide 16

Slide 16 text

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 from dual; – create table/view as Select from

Slide 17

Slide 17 text

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)

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Pre-requirements cases Parameters Case1 Case2 Case3 Case4 pMVNORef null GTC GTC GTC pPhoneNo 493383390202 or null 493383390202 493383390202 pServices Service A Service A null Service B Expected pErrmsg pImmediate null null null null ….and other fake/negative-values tests

Slide 21

Slide 21 text

Post-requirements cases 1) Income: pMVNO=GTC, pPhoneno=493383390202, pServices=‘Service 1’, pImmediate=null Outcome: pErrmsg, expected value= 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

Slide 22

Slide 22 text

Tear Down • Rollback transaction;

Slide 23

Slide 23 text

Java(C++) Unit Testing

Slide 24

Slide 24 text

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….

Slide 25

Slide 25 text

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 :-(

Slide 26

Slide 26 text

Java Script Unit Testing

Slide 27

Slide 27 text

Java Script features • Commonly, Internet browser is an interpreter • Code has deal with DOM • It has OOP paradigm as well

Slide 28

Slide 28 text

QUnit • Made by jQuery team • Tests run in a browser • Able to test DOM oriented and asynchronous modules • Custom assertions • Excluding of passed tests

Slide 29

Slide 29 text

Example 1. 2. 3. 4. QUnit Test 5. 6. 7. 8. 9. 10.

QUnit Test

11.

12.
13.

14.
    15.
    test markup
    16. 17. Report markup include HTML source

    Slide 30

    Slide 30 text

    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

    Slide 31

    Slide 31 text

    Report view

    Slide 32

    Slide 32 text

    Post-fix • new RegExp("\{" + key + "}", "g")

    Slide 33

    Slide 33 text

    QUnit:Unit Test for Class

    Slide 34

    Slide 34 text

    Unit Code

    Slide 35

    Slide 35 text

    Unit Test

    Slide 36

    Slide 36 text

    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)

    Slide 37

    Slide 37 text

    Questions?