/usr/share/pyshared/schooltool/testing/README.txt is in python-schooltool 1:2.1.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | ==========================
SchoolTool Testing Support
==========================
This package basically splits into two parts, an abstract test-setup registry
and some specific setup functions that are useful for many test setups.
The Test-Setup Registry
-----------------------
The test-setup registry was designed to de-centralize the creation of a
testing environment, allowing several independent packages to contribute to a
particular setup. The codebase is located in:
>>> from schooltool.testing import registry
In the module you will find a ``register()`` method that allows you to
register a function for a particular setup. The simplest case is to register a
function that has no arguments:
>>> def addOne():
... result.append(1)
You register the function as follows in the `SampleFill` setup registry:
>>> registry.register('SampleFill', addOne)
Now you execute the setup using the ``setup()`` method::
>>> result = []
>>> registry.setup('SampleFill')
>>> result
[1]
Now we can register more complex functions::
>>> def addTwo(number):
... result.append(number)
>>> registry.register('SampleFill', addTwo, 2)
>>> def addThree(number=None):
... result.append(number)
>>> registry.register('SampleFill', addThree, number=3)
>>> def addFour(number1, number2=None):
... result.append(number1+number2)
>>> registry.register('SampleFill', addFour, 3, number2=1)
And here is the result::
>>> result = []
>>> registry.setup('SampleFill')
>>> result
[1, 2, 3, 4]
Note that the order of registration is preserved, so if you can control the
order of registration, one setup step could depnd on a previous one. However,
this is hard to accomplish for generic development platforms and it is thus
not recommended to rely on the order of the setup steps.
While this functionality in itself is alsready pretty powerful, it does not
cover all of our required use cases. Oftentimes we want to be able to
"decorate" an object using several setup steps. Let's say, we have the
following containerish object::
>>> class Container(object):
...
... def __init__(self):
... self.data = []
...
... def add(self, entry):
... self.data.append(entry)
We now want our setup functions to fill this container with some initial
values. Clearly, the above method does not work here anymore, since we do not
have the container instance available when creating and registering the setup
step function. Here are a couple of functions that we would like to help with
the setup::
>>> def addOneToContainer(container):
... container.add(1)
>>> registry.register('ContainerValues', addOneToContainer)
>>> def addTwoToContainer(container, number=None):
... container.add(number)
>>> registry.register('ContainerValues', addTwoToContainer, number=2)
But how do we pass in the container? The ``setup()`` method allows you to
specify additional positional and keyword arguments. The positional arguments
passed via the ``setup()`` are *appended* to the original ones. The additional
keyword arguments are merged (updated) into the original keyword arguments. ::
>>> container = Container()
>>> registry.setup('ContainerValues', container)
>>> container.data
[1, 2]
>>> container = Container()
>>> registry.setup('ContainerValues', container=container)
>>> container.data
[1, 2]
Note: As you might have already noticed, every test-setup registry has its own
semantics and functions of a particular registry often have the same or
similar signatures.
As syntactic sugar, a setup function will be registered for all registries of
the form ``setup<registry name>``::
>>> container = Container()
>>> registry.setupContainerValues(container)
>>> container.data
[1, 2]
HTML Analyzation Tools
----------------------
There is a set of helpful analyzation tools available.
>>> from schooltool.testing import analyze
They are designed to ease the inspection of HTML and other testing output.
Pick an Element using an XPath expression
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Often you are only interested in a particular element or text. The
``queryHTML`` method allows you to specify an XPath query to pick out a
particular note. A list of all found nodes will be returned. The nodes will be
returned as serialized strings::
>>> html = '''
... <html>
... <head>
... <title>My Page</title>
... </head>
... <body>
... <h1>This is my page!</h1>
... </body>
... </html>
... '''
>>> print analyze.queryHTML('/html/body/h1', html)[0]
<h1>This is my page!</h1>
It works also with XHTML compliant documents::
>>> html = '''
... <html xmlns="http://www.w3.org/1999/xhtml">
... <head>
... <title>My Page</title>
... </head>
... <body>
... <h1>This is my page!</h1>
... </body>
... </html>
... '''
>>> print analyze.queryHTML('/html/body/h1', html)[0]
<h1>This is my page!</h1>
``printQuery`` makes this even easier, by printing all nodes::
>>> analyze.printQuery('/html/body/h1', html)
<h1>This is my page!</h1>
>>> html = '''
... <html>
... <body>
... <ul>
... <li>One</li>
... <li>Two</li>
... </ul>
... </body>
... </html>
... '''
>>> analyze.printQuery('//li', html)
<li>One</li>
<li>Two</li>
``printQuery`` skips empty matches::
>>> html = '''
... <ul>
... <li>One</li>
... <li>Two</li>
... <li>
... <b>Three</b>
... </li>
... <li>Four</li>
... </ul>
... '''
>>> analyze.printQuery('//li/text()', html)
One
Two
Four
Reportlab PDF story testing
---------------------------
Schooltool PDF reports utilize Reportlab platypus module. A report is
built from a list of platypus flowables known as as 'story'.
Let's build a short pdf story::
>>> from reportlab.lib.styles import ParagraphStyle
>>> from reportlab.platypus.paragraph import Paragraph
>>> from reportlab.platypus.flowables import PageBreak
>>> style = ParagraphStyle(name='Test', fontName='Times-Roman')
>>> story = [
... Paragraph('Hello world', style),
... PageBreak(),
... Paragraph('A new page', style)]
There are several helpers for testing the stories.
>>> from schooltool.testing.pdf import StoryXML
The tool aims to build a human readable XML representation of the
story. There is a helper which prints the formatted XML::
>>> StoryXML(story).printXML()
<story>
<Paragraph>Hello world</Paragraph>
<PageBreak/>
<Paragraph>A new page</Paragraph>
</story>
As with HTML analyzation tools, there are helpers for XPath queries::
>>> parser = StoryXML(story)
>>> parser.printXML('//Paragraph')
<Paragraph>Hello world</Paragraph>
<Paragraph>A new page</Paragraph>
>>> parser.query('//Paragraph')
['<Paragraph>Hello world</Paragraph>',
'<Paragraph>A new page</Paragraph>']
If these helpers are not sufficient, we can use XML document directly::
>>> parser.document
<...ElementTree object ...>
>>> for child in parser.document.getroot().iterchildren():
... if child.text:
... print child.text
Hello world
A new page
``StoryXML`` helpers also work on single platypus flowables::
>>> flowable = Paragraph('Some text', style)
>>> StoryXML(flowable).printXML()
<story>
<Paragraph>Some text</Paragraph>
</story>
ZCML execution wrapper
----------------------
`ZCMLWrapper` is a simple tool for convenient execution of ZCML in your tests.
>>> from schooltool.testing.setup import ZCMLWrapper
>>> zcml = ZCMLWrapper()
Let's include a ZCML file that defines a new directive::
>>> zcml.include('schooltool.testing.tests',
... file='echodirective.zcml')
The new directive is under a namespace, so we cannot access it directly::
>>> zcml.string('<echo message="Boo" />')
Traceback (most recent call last):
...
ZopeXMLConfigurationError: File "<string>", line 2.0
ConfigurationError: ('Unknown directive', None, u'echo')
Note that line number is a bit off in string execution, this happens
because the string is wrapped in ``<configure>...</configure>``.
So, lets set the default namespace and execute again::
>>> zcml.setUp(namespaces={'': 'http://schooltool.org/testing/tests'})
>>> zcml.string('<echo message="Boo" />')
Executing echo: Boo
You can use prefixed namespaces like this::
>>> zcml.setUp(namespaces={
... '': 'http://schooltool.org/testing/tests',
... 'test': 'http://schooltool.org/testing/tests'})
>>> zcml.string('<test:echo message="Boo"/>')
Executing echo: Boo
And you can even postpone ZCML action execution, if it's convenient
for your tests::
>>> zcml.auto_execute = False
>>> zcml.string('<echo message="First" echo_on_add="True"/>')
Adding ZCML action: ('echo', u'First')
>>> zcml.string('<echo message="Second" echo_on_add="True"/>')
Adding ZCML action: ('echo', u'Second')
>>> zcml.execute()
Executing echo: First
Executing echo: Second
Finally, each instance of the wrapper has it's own ``ConfigurationMachine``::
>>> zcml.context
<zope.configuration.config.ConfigurationMachine ...>
>>> other = ZCMLWrapper()
>>> other.setUp(namespaces={'': 'http://schooltool.org/testing/tests'})
>>> other.string('<echo message="Boo" />')
Traceback (most recent call last):
...
ZopeXMLConfigurationError: File "<string>", line 2.0
ConfigurationError: ('Unknown directive', ..., u'echo')
Let's include the directive again, but this time also show that we can also
pass module instead of it's dotted name::
>>> import schooltool.testing.tests as the_tests
>>> other.include(the_tests, file='echodirective.zcml')
>>> other.string('<echo message="Boo" />')
Executing echo: Boo
>>> other.context is not zcml.context
True
Fake modules and their globals
------------------------------
Sometimes it is useful to define a class or method inside a test,
replacing actual counterpart in some module for the test only.
>>> from schooltool.testing import mock
>>> @mock.module('schooltool.imaginarium')
... def greet():
... print 'Hello world!'
A fake module was created.
>>> from schooltool import imaginarium
>>> print imaginarium
<module 'schooltool.imaginarium' (built-in)>
With the injected function.
>>> for a in sorted(dir(imaginarium)):
... print '%s: %s' % (a, getattr(imaginarium, a))
__doc__: None
__name__: schooltool.imaginarium
__package__: None
greet: <function greet at ...>
>>> imaginarium.greet()
Hello world!
We can mock classes the same way too.
>>> @mock.module(imaginarium)
... class Pond(object):
... frog = 'Kermit'
>>> pond = imaginarium.Pond()
>>> print pond.frog
Kermit
Note that some attributes of mocked objects are not updated:
>>> print imaginarium.greet.__module__
None
>>> print imaginarium.Pond.__module__
__builtin__
Oh, and we can set global variables too.
>>> mock.fake_global(imaginarium, 'the_answer', 42)
>>> imaginarium.the_answer
42
After the test is finished, fake modules will be removed.
>>> mock.restoreModules()
>>> from schooltool.imaginarium import foo
Traceback (most recent call last):
...
ImportError: No module named imaginarium
|