-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathtestfunc.html
More file actions
116 lines (101 loc) · 2.96 KB
/
testfunc.html
File metadata and controls
116 lines (101 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<!-- Begin utPLSQL Body -->
<!-- $Id$ -->
<h1>Test a Function</h1>
<p>
As with the procedure, there are a couple of scenarios to consider:
</p>
<ul>
<li>
The <a href="#ScalarFunction">function returns a scalar value</a> (number,
date, string, Boolean). In this case, you can embed your call to the function
directly inside a call to a uAssert assertion program, making your test
procedure very concise and easy to write.
</li>
<li>
The function returns a non-scalar value,
such as an object or a collection. In this case, you will need to call
the function and then evaluate the contents of the returned structure.
</li>
<li>
A third situation to consider is that your function returns a value, but
it also takes a number of other actions, the success of which is not reflected
in the returned value. Fully testing such a function (one with "side effects")
can be very difficult, since you must test for a variety of conditions.
</li>
</ul>
<h3><a name="ScalarFunction"></a>Testing a Scalar Function</h3>
<p>
First, a test of a function returning a scalar value. Consider the following
packaged function:
</p>
<pre>
/*file str.pks and str.pkb */
CREATE OR REPLACE PACKAGE str
IS
FUNCTION betwn (
string_in IN VARCHAR2,
start_in IN PLS_INTEGER,
end_in IN PLS_INTEGER
)
RETURN VARCHAR2;
END str;
/
</pre>
<p>
The str.betwn function returns the sub-string of a string_in that is found
between the start and end locations specified by start_in and end_in.
</p>
<p>
So...time to test! I <a href="utgen.html">generate a test package</a>
and then modify the unit test procedure to check for various conditions:
</p>
<pre>
/*file ut_str.pkb */
CREATE OR REPLACE PACKAGE BODY ut_str
IS
PROCEDURE ut_setup
IS
BEGIN
NULL;
END;
PROCEDURE ut_teardown
IS
BEGIN
NULL;
END;
-- For each program to test...
PROCEDURE ut_betwn IS
BEGIN
utAssert.eq (
'Typical Valid Usage',
str.betwn ('this is a string', 3, 7),
'is is'
);
utAssert.eq (
'Test Negative Start',
str.betwn ('this is a string', -3, 7),
'ing'
);
utAssert.isNULL (
'Start bigger than end',
str.betwn ('this is a string', 3, 1)
);
END ut_betwn;
END ut_str;
/
</pre>
<p>
As you can see, my calls to str.betwn are embedded right within calls to
utAssert.eq and utAssert.isNULL, making my test code compact.
</p>
<!-- End utPLSQL Body -->
</body>
</html>