Made Easy with SQL Developer Philipp - Salvis
Transcript of Made Easy with SQL Developer Philipp - Salvis
![Page 1: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/1.jpg)
https://www.salvis.com/blog@phsalvisberg
Testing with utPLSQLMade Easy with SQL Developer
Philipp
![Page 2: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/2.jpg)
Philipp• Database centric development
• Model Driven Software Development
• Author of free SQL Developer Extensions PL/SQL Unwrapper, PL/SQL Cop, utPLSQL, plscope-utils, oddgen and Bitemp Remodeler
@phsalvisberg https://www.salvis.com/blog
![Page 3: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/3.jpg)
Agenda
1. Introduction2. Installation3. Build & Run Tests in SQL Developer4. Run Code Coverage Reports in SQL Developer5. Core Messages
![Page 4: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/4.jpg)
Introduction
![Page 5: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/5.jpg)
Why?
Does it work?
Do we get the expected
results?
Are the requirements
met?
Is it complete?
Can I reproduce the bug?
Is the bug fixed?
Are there side-effects?
"Unit"
![Page 6: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/6.jpg)
utPLSQL Test Scope
Source: Miško Hevery, The Clean Code Talks, Unit Testing, October 30, 2008,
https://www.youtube.com/watch?v=wEhu57pih5w&t=991
utP
LS
QL
• GUI
• API
• Integration
• Components
• Unit
![Page 7: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/7.jpg)
utPLSQL Units Under Test
Primary
• Types
• Packages
• Procedures
• Functions
Secondary
• Non-PL/SQL Units
• Views
• Triggers
• Tables
![Page 8: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/8.jpg)
utPLSQL Suite – Apache 2.0 License
Mandatory
• Core Testing Framework
• Schema installed in Oracle DB
• No repository
• Annotation based tests
Optional
• Command Line Client
• Maven Plugin
• SQL Developer Extension
![Page 9: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/9.jpg)
CREATE OR REPLACE PACKAGE test_package_name AS
--%suite
--%test
PROCEDURE procedure_name;
END;
Test Declaration
--%suite(<description>)--%suitepath(<path>)--%tags(<tag>[,...]--%displayame(<description>)--%beforeall([…])--%afterall([…])--%beforeeach([…])--%aftereach([…])--%rollback(manual)--%disabled--%context--%endcontext
--%displayname(<description>)--%test(<description>)--%tags(<tag>[,...]--%throws(<exception>[,...])--%beforeall--%afterall--%beforeeach--%aftereach--%beforetest([…])--%aftertest([…])--%rollback(manual)--%disabled
![Page 10: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/10.jpg)
CREATE OR REPLACE PACKAGE BODY test_package_name AS
PROCEDURE procedure_name IS
l_actual INTEGER := 0;
l_expected INTEGER := 1;
BEGIN
ut.expect(l_actual).to_equal(l_expected);
END procedure_name;
END;
Test Implementation
Matcher:
be_between, be_empty, be_false, be_greater_than, be_greater_or_equal, be_less_or_equal, be_less_than, be_like, be_not_null, be_null, be_true, equal, have_count, match
Extended options for refcursor, object type, JSON, nested table and varray:
- include(<items>)- exclude(<items>)- unordered- join_by(<items>)
![Page 11: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/11.jpg)
SET SERVEROUTPUT ON SIZE UNLIMITED
EXEC ut.run('test_package_name')
Test Run
test_package_name
procedure_name [.003 sec] (FAILED - 1)
Failures:
1) procedure_name
Actual: 0 (number) was expected to equal: 1 (number)
at "TEST_PACKAGE_NAME.PROCEDURE_NAME", line 7 ut.expect(l_actual).to_equal(l_expected);
Finished in .007015 seconds
1 tests, 1 failed, 0 errored, 0 disabled, 0 warning(s)
![Page 12: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/12.jpg)
Installation
![Page 13: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/13.jpg)
Install utPLSQL Core Testing Framework
• Download utPLSQL.zip from https://github.com/utPLSQL/utPLSQL/releases
• Unzip utPLSQL.zip
• cd source
• sqlplus / as sysdba @install_headless.sql
• User UT3
• Password XNtxj8eEgA6X6b6f
• Tablespace USERS
![Page 14: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/14.jpg)
Install utPLSQL for SQL Developer
• Download utplsql_for_SQLDev_*.zip from https://github.com/utPLSQL/utPLSQL-SQLDeveloper/releases
• Start SQL Developer
• Select "Check for Updates…" in the help menu
• Use the "Install From Local File" option to install the previously downloaded "utplsql_for_SQLDev_*.zip" file
• User must have read/write access to SQL Developer installation directory
• Run as Administrator, if required
• Restart SQL Developer
![Page 15: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/15.jpg)
Build & Run Tests in SQL Developer
![Page 16: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/16.jpg)
Starting Point?
1 2
![Page 17: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/17.jpg)
Test First – Create Test from Template
![Page 18: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/18.jpg)
Test First – Complete Test & Run
![Page 19: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/19.jpg)
Configuration – Running utPLSQL Tests
![Page 20: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/20.jpg)
Configuration – Generating utPLSQL Tests
![Page 21: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/21.jpg)
Test Last – Create Test from Existing Code
![Page 22: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/22.jpg)
Test Last – Generate Multiple Test Skeletons
![Page 23: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/23.jpg)
Run Code Coverage Reportsin SQL Developer
![Page 24: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/24.jpg)
Code Coverage – Defintion
A measure used to describe the degree to which the source code of a program is executed
when a particular test suite runs.
Source: https://en.wikipedia.org/wiki/Code_coverage
![Page 25: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/25.jpg)
CREATE OR REPLACE FUNCTION f(a IN INTEGER) RETURN INTEGER IS
BEGIN
IF a IS NULL THEN
RETURN 0;
ELSE
RETURN a*a;
END IF;
END f;
/
Line Coverage
Two test cases for 100% coverage
![Page 26: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/26.jpg)
CREATE OR REPLACE FUNCTION f(a IN INTEGER) RETURN INTEGER IS
BEGIN
IF a IS NULL THEN RETURN 0; ELSE RETURN a*a; END IF;
END f;
/
Code Block Coverage (12.2 and higher)
Two test cases for 100% coverage
CREATE OR REPLACE FUNCTION f(a IN INTEGER) RETURN INTEGER IS
BEGIN
RETURN coalesce(a*a, 0);
END f;
/ One test case for 100% coverage
when passing NULL
![Page 27: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/27.jpg)
utPLSQL – Line & Code Block Coverage
1 of 2 code blocks
covered
Passing a not null
value
Is this good? helpful?
![Page 28: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/28.jpg)
Run Code Coverage Report
![Page 29: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/29.jpg)
Code Coverage Report
![Page 30: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/30.jpg)
Core Messages
![Page 31: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/31.jpg)
The First Step Is the Hardest
• Set up a test-friendly environment
• Install utPLSQL core testing framework
• Install SQL Developer for utPLSQL
• Start with tests
• to reproduce bugs
• for new requirements
• utPLSQL will change how you code
• Write smaller units
• Isolate code that is difficult to test
![Page 32: Made Easy with SQL Developer Philipp - Salvis](https://reader030.fdocuments.us/reader030/viewer/2022040815/624db2d8bbbb26142d10e213/html5/thumbnails/32.jpg)