@@ -15,22 +15,83 @@ Custom Assertions
15
15
=================
16
16
17
17
18
- assertText
19
- ----------
18
+ assertText()
19
+ ------------
20
20
21
21
.. code ::
22
22
23
- assertText(actual_text, expected_text , strip=True)
23
+ self. assertText(expected_text, actual_text , strip=True)
24
24
25
25
26
- Currently, this is mostly a wrapper for assertEqual(), which prints full
27
- values to console on mismatch.
26
+ This is effectively a wrapper for assertEqual(), which is meant specifically
27
+ for comparing two bits of text.
28
+ The difference is that on failure, this attempts to print full values to
29
+ console on mismatch (``assertEqual() `` will truncate if values are too long).
28
30
29
- In the future, this may be updated to have more useful AssertionFailure
30
- output, particularly for long values .
31
+ Furthermore if ` Colorama < https://pypi.org/project/colorama/ >`_ is installed,
32
+ then text will be colored according to what characters did and did not match .
31
33
32
- :param actual_text: The value you wish to verify.
33
- :param expected_text: The value to compare against. These should match.
34
+
35
+ :param expected_text: The value you expect to find.
36
+ :param actual_text: The variable value you're testing against, to match against
37
+ ``expected_text ``.
38
+ :param strip: A boolean indicating if white space characters should be stripped
39
+ from both comparison values, prior to comparing.
40
+ Set False to skip stripping.
41
+
42
+
43
+ assertTextStartsWith()
44
+ ----------------------
45
+
46
+ .. code ::
47
+
48
+ self.assertTextStartsWith(expected_text, actual_text, strip=True)
49
+
50
+ Similar to above
51
+ :ref: `test_cases/base_test_case:assertText() `, except it only cares about
52
+ matching from the start of the comparison strings.
53
+
54
+ If ``expected_text `` is shorter than ``actual_text ``, then this assertion
55
+ only matches equal to the total characters found in ``expected_text ``.
56
+
57
+ If ``expected_text `` is longer than ``actual_text ``, then raise an error
58
+ for the missing characters, but still attempt to compare full strings to
59
+ indicate if all other values match or not.
60
+
61
+
62
+ :param expected_text: The value you expect to find.
63
+ :param actual_text: The variable value you're testing against, to match against
64
+ ``expected_text ``.
65
+ :param strip: A boolean indicating if white space characters should be stripped
66
+ from both comparison values, prior to comparing.
67
+ Set False to skip stripping.
68
+
69
+
70
+ assertTextEndsWith()
71
+ ----------------------
72
+
73
+ .. code ::
74
+
75
+ self.assertTextEndsWith(expected_text, actual_text, strip=True)
76
+
77
+ Similar to above
78
+ :ref: `test_cases/base_test_case:assertText() `, except it only cares about
79
+ matching from the end of the comparison strings.
80
+
81
+ If ``expected_text `` is shorter than ``actual_text ``, then this assertion
82
+ only matches equal to the total characters found in ``expected_text ``.
83
+
84
+ If ``expected_text `` is longer than ``actual_text ``, then raise an error
85
+ for the missing characters, but still attempt to compare full strings to
86
+ indicate if all other values match or not.
87
+
88
+
89
+ :param expected_text: The value you expect to find.
90
+ :param actual_text: The variable value you're testing against, to match against
91
+ ``expected_text ``.
92
+ :param strip: A boolean indicating if white space characters should be stripped
93
+ from both comparison values, prior to comparing.
94
+ Set False to skip stripping.
34
95
35
96
36
97
Helper Functions
@@ -41,81 +102,177 @@ Reminder that the ``BaseTestCase`` class is meant to be minimalistic. So most
41
102
types of input, and then return the expected value.
42
103
43
104
44
- get_user
45
- --------
105
+ User Helper Functions
106
+ ---------------------
107
+
108
+ get_user()
109
+ ^^^^^^^^^^
46
110
47
111
.. code ::
48
112
49
- get_user(user, password='password')
113
+ self. get_user(user, password='password', extra_usergen_kwargs=**kwargs )
50
114
51
115
Helper function to obtain a given User object.
52
116
53
- Treats the provided value as the `username ` field. Returns the User object that
54
- matches. If no such User exists in the database yet, then a new one is first
55
- created.
117
+ Treats the provided value as the field defined in settings as the
118
+ :ref: `user model identifier <configuration/auth:USER_MODEL_IDENTIFIER >`.
119
+
120
+ Returns the User object that matches.
121
+ If no such User exists in the database yet, then a new one is first created.
56
122
57
123
For testing purposes, also makes sure the provided password is assigned to the
58
- user, and then includes this raw value as an attribute on the returned object.
124
+ test user object, and then includes this raw value as an attribute on the
125
+ returned object, found as ``user_object.password ``.
126
+
59
127
128
+ :param user: Identifier to use to attempt to get user with.
60
129
:param password: The password to assign this user. On the returned User
61
130
object, the raw password value can be accessed via a
62
131
provided ``unhashed_password `` field.
132
+ :param extra_usergen_kwargs: Optional extra kwargs to pass into the
133
+ ``get_user_model().objects.create_user() `` function.
134
+ If your project has custom logic for the
135
+ ``create_user() `` function, then you can use this to
136
+ pass additional values in.
63
137
64
138
:return: Found User object.
65
139
66
140
67
- add_user_permission
68
- -------------------
141
+ add_user_permission()
142
+ ^^^^^^^^^^^^^^^^^^^^^
69
143
70
144
.. code ::
71
145
72
- add_user_permission(user_permission, user='test_user')
146
+ self. add_user_permission(user_permission, user='test_user')
73
147
74
148
Helper function to add
75
149
`permissions <https://docs.djangoproject.com/en/dev/topics/auth/default/#permissions-and-authorization >`_
76
150
to a given User.
77
151
152
+
78
153
:param user_permission: Permission object, or name of permission object, to
79
154
add to User.
80
- :parm user: User to add permission to. Defaults to ``test_user ``.
155
+ :parm user: User to add permission to.
156
+ Can take in either a literal user object, or the identifier of a
157
+ user.
158
+ Defaults to ``test_user ``.
81
159
82
160
:return: Updated User object.
83
161
84
162
85
- add_user_group
86
- --------------
163
+ add_user_group()
164
+ ^^^^^^^^^^^^^^^^
87
165
88
166
.. code ::
89
167
90
- add_user_group(user_group, user='test_user')
168
+ self. add_user_group(user_group, user='test_user')
91
169
92
170
Helper function to add
93
171
`groups <https://docs.djangoproject.com/en/dev/topics/auth/default/#groups >`_
94
172
to a given User.
95
173
174
+
96
175
:param user_group: Group object, or name of group object, to add to User.
97
- :param user: User to add group to. Defaults to ``test_user ``.
176
+ :param user: User to add group to.
177
+ Can take in either a literal user object, or the identifier of a
178
+ user.
179
+ Defaults to ``test_user ``.
98
180
99
181
:return: Updated User object.
100
182
101
183
102
- generate_get_url
103
- ----------------
184
+ Other Helper Functions
185
+ ----------------------
186
+
187
+ generate_get_url()
188
+ ^^^^^^^^^^^^^^^^^^
104
189
105
190
.. code ::
106
191
107
- generate_get_url(url=None, **kwargs)
192
+ self. generate_get_url(url=None, **kwargs)
108
193
109
194
Helper function to generate a full GET request URL.
110
195
111
- Note: If you're repeatedly accessing the same URL, you can define the value
112
- ```self.url` `` in the **BaseTestCase ** class.
113
-
114
196
Any provided kwargs are assumed to be
115
197
`URL Parameters <https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL#parameters >`_,
116
198
and are appended to the end of the URL accordingly.
117
199
200
+ .. note ::
201
+
202
+ If you're repeatedly accessing the same URL, you can define the value
203
+ ``self.url `` class variable.
204
+
205
+
118
206
:param url: The desired url string value to use as the
119
207
`URL path <https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL#path_to_resource >`_.
208
+ :param kwargs: A dictionary of key-value pairs, to be converted to Url
209
+ Parameters in the final url.
120
210
121
211
:return: The generated url string.
212
+
213
+
214
+ standardize_characters()
215
+ ^^^^^^^^^^^^^^^^^^^^^^^^
216
+
217
+ .. code ::
218
+
219
+ self.standardize_characters(value)
220
+
221
+ Standardizes characters in provided string.
222
+
223
+ For example, in HTML, the "greater than" symbol can be denoted in multiple
224
+ ways, such as ``> ``, ``> ``, ``> ``, or ``> ``.
225
+
226
+ This function takes all such known "equivalent" representations of various
227
+ characters, and reformats them to a single type for consistency.
228
+
229
+ Many of the assertions in this package use this function, in order to make
230
+ testing easier and more consistent. (In our above example, the programmer
231
+ probably doesn't care WHICH version of the "greater than" symbol is present.
232
+ They most likely would only care that one of the several representations is
233
+ present.)
234
+
235
+
236
+ :param value: A string value to convert to standardized characters.
237
+
238
+ :return: A string of more standardized characters.
239
+
240
+
241
+ standardize_whitespace()
242
+ ^^^^^^^^^^^^^^^^^^^^^^^^
243
+
244
+ .. code ::
245
+
246
+ self.standardize_whitespace(value)
247
+
248
+ Standardizes whitespace characters in provided string.
249
+
250
+ When possible, all repeating whitespace and whitespace-esque characters
251
+ (including newlines) are reduced down to a single space.
252
+
253
+
254
+ :param value: A string value to convert to standardized characters.
255
+
256
+ :return: A string of more standardized characters.
257
+
258
+
259
+ standardize_newlines()
260
+ ^^^^^^^^^^^^^^^^^^^^^^
261
+
262
+ .. code ::
263
+
264
+ self.standardize_newlines(value)
265
+
266
+ Standardizes newline/whitespace characters in provided string.
267
+
268
+ When possible, all repeating newlines are converted down to a single ``\n ``
269
+ character, and whitespace and whitespace-esque characters
270
+ (excluding newlines) are reduced down to a single space.
271
+
272
+ If multiple newline characters and space characters repeat in a row, then
273
+ that's reduced down to a single newline with up to one space on either side.
274
+
275
+
276
+ :param value: A string value to convert to standardized characters.
277
+
278
+ :return: A string of more standardized characters.
0 commit comments