You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -997,9 +997,9 @@ In most of the cases, the code to be tested is consisting of PLSQL packages cont
997
997
When creating test suites, it's quite common to maintain `one to one` relationship between test suite packages and tested code.
998
998
999
999
When it comes to test procedures themselves, it is best practice to have one test procedure for one tested behavior of the code that is tested.
1000
-
The relationship between test procedure and tested procedure/function will be therefore `many to one` in most of the cases.
1000
+
The relationship between test procedure and tested code will be therefore `many to one` or `many to many` in most of the cases.
1001
1001
1002
-
With this comes a challenge. How to group tests, related to one tested procedure, so that it is obvious that they relate to the same code.
1002
+
With this comes a challenge. How to group tests, related to one tested behavior, so that it is obvious that they relate to the same thing.
1003
1003
1004
1004
This is where utPLSQL contexts come handy.
1005
1005
@@ -1008,18 +1008,22 @@ Contexts allow for creating sub-suites within a suite package and they allow for
1008
1008
In essence, context behaves like a suite within a suite.
1009
1009
1010
1010
Context have following characteristics:
1011
-
- start with the `--%context` annotation and ends with `--%endcontext`
1012
-
- can have a name provided as parameter for example `--%context(remove_rooms_by_name)`
1013
-
- when no name is provided for context, the context is names `context_N` where `N` is the number of the context in suite
1014
-
- can have their own `--%beforeall`, `--%beforeeach`, `--%afterall` and `--%aftereach` procedures
1015
-
-`--%beforeall`, `--%beforeeach`, `--%afterall` and `--%aftereach` procedures defined at suite level, propagate to context
1016
-
- test suite package can have multiple contexts in it
1017
-
- contexts cannot be nested
1018
-
1011
+
- context starts with the `--%context` annotation and ends with `--%endcontext`
1012
+
- can have a name provided as parameter for example `--%context(remove_rooms_by_name)`. This is different than with `suite` and `test` annotations, where name is taken from test `package/procedure`
1013
+
- when no name is provided for context, the context is named `context_N` where `N` is the number of the context in suite or parent context
1014
+
- context name must be unique within it's parent (suite or parent context)
1015
+
- if context name is not unique within it's parent, context and it's entire content is excluded from execution
1016
+
- context name should not contain spaces or special characters
1017
+
- context name cannot contain a `.` (hard stop) character
1018
+
- contexts can be nested, so a context can be nested within another context
1019
+
- suite/context can have multiple nested sibling contexts in it
1020
+
- contexts can have their own `--%beforeall`, `--%beforeeach`, `--%afterall` and `--%aftereach` procedures
1021
+
-`--%beforeall`, `--%beforeeach`, `--%afterall` and `--%aftereach` procedures defined at ancestor level, propagate to context
1022
+
- if `--%endcontext` is missing for a context, the context spans to the end of package specification
1019
1023
1020
1024
The below example illustrates usage of `--%context` for separating tests for individual procedures of package.
1021
1025
1022
-
Tested tables and code
1026
+
Sample tables and code
1023
1027
```sql
1024
1028
createtablerooms (
1025
1029
room_key numberprimary key,
@@ -1078,8 +1082,8 @@ end;
1078
1082
1079
1083
Below test suite defines:
1080
1084
-`--%beforeall` outside of context, that will be executed before all tests
1081
-
-`--%context(remove_rooms_by_name)` to group tests for `remove_rooms_by_name`procedure
1082
-
-`--%context(add_rooms_content)` to group tests for `add_rooms_content`procedure
1085
+
-`--%context(remove_rooms_by_name)` to group tests related to `remove_rooms_by_name`functionality
1086
+
-`--%context(add_rooms_content)` to group tests related to `add_rooms_content`functionality
1083
1087
1084
1088
```sql
1085
1089
create or replace package test_rooms_management is
@@ -1103,7 +1107,6 @@ create or replace package test_rooms_management is
1103
1107
1104
1108
--%endcontext
1105
1109
1106
-
1107
1110
--%context(add_rooms_content)
1108
1111
--%displayname(Add content to a room)
1109
1112
@@ -1221,6 +1224,93 @@ Finished in .035261 seconds
Example of nested contexts test suite specification.
1228
+
*Source - [slide 145](https://www.slideshare.net/Kevlin/structure-and-interpretation-of-test-cases/145?src=clipshare) of Structure and Interpretation of Test Cases by Kevlin Henney*
1229
+
1230
+
```sql
1231
+
create or replace package queue_spec as
1232
+
--%suite(Queue specification)
1233
+
1234
+
--%context(a_new_queue)
1235
+
--%displayname(A new queue)
1236
+
1237
+
--%test(Is empty)
1238
+
procedure is_empty;
1239
+
--%test(Preserves positive bounding capacity)
1240
+
procedure positive_bounding_capacity;
1241
+
--%test(Cannot be created with non positive bounding capacity)
1242
+
procedure non_positive_bounding_cap;
1243
+
--%endcontext
1244
+
--%context(an_empty_queue)
1245
+
--%displayname(An empty queue)
1246
+
1247
+
--%test(Dequeues an empty value)
1248
+
procedure deq_empty_value;
1249
+
--%test(Remains empty when null enqueued)
1250
+
procedure empty_with_null_enq;
1251
+
--%test(Becomes non empty when non null value enqueued)
1252
+
procedure non_empty_after_enq;
1253
+
--%endcontext
1254
+
--%context(a_non_empty_queue)
1255
+
--%displayname(A non empty queue)
1256
+
1257
+
--%context(that_is_not_full)
1258
+
--%displayname(that is not full)
1259
+
1260
+
--%test(Becomes longer when non null value enqueued)
1261
+
procedure grow_on_enq_non_null;
1262
+
--%test(Becomes full when enqueued up to capacity)
1263
+
procedure full_on_enq_to_cap;
1264
+
--%endcontext
1265
+
--%context(that_is_full)
1266
+
--%displayname(That is full)
1267
+
1268
+
--%test(Ignores further enqueued values)
1269
+
procedure full_ignore_enq;
1270
+
--%test(Becomes non full when dequeued)
1271
+
procedure non_full_on_deq;
1272
+
--%endcontext
1273
+
1274
+
--%test(Dequeues values in order enqueued)
1275
+
procedure dequeue_ordered;
1276
+
--%test(Remains unchanged when null enqueued)
1277
+
procedure no_change_on_null_enq;
1278
+
--%endcontext
1279
+
end;
1280
+
```
1281
+
1282
+
1283
+
When such specification gets executed `ut.run('queue_spec'')` (without body created) you will see the nesting of tests within contexts.
1284
+
```
1285
+
Queue specification
1286
+
An empty queue
1287
+
Dequeues an empty value [.014 sec] (FAILED - 1)
1288
+
Remains empty when null enqueued [.004 sec] (FAILED - 2)
1289
+
Becomes non empty when non null value enqueued [.005 sec] (FAILED - 3)
1290
+
A non empty queue
1291
+
that is not full
1292
+
Becomes longer when non null value enqueued [.005 sec] (FAILED - 4)
1293
+
Becomes full when enqueued up to capacity [.005 sec] (FAILED - 5)
1294
+
That is full
1295
+
Ignores further enqueued values [.004 sec] (FAILED - 6)
1296
+
Becomes non full when dequeued [.005 sec] (FAILED - 7)
1297
+
Dequeues values in order enqueued [.006 sec] (FAILED - 8)
1298
+
Remains unchanged when null enqueued [.004 sec] (FAILED - 9)
'%Invalid value "'||l_bad_name||'" for context name. The name cannot contain "." (hard stop) character. Context name ignored and fallback to auto-name "context_1"%'
1042
+
);
1043
+
ut.expect(l_actual).to_be_like(
1044
+
'<ROWSET><ROW>'||
1045
+
'<UT_LOGICAL_SUITE>' ||
1046
+
'%<ITEMS>' ||
1047
+
'<UT_SUITE_ITEM>' ||
1048
+
'%<NAME>context_1</NAME><DESCRIPTION>Context with invalid name. Should fail</DESCRIPTION><PATH>some_package.context_1</PATH>' ||
0 commit comments