Skip to content

Commit 9490e3b

Browse files
committed
refactoring documentation
1 parent 65c008f commit 9490e3b

File tree

1 file changed

+38
-45
lines changed

1 file changed

+38
-45
lines changed

docs/checks/db-access-in-ut.md

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,59 @@
44

55
### What is the Intent of the Check?
66

7-
This check scans test classes and its contents searching for any kind of explicit DB access within the tests. Since every DB access is considered to be a dependency, this should not be allowed in test code.
7+
The Check identifies database operations within the test classes.
88

99
### How does the check work?
1010

11-
Statements like: SELECT, EXEC SQL, COMMIT, COMMIT WORK, ROLLBACK, INSERT, DELETE, ALTER; UPDATE or READ TABLE accessing physical database tables (SAP Dictionary Tables) are detected and presented. However, the Check acts differently according to the RISK LEVEL classification of the test class under evaluation. So that:
12-
13-
· RISK LEVEL HARMLESS or missing RISK LEVEL classification --> These DB Operation will be forbidden/reported: COMMIT, DELETE, INSERT, MODIFY, ROLLBACK, SELECT and UPDATE statements on persistent DDIC tables;
14-
15-
· RISK LEVEL DANGEROUS/CRITICAL --> UPDATE, MODIFY DELETE, COMMIT, ROLLBACK operation(s) on persistent DDIC tables will be forbidden/reported.
11+
The Check behaves differently depending on the test class classification (`RISK LEVEL`) and automatically exempts the finding if a known test framework is find.
1612

17-
REMARK: For more details on RISK LEVEL classification, please refer to the last two sections of this page or follow this path: SE24 --> <Open any Test Class> --> Properties --> Highest Duration Category / Highest Allowed Risk --> PRESS F1.
13+
#### 1. `RISK LEVEL HARMLESS` or not set
1814

19-
Besides, test classes having AMDP (this happens only in productive mode) are already excluded from the scope of the check (in other words, exempted) since the Check is only applicable on test-code. Again, data access should be always mocked. For the mocking, one of these alternatives should be used:
15+
The Check reports:
16+
* `SELECT`
17+
* `INSERT`
18+
* `UPDATE`
19+
* `MODIFY`
20+
* `DELETE`
21+
* `ROLLBACK`
22+
* `COMMIT`
23+
* `ALTER`
2024

21-
· OSQL Test Framework; and/or
25+
#### 2. `RISK LEVEL DANGEROUS` or `RISK LEVEL CRITICAL`
2226

23-
· CDS Test Framework; and/or
27+
The Check reports:
28+
* `UPDATE`
29+
* `MODIFY`
30+
* `DELETE`
31+
* `ALTER`
2432

25-
· Test-Double Framework (isolating the database access in classes an mocking them via TDF).
26-
27-
Ideally, there has to be a data access class for every (customizing, master, or transactional) database table. That one shall be tested with one of the mocking frameworks listed above. All backend code layer above this boundary shall be tested with object-oriented test doubles according to the test pyramid strategy. There may be still some non-isolated E2E tests across systems, but these shall not test functionality, rather focus on connectivity, networking and things like that.
33+
In short, the Check allows:
34+
* `SELECT`
35+
* `INSERT`
36+
* `COMMIT`
37+
* `ROLLBACK`
38+
39+
#### 3. Test Frameworks
40+
41+
The Check identifies if the test class uses one of the following objects internally and exempts the finding automatically.
42+
43+
The relevant objects for the automatic exemption are:
44+
* `IF_OSQL_TEST_ENVIRONMENT`
45+
* `CL_OSQL_TEST_ENVIRONMENT`
46+
* `IF_CDS_TEST_ENVIRONMENT`
47+
* `CL_CDS_TEST_ENVIRONMENT`
2848

2949
### How to solve the issue?
3050

31-
The solution is to mock these DB accesses with a proper dependency isolation technique.
51+
Please, isolate the database dependency using one of the below frameworks:
52+
* [ABAP SQL Test Double Framework](https://help.sap.com/viewer/c238d694b825421f940829321ffa326a/1809.000/en-US/8562b437073d4b9c93078c45f7a64f21.html)
53+
* [ABAP CDS Test Double Framework](https://help.sap.com/viewer/5371047f1273405bb46725a417f95433/Cloud/en-US/8562b437073d4b9c93078c45f7a64f21.html)
3254

3355
### What to do in case of exception?
3456

3557
In special cases, it is possible to suppress a finding by using the pseudo comment `"#EC DB_ACCESS_UT`.
3658
The pseudo comment must be placed right after the DB access statement.
3759

38-
### Example
39-
40-
```abap
41-
SELECT XXXXX. "#EC DB_ACCESS_UT
60+
```ABAP
61+
SELECT * FROM tadir INTO TABLE @DATA(entries). "#EC DB_ACCESS_UT
4262
```
43-
### How to set the "Risk Level"
44-
Definition: The RISK LEVEL describes the effects that a test can have on the data security of the system.
45-
The RISK LEVEL is assigned by the following extension to the CLASS DEFINITION statement of a test class:
46-
47-
• RISK LEVEL CRITICAL (default) - such as changes to system settings or Customizing;
48-
49-
• RISK LEVEL DANGEROUS - such as changes to persistent data;
50-
51-
• RISK LEVEL HARMLESS - no effects on persistent data or system settings.
52-
53-
54-
### How to set the "Duration"
55-
Definition: The DURATION property allows you to define an expected runtime for all methods of a test class.
56-
57-
At runtime the cumulated duration of all test methods is measured. If the actual duration exceeds the expectation ABAP Unit will raise an alert.
58-
Please not the cumulated duration contains also system activities as program compilation. In case of doubts it is recommend to choose the higher category.
59-
60-
Maintenance: Use on of the following key words in the CLASS DEFINITION statement of a test class to specify the duration category:
61-
62-
• DURATION SHORT (default) - within the blink of an eye ( < 10 seconds );
63-
64-
• DURATION MEDIUM - take a sip of tea ( < 2 minutes );
65-
66-
• DURATION LONG - get another cup.
67-
68-
Purpose: Unit tests consist of source code and can contain errors. It is easy to recognize syntax errors and runtime errors, but endless loops are more difficult to detect.
69-
With help of this classification of the duration the test runner is able to detect these situations and cancel the execution.

0 commit comments

Comments
 (0)