PL/SQL Testing - Setup and Teardown of Database Tests
14. Jan 2020•0 gefällt mir•170 views
Downloaden Sie, um offline zu lesen
Melden
Software
Slides of the AskTom Office Hours about Testing PL/SQL Code with utplsql (14.1.2020). Test Data Management and examples on how to version your test data are shown.
PL/SQL Testing - Setup and Teardown of Database Tests
1. ASK TOM – OFFICE
HOURS
Testing in PL/SQL
Jasmin Fluri
Photo by Adi Goldstein on Unsplash
2. Architecture of a Unit Test - Concept
Januar 20ASK TOM - Testing in PL/SQL - Jasmin Fluri 2
Test Object
Preconditions
Postconditions
Input Actual Results
Expected Results
Test Case
Test
Result
Setup Teardown
3. Architecture of a Unit Test - Concept
Januar 20ASK TOM - Testing in PL/SQL - Jasmin Fluri 3
Test
Case
BeforeEachBeforeAll
AfterAll
Test
Case
Test
Case
Test
Case
Test
Case
Test
Case
Test
Case
Test
Case
Test
Case
AfterEach
BeforeEach
Test
Case
AfterEach
BeforeEach
Test
Case
AfterEach
BeforeEach
Test
Case
AfterEach
BeforeAll
AfterAll
BeforeAll
AfterAll
BeforeAll
AfterAll
BeforeEach
AfterEach
4. Architecture of a Unit Test - Naming
Januar 20ASK TOM - Testing in PL/SQL - Jasmin Fluri 4
Package test_package is
--%suite(test_testsuite)
--%beforeall(test_package.setup)
--%context(testcontext)
--%test()
procedure my_first_test;
--%endcontext
end;
/
Package prod_package is
procedure my_first_func;
procedure my_second_func;
end;
/
Productive Procedure / FunctionTest Package
Test
Suite
Test
Case
Test
Context
Tests one
functionality
Test Object
Unit
Under
Test
5. Development and Test Lifecycle / Stages of
Testing
Januar 20ASK TOM - Testing in PL/SQL - Jasmin Fluri 5
TEST INT PROD
Development and
Testing of Code
Version
Control
Continuous
Integration
Deployment
• All unit tests and test data belong into version control!
• Automate the execution of your unit tests in CI!
• Don’t deploy unit tests or test data to production!
TEST PROD
6. Management of Test Data
Januar 20ASK TOM - Testing in PL/SQL - Jasmin Fluri 6
myproject/
├── docs/
│ ├── documentation.md
├── db/
│ ├── 0_sequences/
│ ├── 1_tables/
│ ├── 2_mviews/
│ ├── 3_views/
│ └── 4_packages/
└── tests/
├── packages/
└── data/
Production
code
Test code
and test data
Sample Project Structure
• Similar naming of test packages and prod
packages makes navigation easier
• Store test code and test data separate
from production code
• Have one test package per production
package you are testing
• Store test data and test functionality
separate from each other
7. Unit Test Development Cycle
Define
Test
Case
Create
Test
Data
Setup
Test
Harness
Implement
Test
Run
Test
Januar 20ASK TOM - Testing in PL/SQL - Jasmin Fluri 7
What do you want to test?
What data do you
need to test it?
How does the database need
to look like for the test?
How do I test it?
Does it work?
8. Requirements of test data
• Possibility to review test data during code review in pull requests
• Test data has to be stored per table
• Test data needs to be flexible to be reused by multiple test contexts
• Test data needs to be updated together with new or changed functionality
Januar 20ASK TOM - Testing in PL/SQL - Jasmin Fluri 8
9. Test Management Example - DB Schema
Januar 20ASK TOM - Testing in PL/SQL - Jasmin Fluri 9
PERSON
ID
FIRSTNAME
LASTNAME
EMAIL
PERSON_ADDRESS
PERSON_ID
ADDRESS_ID
ADDRESS
ID
STREET
NR
ZIP
CITY
10. Testdata Package - Specification
CREATE OR REPLACE PACKAGE testdata AS
PROCEDURE insert_data_into_person;
PROCEDURE insert_data_into_address;
PROCEDURE insert_data_into_person_address;
PROCEDURE setup;
PROCEDURE teardown;
END testdata;
/
Januar 20ASK TOM - Testing in PL/SQL - Jasmin Fluri 10
11. Testdata Package - Body
CREATE OR REPLACE PACKAGE BODY testdata AS
PROCEDURE insert_data_into_person IS
BEGIN
INSERT INTO person VALUES (1,'Jasmin', 'Fluri', 'jasmin@schaltstelle.ch');
INSERT INTO person VALUES (2,'Gustav', 'Miller', 'gustav@homedepot.ca');
INSERT INTO person VALUES (3,'Mike', 'Heller', 'mike@heller.com');
END;
PROCEDURE insert_data_into_address IS
BEGIN
INSERT INTO address VALUES (11, 'Main Street', 33, '12345', 'Maine');
INSERT INTO address VALUES (12, 'Hyde Street', 9, '3014', 'Bern');
INSERT INTO address VALUES (13, 'Forest Street', 5, '44393', 'St. Lawrence');
END;
PROCEDURE insert_data_into_person_address IS
BEGIN
INSERT INTO person_address VALUES (1, 12);
INSERT INTO person_address VALUES (2, 13);
INSERT INTO person_address VALUES (3, 11);
INSERT INTO person_address VALUES (3, 12);
END;
...
Januar 20ASK TOM - Testing in PL/SQL - Jasmin Fluri 11
• Have as little test data as
possible
• Stored Procedures for
provisioning tables with test
data
• One procedure per table
12. Testdata Package – Setup and Teardown
...
PROCEDURE setup IS
BEGIN
insert_data_into_person();
insert_data_into_address();
insert_data_into_person_address();
COMMIT;
END;
PROCEDURE teardown IS
BEGIN
execute immediate 'truncate table person';
execute immediate 'truncate table address';
execute immediate 'truncate table person_address’;
COMMIT;
END;
END testdata;
/
Januar 20ASK TOM - Testing in PL/SQL - Jasmin Fluri 12
• Setup and Teardown functions
that group the tasks for the unit
tests
• Test data is treated as part of
the application code
13. Unit Test Package
Package test_package is
--%suite(testsuite)
--%beforeall(testdata.setup)
--%context(testcontext)
--%test(checks update of personal data)
procedure check_update_personal_data is
begin
-- test
end;
--%endcontext
--%afterall(testdata.teardown)
Januar 20ASK TOM - Testing in PL/SQL - Jasmin Fluri 13
Provisioning of tables with test data
Start of context
First test
End of context
Teardown of test data (truncate)
14. Summary
• Keep your test data where your application code is.
• Separate production logic from test logic and test logic from test data.
• Data collection, maintenance, automation and data management is the job of the
developer.
• Always create a fresh set of data according to the requirements of your test case.
• Sample test data should be consistent and represent the production data.
• Always test on boundaries
Januar 20ASK TOM - Testing in PL/SQL - Jasmin Fluri 14