-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtestfunc.html
More file actions
148 lines (126 loc) · 4.84 KB
/
Copy pathtestfunc.html
File metadata and controls
148 lines (126 loc) · 4.84 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- WARNING! This file is generated. -->
<!-- To alter documentation, edit files in src directory -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test a Function</title>
<link rel="stylesheet" href="utplsql.css" type="text/css" />
<meta name="keywords" content="utPLSQL, PL\SQL, Unit Testing, Framework, Oracle" />
<meta name="description" content="Unit Testing PL\SQL" />
<meta name="title" content="Test a Function" />
<meta name="author" content="Steven Feuerstein, Chris Rimmer, Patrick Barel and the utPLSQL Project" />
<meta name="copyright" content="(C) 2000-2005, 2014-2016 Steven Feuerstein, Chris Rimmer, Patrick Barel and the utPLSQL Project" />
</head>
<body>
<div class="purple_bar"><a href="index.html"><img src="utplsql.jpg" alt="utPLSQL logo" /></a></div>
<p>[ <a href="index.html">Home</a>
| <a href="started.html">Getting Started</a>
| <a href="buildpack.html">Build Test Packages</a>
| <a href="examples.html">Examples</a>
| <a href="userguide.html">User Guide</a>
| <a href="release.html">Release Notes</a>
| <a href="map.html">Document Map</a> ]</p>
<p><a href="testproc.html">< Previous Section: Test a Procedure</a> | <a href="testapi.html">Next Section: Test an Entire Package API ></a></p>
<!-- 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 -->
<p><a href="testproc.html">< Previous Section: Test a Procedure</a> | <a href="testapi.html">Next Section: Test an Entire Package API ></a></p>
<div class="purple_bar"><a href="index.html"><img src="utplsql.jpg" alt="utPLSQL logo" /></a></div>
<p>
<a href="http://validator.w3.org/check?uri=referer">
<img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Strict" height="31" width="88" />
</a>
</p>
<p class="copyright">Copyright © 2000-2005, 2014-2016 <a href="mailto:steven@stevenfeuerstein.com">Steven Feuerstein</a>, <a href="mailto:c@24.org.uk">Chris Rimmer</a>, <a href="mailto:pbarel@vda.nl">Patrick Barel</a> and the utPLSQL Project. All rights reserved</p>
</body>
</html>