| 1 |
.. _API Reference: |
| 2 |
|
| 3 |
API Reference |
| 4 |
============= |
| 5 |
|
| 6 |
|
| 7 |
Setup/Teardown |
| 8 |
-------------- |
| 9 |
|
| 10 |
You might be familiar with to how the :py:mod:`unittest` module |
| 11 |
suggests to `implement setup and teardown callbacks <https://docs.python.org/2/library/unittest.html#class-and-module-fixtures>`_ |
| 12 |
for your tests. |
| 13 |
|
| 14 |
But if you prefer to define test cases as functions and use a runner |
| 15 |
like `nose <https://nose.readthedocs.io/en/latest/>`_ then *sure* can |
| 16 |
help you define and activate modular fixtures. |
| 17 |
|
| 18 |
In *sure's* parlance, we call it a *Scenario* |
| 19 |
|
| 20 |
|
| 21 |
Example: Setup a Flask app for testing |
| 22 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 23 |
|
| 24 |
|
| 25 |
``my_flask_app.py`` |
| 26 |
................... |
| 27 |
|
| 28 |
.. code:: |
| 29 |
|
| 30 |
import json |
| 31 |
from flask import Response, Flask |
| 32 |
|
| 33 |
webapp = Flask(__name__) |
| 34 |
|
| 35 |
@webapp.route('/') |
| 36 |
def index(): |
| 37 |
data = json.dumps({'hello': 'world'}} |
| 38 |
return Response(data, headers={'Content-Type': 'application/json'}) |
| 39 |
|
| 40 |
|
| 41 |
``tests/scenarios.py`` |
| 42 |
...................... |
| 43 |
|
| 44 |
.. code:: python |
| 45 |
|
| 46 |
from sure import scenario |
| 47 |
from my_flask_app import webapp |
| 48 |
|
| 49 |
def prepare_webapp(context): |
| 50 |
context.server = webapp.test_client() |
| 51 |
|
| 52 |
web_scenario = scenario(prepare_webapp) |
| 53 |
|
| 54 |
|
| 55 |
``tests/test_webapp.py`` |
| 56 |
........................ |
| 57 |
|
| 58 |
.. code:: python |
| 59 |
|
| 60 |
import json |
| 61 |
from sure import scenario |
| 62 |
from tests.scenarios import web_scenario |
| 63 |
|
| 64 |
@web_scenario |
| 65 |
def test_hello_world(context): |
| 66 |
# Given that I GET / |
| 67 |
response = context.server.get('/') |
| 68 |
|
| 69 |
# Then it should have returned a successful json response |
| 70 |
response.headers.should.have.key('Content-Type').being.equal('application/json') |
| 71 |
response.status_code.should.equal(200) |
| 72 |
|
| 73 |
json.loads(response.data).should.equal({'hello': 'world'}) |
| 74 |
|
| 75 |
|
| 76 |
Example: Multiple Setup and Teardown functions |
| 77 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 78 |
|
| 79 |
|
| 80 |
``tests/scenarios.py`` |
| 81 |
...................... |
| 82 |
|
| 83 |
.. code:: python |
| 84 |
|
| 85 |
import os |
| 86 |
import shutil |
| 87 |
from sure import scenario |
| 88 |
|
| 89 |
def prepare_directories(context): |
| 90 |
context.root = os.path.dirname(os.path.abspath(__file__)) |
| 91 |
context.fixture_path = os.path.join(context.root, 'input_data') |
| 92 |
context.result_path = os.path.join(context.root, 'output_data') |
| 93 |
context.directories = [ |
| 94 |
context.fixture_path, |
| 95 |
context.result_path, |
| 96 |
] |
| 97 |
|
| 98 |
for path in context.directories: |
| 99 |
if os.path.isdir(path): |
| 100 |
shutil.rmtree(path) |
| 101 |
|
| 102 |
os.makedirs(path) |
| 103 |
|
| 104 |
|
| 105 |
def cleanup_directories(context): |
| 106 |
for path in context.directories: |
| 107 |
if os.path.isdir(path): |
| 108 |
shutil.rmtree(path) |
| 109 |
|
| 110 |
|
| 111 |
def create_10_dummy_hex_files(context): |
| 112 |
for index in range(10): |
| 113 |
filename = os.path.join(context.fixture_path, 'dummy-{}.hex'.format(index)) |
| 114 |
open(filename, 'wb').write(os.urandom(32).encode('hex')) |
| 115 |
|
| 116 |
|
| 117 |
dummy_files_scenario = scenario([create_directories, create_10_dummy_hex_files], [cleanup_directories]) |
| 118 |
|
| 119 |
``tests/test_filesystem.py`` |
| 120 |
........................ |
| 121 |
|
| 122 |
.. code:: python |
| 123 |
|
| 124 |
import os |
| 125 |
from tests.scenarios import dummy_files_scenario |
| 126 |
|
| 127 |
@dummy_files_scenario |
| 128 |
def test_files_exist(context): |
| 129 |
os.listdir(context.fixture_path).should.equal([ |
| 130 |
'dummy-0.hex', |
| 131 |
'dummy-1.hex', |
| 132 |
'dummy-2.hex', |
| 133 |
'dummy-3.hex', |
| 134 |
'dummy-4.hex', |
| 135 |
'dummy-5.hex', |
| 136 |
'dummy-6.hex', |
| 137 |
'dummy-7.hex', |
| 138 |
'dummy-8.hex', |
| 139 |
'dummy-9.hex', |
| 140 |
]) |
| 141 |
|
| 142 |
|
| 143 |
Number Equality |
| 144 |
--------------- |
| 145 |
|
| 146 |
``(2 + 2).should.equal(4)`` |
| 147 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 148 |
|
| 149 |
.. code:: python |
| 150 |
|
| 151 |
import sure |
| 152 |
|
| 153 |
(4).should.be.equal(2 + 2) |
| 154 |
(7.5).should.eql(3.5 + 4) |
| 155 |
(2).should.equal(8 / 4) |
| 156 |
|
| 157 |
(3).shouldnt.be.equal(5) |
| 158 |
|
| 159 |
``.equal(float, epsilon)`` |
| 160 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 161 |
|
| 162 |
.. code:: python |
| 163 |
|
| 164 |
import sure |
| 165 |
|
| 166 |
(4.242423).should.be.equal(4.242420, epsilon=0.000005) |
| 167 |
(4.01).should.be.eql(4.00, epsilon=0.01) |
| 168 |
(6.3699999).should.equal(6.37, epsilon=0.001) |
| 169 |
|
| 170 |
(4.242423).shouldnt.be.equal(4.249000, epsilon=0.000005) |
| 171 |
|
| 172 |
|
| 173 |
String Equality |
| 174 |
--------------- |
| 175 |
|
| 176 |
``.should_not.be.different_of(string)`` |
| 177 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 178 |
|
| 179 |
.. code:: python |
| 180 |
|
| 181 |
import sure |
| 182 |
|
| 183 |
XML1 = '''<root> |
| 184 |
<a-tag with-attribute="one">AND A VALUE</a-tag> |
| 185 |
</root>''' |
| 186 |
|
| 187 |
|
| 188 |
XML1.should_not.be.different_of(XML1) |
| 189 |
|
| 190 |
XML2 = '''<root> |
| 191 |
<a-tag with-attribute="two">AND A VALUE</a-tag> |
| 192 |
</root>''' |
| 193 |
|
| 194 |
XML2.should.be.different_of(XML1) |
| 195 |
|
| 196 |
this will give you and output like |
| 197 |
|
| 198 |
.. code:: bash |
| 199 |
|
| 200 |
Difference: |
| 201 |
|
| 202 |
<root> |
| 203 |
- <a-tag with-attribute="one">AND A VALUE</a-tag> |
| 204 |
? -- |
| 205 |
+ <a-tag with-attribute="two">AND A VALUE</a-tag> |
| 206 |
? ++ |
| 207 |
</root>''' |
| 208 |
|
| 209 |
``.should.equal("a string")`` |
| 210 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 211 |
|
| 212 |
.. code:: python |
| 213 |
|
| 214 |
"Awesome ASSERTIONS".lower().split().should.equal(['awesome', 'assertions']) |
| 215 |
|
| 216 |
|
| 217 |
String Similarity |
| 218 |
----------------- |
| 219 |
|
| 220 |
``.look_like()`` |
| 221 |
~~~~~~~~~~~~~~~~ |
| 222 |
|
| 223 |
.. code:: python |
| 224 |
|
| 225 |
|
| 226 |
""" |
| 227 |
|
| 228 |
THIS IS MY loose string |
| 229 |
""".should.look_like('this is my loose string') |
| 230 |
|
| 231 |
"""this one is different""".should_not.look_like('this is my loose string') |
| 232 |
|
| 233 |
|
| 234 |
Strings Matching Regular-Expressions |
| 235 |
------------------------------------ |
| 236 |
|
| 237 |
``should.match()`` |
| 238 |
~~~~~~~~~~~~~~~~~~ |
| 239 |
|
| 240 |
You can also use the modifiers: |
| 241 |
|
| 242 |
- `re.DEBUG <http://docs.python.org/2/library/re.html#re.DEBUG>`__ |
| 243 |
- `re.I and re.IGNORECASE <http://docs.python.org/2/library/re.html#re.IGNORECASE>`_ |
| 244 |
- `re.M and re.MULTILINE <http://docs.python.org/2/library/re.html#re.MULTILINE>`_ |
| 245 |
- `re.S re.DOTALL <http://docs.python.org/2/library/re.html#re.DOTALL>`_ |
| 246 |
- `re.U and re.UNICODE <http://docs.python.org/2/library/re.html#re.UNICODE>`_ |
| 247 |
- `re.X and re.VERBOSE <http://docs.python.org/2/library/re.html#re.VERBOSE>`_ |
| 248 |
|
| 249 |
.. code:: python |
| 250 |
|
| 251 |
import re |
| 252 |
|
| 253 |
"SOME STRING".should.match(r'some \w+', re.I) |
| 254 |
"FOO BAR CHUCK NORRIS".should_not.match(r'some \w+', re.M) |
| 255 |
|
| 256 |
|
| 257 |
Collections and Iterables |
| 258 |
------------------------- |
| 259 |
|
| 260 |
|
| 261 |
Works with: |
| 262 |
|
| 263 |
- Lists, Tuples, Sets |
| 264 |
- Dicts, OrderedDicts |
| 265 |
- Anything that implements ``__iter__()`` / ``next()`` |
| 266 |
|
| 267 |
|
| 268 |
``.equal({'a': 'collection'})`` |
| 269 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 270 |
|
| 271 |
.. code:: python |
| 272 |
|
| 273 |
{'foo': 'bar'}.should.equal({'foo': 'bar'}) |
| 274 |
{'foo': 'bar'}.should.eql({'foo': 'bar'}) |
| 275 |
{'foo': 'bar'}.must.be.equal({'foo': 'bar'}) |
| 276 |
|
| 277 |
|
| 278 |
``.contain()`` |
| 279 |
~~~~~~~~~~~~~~~~~~~~~ |
| 280 |
|
| 281 |
``expect(collection).to.contain(item)`` is a shorthand to |
| 282 |
``expect(item).to.be.within(collection)`` |
| 283 |
|
| 284 |
.. code:: python |
| 285 |
|
| 286 |
['1.2.5', '1.2.4'].should.contain('1.2.5') |
| 287 |
'1.2.4'].should.be.within(['1.2.5', '1.2.4']) |
| 288 |
|
| 289 |
# also works with strings |
| 290 |
"My bucket of text".should.contain('bucket') |
| 291 |
"life".should_not.contain('anger') |
| 292 |
'1.2.3'.should.contain('2') |
| 293 |
|
| 294 |
|
| 295 |
``.should.be.empty`` |
| 296 |
~~~~~~~~~~~~~~~~~~~~ |
| 297 |
|
| 298 |
.. code:: python |
| 299 |
|
| 300 |
|
| 301 |
[].should.be.empty |
| 302 |
{}.should.be.empty |
| 303 |
set().should.be.empty |
| 304 |
"".should.be.empty |
| 305 |
().should.be.empty |
| 306 |
range(0).should.be.empty |
| 307 |
|
| 308 |
## negate with: |
| 309 |
|
| 310 |
[1, 2, 3].shouldnt.be.empty |
| 311 |
"Dummy String".shouldnt.be.empty |
| 312 |
"Dummy String".should_not.be.empty |
| 313 |
|
| 314 |
|
| 315 |
``{number}.should.be.within(0, 10)`` |
| 316 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 317 |
|
| 318 |
asserts inclusive numeric range |
| 319 |
|
| 320 |
.. code:: python |
| 321 |
|
| 322 |
(1).should.be.within(0, 2) |
| 323 |
(5).should.be.within(0, 10) |
| 324 |
|
| 325 |
## negate with: |
| 326 |
|
| 327 |
(1).shouldnt.be.within(5, 6) |
| 328 |
|
| 329 |
|
| 330 |
``.be.within({iterable})`` |
| 331 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 332 |
|
| 333 |
asserts that a member is part of the iterable |
| 334 |
|
| 335 |
.. code:: python |
| 336 |
|
| 337 |
"g".should.be.within("gabriel") |
| 338 |
'name'.should.be.within({'name': 'Gabriel'}) |
| 339 |
'Lincoln'.should.be.within(['Lincoln', 'Gabriel']) |
| 340 |
|
| 341 |
## negate with: |
| 342 |
|
| 343 |
'Bug'.shouldnt.be.within(['Sure 1.0']) |
| 344 |
'Bug'.should_not.be.within(['Sure 1.0']) |
| 345 |
|
| 346 |
|
| 347 |
``.be.none`` |
| 348 |
~~~~~~~~~~~~ |
| 349 |
|
| 350 |
Assert whether an object is or not ``None`` |
| 351 |
|
| 352 |
.. code:: python |
| 353 |
|
| 354 |
|
| 355 |
value = None |
| 356 |
value.should.be.none |
| 357 |
None.should.be.none |
| 358 |
|
| 359 |
"".should_not.be.none |
| 360 |
(not None).should_not.be.none |
| 361 |
|
| 362 |
``.be.ok`` |
| 363 |
~~~~~~~~~~ |
| 364 |
|
| 365 |
Assert truthfulness: |
| 366 |
|
| 367 |
.. code:: python |
| 368 |
|
| 369 |
from sure import this |
| 370 |
|
| 371 |
True.should.be.ok |
| 372 |
'truthy string'.should.be.ok |
| 373 |
{'truthy': 'dictionary'}.should.be.ok |
| 374 |
|
| 375 |
And negate truthfulness: |
| 376 |
|
| 377 |
.. code:: python |
| 378 |
|
| 379 |
|
| 380 |
from sure import this |
| 381 |
|
| 382 |
False.shouldnt.be.ok |
| 383 |
''.should_not.be.ok |
| 384 |
{}.shouldnot.be.ok |
| 385 |
|
| 386 |
|
| 387 |
``.have.property()`` |
| 388 |
~~~~~~~~~~~~~~~~~~~~ |
| 389 |
|
| 390 |
.. code:: python |
| 391 |
|
| 392 |
|
| 393 |
class Basket(object): |
| 394 |
fruits = ["apple", "banana"] |
| 395 |
|
| 396 |
|
| 397 |
basket1 = Basket() |
| 398 |
|
| 399 |
basket1.should.have.property("fruits") |
| 400 |
|
| 401 |
|
| 402 |
``.have.property().being.*`` |
| 403 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 404 |
|
| 405 |
If the programmer calls ``have.property()`` it returns an assertion |
| 406 |
builder of the property if it exists, so that you can chain up |
| 407 |
assertions for the property value itself. |
| 408 |
|
| 409 |
.. code:: python |
| 410 |
|
| 411 |
class Basket(object): |
| 412 |
fruits = ["apple", "banana"] |
| 413 |
|
| 414 |
basket2 = Basket() |
| 415 |
basket2.should.have.property("fruits").which.should.be.equal(["apple", "banana"]) |
| 416 |
basket2.should.have.property("fruits").being.equal(["apple", "banana"]) |
| 417 |
basket2.should.have.property("fruits").with_value.equal(["apple", "banana"]) |
| 418 |
basket2.should.have.property("fruits").with_value.being.equal(["apple", "banana"]) |
| 419 |
|
| 420 |
``.have.key()`` |
| 421 |
~~~~~~~~~~~~~~~ |
| 422 |
|
| 423 |
.. code:: python |
| 424 |
|
| 425 |
basket3 = dict(fruits=["apple", "banana"]) |
| 426 |
basket3.should.have.key("fruits") |
| 427 |
|
| 428 |
``.have.key().being.*`` |
| 429 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 430 |
|
| 431 |
If the programmer calls ``have.key()`` it returns an assertion builder |
| 432 |
of the key if it exists, so that you can chain up assertions for the |
| 433 |
dictionary key value itself. |
| 434 |
|
| 435 |
.. code:: python |
| 436 |
|
| 437 |
person = dict(name=None) |
| 438 |
person.should.have.key("name").being.none |
| 439 |
person.should.have.key("name").being.equal(None) |
| 440 |
|
| 441 |
|
| 442 |
``.have.length_of(2)`` |
| 443 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 444 |
|
| 445 |
Assert the length of objects |
| 446 |
|
| 447 |
.. code:: python |
| 448 |
|
| 449 |
|
| 450 |
[3, 4].should.have.length_of(2) |
| 451 |
|
| 452 |
"Python".should.have.length_of(6) |
| 453 |
|
| 454 |
{'john': 'person'}.should_not.have.length_of(2) |
| 455 |
|
| 456 |
|
| 457 |
``{X}.should.be.greater_than(Y) and {Y}.should.be.lower_than(X)`` |
| 458 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 459 |
|
| 460 |
Assert the magnitude of objects with ``{X}.should.be.greater_than(Y)`` and ``{Y}.should.be.lower_than(X)`` as well as ``{X}.should.be.greater_than_or_equal_to(Y)`` and ``{Y}.should.be.lower_than_or_equal_to(X)``. |
| 461 |
|
| 462 |
|
| 463 |
.. code:: python |
| 464 |
|
| 465 |
(5).should.be.greater_than(4) |
| 466 |
(5).should_not.be.greater_than(10) |
| 467 |
(1).should.be.lower_than(2) |
| 468 |
(1).should_not.be.lower_than(0) |
| 469 |
|
| 470 |
(5).should.be.greater_than_or_equal_to(4) |
| 471 |
(5).should_not.be.greater_than_or_equal_to(10) |
| 472 |
(1).should.be.lower_than_or_equal_to(2) |
| 473 |
(1).should_not.be.lower_than_or_equal_to(0) |
| 474 |
|
| 475 |
|
| 476 |
Callables |
| 477 |
--------- |
| 478 |
|
| 479 |
|
| 480 |
``callable.when.called_with(arg1, kwarg1=2).should.have.raised(Exception)`` |
| 481 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 482 |
|
| 483 |
You can use this feature to assert that a callable raises an exception: |
| 484 |
|
| 485 |
.. code:: python |
| 486 |
|
| 487 |
range.when.called_with("chuck norris").should.have.raised(TypeError) |
| 488 |
range.when.called_with(10).should_not.throw(TypeError) |
| 489 |
|
| 490 |
Regular Expression matching on the exception message |
| 491 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 492 |
|
| 493 |
You can also match regular expressions with to the expected exception |
| 494 |
messages: |
| 495 |
|
| 496 |
.. code:: python |
| 497 |
|
| 498 |
import re |
| 499 |
range.when.called_with(10, step=20).should.have.raised(TypeError, re.compile(r'(does not take|takes no) keyword arguments')) |
| 500 |
range.when.called_with("chuck norris").should.have.raised(TypeError, re.compile(r'(cannot be interpreted as an integer|integer end argument expected)')) |
| 501 |
|
| 502 |
|
| 503 |
``.should.throw(Exception)`` |
| 504 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 505 |
|
| 506 |
An idiomatic alias to ``.should.have.raised``. |
| 507 |
|
| 508 |
.. code:: python |
| 509 |
|
| 510 |
range.when.called_with(10, step="20").should.throw(TypeError, "range() takes no keyword arguments") |
| 511 |
range.when.called_with(b"chuck norris").should.throw("range() integer end argument expected, got str.") |
| 512 |
|
| 513 |
|
| 514 |
``function.when.called_with(arg1, kwarg1=2).should.return_value(value)`` |
| 515 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 516 |
|
| 517 |
This is a shorthand for testing that a callable returns the expected |
| 518 |
result |
| 519 |
|
| 520 |
.. code:: python |
| 521 |
|
| 522 |
list.when.called_with([0, 1]).should.have.returned_the_value([0, 1]) |
| 523 |
|
| 524 |
which equates to: |
| 525 |
|
| 526 |
:: |
| 527 |
|
| 528 |
value = range(2) |
| 529 |
value.should.equal([0, 1]) |
| 530 |
|
| 531 |
there are no differences between those 2 possibilities, use at will |
| 532 |
|
| 533 |
``.be.a('typename')`` |
| 534 |
~~~~~~~~~~~~~~~~~~~~~ |
| 535 |
|
| 536 |
this takes a type name and checks if the class matches that name |
| 537 |
|
| 538 |
.. code:: python |
| 539 |
|
| 540 |
import sure |
| 541 |
|
| 542 |
{}.should.be.a('dict') |
| 543 |
(5).should.be.an('int') |
| 544 |
|
| 545 |
## also works with paths to modules |
| 546 |
|
| 547 |
range(10).should.be.a('collections.Iterable') |
| 548 |
|
| 549 |
``.be.a(type)`` |
| 550 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 551 |
|
| 552 |
this takes the class (type) itself and checks if the object is an |
| 553 |
instance of it |
| 554 |
|
| 555 |
.. code:: python |
| 556 |
|
| 557 |
import sure |
| 558 |
from six import PY3 |
| 559 |
|
| 560 |
if PY3: |
| 561 |
u"".should.be.an(str) |
| 562 |
else: |
| 563 |
u"".should.be.an(unicode) |
| 564 |
[].should.be.a(list) |
| 565 |
|
| 566 |
``.be.above(num) and .be.below(num)`` |
| 567 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 568 |
|
| 569 |
assert the instance value above and below ``num`` |
| 570 |
|
| 571 |
.. code:: python |
| 572 |
|
| 573 |
import sure |
| 574 |
|
| 575 |
(10).should.be.below(11) |
| 576 |
(10).should.be.above(9) |
| 577 |
(10).should_not.be.above(11) |
| 578 |
(10).should_not.be.below(9) |
| 579 |
|
| 580 |
``it()``, ``this()``, ``those()``, ``these()`` |
| 581 |
---------------------------------------------- |
| 582 |
|
| 583 |
``.should`` aliases to make your tests more idiomatic. |
| 584 |
|
| 585 |
Whether you don't like the ``object.should`` syntax or you are simply |
| 586 |
not running CPython, sure still allows you to use any of the assertions |
| 587 |
above, all you need to do is wrap the object that is being compared in |
| 588 |
one of the following options: ``it``, ``this``, ``those`` and ``these``. |
| 589 |
|
| 590 |
|
| 591 |
Too long, don't read |
| 592 |
~~~~~~~~~~~~~~~~~~~~ |
| 593 |
|
| 594 |
|
| 595 |
All those possibilities below work just as the same |
| 596 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 597 |
|
| 598 |
.. code:: python |
| 599 |
|
| 600 |
from sure import it, this, those, these |
| 601 |
|
| 602 |
(10).should.be.equal(5 + 5) |
| 603 |
|
| 604 |
this(10).should.be.equal(5 + 5) |
| 605 |
it(10).should.be.equal(5 + 5) |
| 606 |
these(10).should.be.equal(5 + 5) |
| 607 |
those(10).should.be.equal(5 + 5) |
| 608 |
|
| 609 |
|
| 610 |
Also if you prefer using the assert keyword in your tests just go ahead an do it! |
| 611 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 612 |
|
| 613 |
Every assertion returns ``True`` when succeeded, and if failed the |
| 614 |
AssertionError is already raised internally by sure, with a nice |
| 615 |
description of what failed to match, too. |
| 616 |
|
| 617 |
.. code:: python |
| 618 |
|
| 619 |
from sure import it, this, those, these, expect |
| 620 |
|
| 621 |
assert (10).should.be.equal(5 + 5) |
| 622 |
assert this(10).should.be.equal(5 + 5) |
| 623 |
assert it(10).should.be.equal(5 + 5) |
| 624 |
assert these(10).should.be.equal(5 + 5) |
| 625 |
assert those(10).should.be.equal(5 + 5) |
| 626 |
|
| 627 |
expect(10).to.be.equal(5 + 5) |
| 628 |
expect(10).to.not_be.equal(8) |
| 629 |
|
| 630 |
``(lambda: None).should.be.callable`` |
| 631 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 632 |
|
| 633 |
Test if something is or not callable |
| 634 |
|
| 635 |
.. code:: python |
| 636 |
|
| 637 |
import sure |
| 638 |
|
| 639 |
range.should.be.callable |
| 640 |
(lambda: None).should.be.callable |
| 641 |
(123).should_not.be.callable |
| 642 |
|
| 643 |
|
| 644 |
A note about the assert keyword |
| 645 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 646 |
|
| 647 |
.. note:: *you can use or not the* ``assert`` *keyword, sure |
| 648 |
internally already raises an appropriate* ``AssertionError`` *with an |
| 649 |
assertion message so that you don't have to specify your own, but you |
| 650 |
can still use* ``assert`` *if you find it more semantic* |
| 651 |
|
| 652 |
Example: |
| 653 |
|
| 654 |
.. code:: python |
| 655 |
|
| 656 |
import sure |
| 657 |
|
| 658 |
"Name".lower().should.equal('name') |
| 659 |
|
| 660 |
## or you can also use |
| 661 |
|
| 662 |
assert "Name".lower().should.equal('name') |
| 663 |
|
| 664 |
## or still |
| 665 |
|
| 666 |
from sure import this |
| 667 |
|
| 668 |
assert this("Name".lower()).should.equal('name') |
| 669 |
|
| 670 |
## also without the assert |
| 671 |
|
| 672 |
this("Name".lower()).should.equal('name') |
| 673 |
|
| 674 |
Any of the examples above will raise their own ``AssertionError`` with a |
| 675 |
meaningful error message. |
| 676 |
|
| 677 |
Synonyms |
| 678 |
-------- |
| 679 |
|
| 680 |
Sure provides you with a lot of synonyms so that you can pick the ones |
| 681 |
that makes more sense for your tests. |
| 682 |
|
| 683 |
Note that the examples below are merely illustrative, they work not only |
| 684 |
with numbers but with any of the assertions you read early in this |
| 685 |
documentation. |
| 686 |
|
| 687 |
Positive synonyms |
| 688 |
~~~~~~~~~~~~~~~~~ |
| 689 |
|
| 690 |
.. code:: python |
| 691 |
|
| 692 |
|
| 693 |
(2 + 2).should.be.equal(4) |
| 694 |
(2 + 2).must.be.equal(4) |
| 695 |
(2 + 2).does.equals(4) |
| 696 |
(2 + 2).do.equals(4) |
| 697 |
|
| 698 |
Negative synonyms |
| 699 |
~~~~~~~~~~~~~~~~~ |
| 700 |
|
| 701 |
.. code:: python |
| 702 |
|
| 703 |
from sure import expect |
| 704 |
|
| 705 |
(2).should_not.be.equal(3) |
| 706 |
(2).shouldnt.be.equal(3) |
| 707 |
(2).doesnt.equals(3) |
| 708 |
(2).does_not.equals(3) |
| 709 |
(2).doesnot.equals(3) |
| 710 |
(2).dont.equal(3) |
| 711 |
(2).do_not.equal(3) |
| 712 |
|
| 713 |
expect(3).to.not_be.equal(1) |
| 714 |
|
| 715 |
Chain-up synonyms |
| 716 |
~~~~~~~~~~~~~~~~~ |
| 717 |
|
| 718 |
Any of those synonyms work as an alias to the assertion builder: |
| 719 |
|
| 720 |
- ``be`` |
| 721 |
- ``being`` |
| 722 |
- ``to`` |
| 723 |
- ``when`` |
| 724 |
- ``have`` |
| 725 |
- ``with_value`` |
| 726 |
|
| 727 |
.. code:: python |
| 728 |
|
| 729 |
from sure import expect |
| 730 |
|
| 731 |
{"foo": 1}.must.with_value.being.equal({"foo": 1}) |
| 732 |
{"foo": 1}.does.have.key("foo").being.with_value.equal(1) |
| 733 |
|
| 734 |
Equality synonyms |
| 735 |
~~~~~~~~~~~~~~~~~ |
| 736 |
|
| 737 |
.. code:: python |
| 738 |
|
| 739 |
|
| 740 |
(2).should.equal(2) |
| 741 |
(2).should.equals(2) |
| 742 |
(2).should.eql(2) |
| 743 |
|
| 744 |
Positive boolean synonyms |
| 745 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 746 |
|
| 747 |
.. code:: python |
| 748 |
|
| 749 |
import sure |
| 750 |
(not None).should.be.ok |
| 751 |
(not None).should.be.truthy |
| 752 |
(not None).should.be.true |
| 753 |
|
| 754 |
Negative boolean synonyms |
| 755 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 756 |
|
| 757 |
.. code:: python |
| 758 |
|
| 759 |
import sure |
| 760 |
False.should.be.falsy |
| 761 |
False.should.be.false |
| 762 |
False.should_not.be.true |
| 763 |
False.should_not.be.ok |
| 764 |
None.should_not.be.true |
| 765 |
None.should_not.be.ok |
| 766 |
|
| 767 |
Holy guacamole, how did you implement that feature ? |
| 768 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 769 |
|
| 770 |
Differently of `ruby <http://www.ruby-lang.org>`__ python doesn't have |
| 771 |
`open classes |
| 772 |
<http://blog.aizatto.com/2007/06/01/ruby-and-open-classes/>`__, but |
| 773 |
`sure uses a technique |
| 774 |
<https://github.com/gabrielfalcao/sure/blob/master/sure/magic.py>`_ |
| 775 |
involving the module :py:mod:`ctypes` to write directly in the private |
| 776 |
``__dict__`` of in-memory objects. |
| 777 |
For more information check out the `Forbidden Fruit <https://github.com/clarete/forbiddenfruit>`_ project. |
| 778 |
|
| 779 |
Yes, it is dangerous, non-pythonic and should not be used in |
| 780 |
production code. |
| 781 |
|
| 782 |
Although ``sure`` is here to be used **ONLY** in test code, therefore |
| 783 |
it should be running in **ONLY** possible environments: your local |
| 784 |
machine or your continuous-integration server. |
| 785 |
|
| 786 |
|
| 787 |
API Builtin Documentation |
| 788 |
------------------------- |
| 789 |
|
| 790 |
.. automodule:: sure |
| 791 |
:members: |
| 792 |
|
| 793 |
.. automodule:: sure.core |
| 794 |
:members: |
| 795 |
|
| 796 |
.. automodule:: sure.old |
| 797 |
:members: |
| 798 |
|
| 799 |
|
| 800 |
Add custom assertions, chains and chain properties |
| 801 |
-------------------------------------------------- |
| 802 |
|
| 803 |
``sure`` allows to add custom assertion methods, chain methods and chain properties. |
| 804 |
|
| 805 |
Custom assertion methods |
| 806 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 807 |
|
| 808 |
By default ``sure`` comes with a good amount of *assertion methods*. For example: |
| 809 |
|
| 810 |
- ``equals()`` |
| 811 |
- ``within()`` |
| 812 |
- ``contains()`` |
| 813 |
|
| 814 |
And plenty more. |
| 815 |
|
| 816 |
However, in some cases it makes sense to add custom *assertion methods* to improve the test experience. |
| 817 |
|
| 818 |
Let's assume you want to test your web application. Somewhere there is a ``Response`` class with a ``return_code`` property. We could do the following: |
| 819 |
|
| 820 |
.. code:: python |
| 821 |
|
| 822 |
response = Response(...) |
| 823 |
response.return_code.should.be.equal(200) |
| 824 |
|
| 825 |
This is already quiet readable, but wouldn't it be awesome do to something like this: |
| 826 |
|
| 827 |
.. code:: python |
| 828 |
|
| 829 |
response = Response(...) |
| 830 |
response.should.have.return_code(200) |
| 831 |
|
| 832 |
To achieve this the custom assertion methods come into play: |
| 833 |
|
| 834 |
.. code:: python |
| 835 |
|
| 836 |
from sure import assertion |
| 837 |
|
| 838 |
@assertion |
| 839 |
def return_code(self, expected_return_code): |
| 840 |
if self.negative: |
| 841 |
assert expected_return_code != self.obj.return_code, \ |
| 842 |
'Expected return code matches' |
| 843 |
else: |
| 844 |
assert expected_return_code == self.obj.return_code, \ |
| 845 |
'Expected return code does not match' |
| 846 |
|
| 847 |
|
| 848 |
response = Response(...) |
| 849 |
response.should.have.return_code(200) |
| 850 |
|
| 851 |
|
| 852 |
I'll admit you have to write the assertion method yourself, but the result is a great experience you don't want to miss. |
| 853 |
|
| 854 |
|
| 855 |
Chain methods |
| 856 |
~~~~~~~~~~~~~ |
| 857 |
|
| 858 |
*chain methods* are similar to *assertion methods*. The only difference is that the *chain methods*, as the name implies, can be chained with further chains or assertions: |
| 859 |
|
| 860 |
.. code:: python |
| 861 |
|
| 862 |
from sure import chain |
| 863 |
|
| 864 |
@chain |
| 865 |
def header(self, header_name): |
| 866 |
# check if header name actually exists |
| 867 |
self.obj.headers.should.have.key(header_name) |
| 868 |
# return header value |
| 869 |
return self.obj.headers[header_name] |
| 870 |
|
| 871 |
|
| 872 |
response = Response(200, headers={'Content-Type': 'text/python'}) |
| 873 |
response.should.have.header('Content-Type').equals('text/python') |
| 874 |
|
| 875 |
|
| 876 |
Chain properties |
| 877 |
~~~~~~~~~~~~~~~~ |
| 878 |
|
| 879 |
*chain properties* are simple properties which are available to build an assertion. |
| 880 |
Some of the default chain properties are: |
| 881 |
|
| 882 |
- ``be`` |
| 883 |
- ``to`` |
| 884 |
- ``when`` |
| 885 |
- ``have`` |
| 886 |
- ... |
| 887 |
|
| 888 |
Use the ``chainproperty`` decorator like the following to build your own *chain*: |
| 889 |
|
| 890 |
.. code:: python |
| 891 |
|
| 892 |
from sure import chainproperty, assertion |
| 893 |
|
| 894 |
|
| 895 |
class Foo: |
| 896 |
magic = 42 |
| 897 |
|
| 898 |
|
| 899 |
@chainproperty |
| 900 |
def having(self): |
| 901 |
return self |
| 902 |
|
| 903 |
|
| 904 |
@chainproperty |
| 905 |
def implement(self): |
| 906 |
return self |
| 907 |
|
| 908 |
|
| 909 |
@assertion |
| 910 |
def attribute(self, name): |
| 911 |
has_it = hasattr(self.obj, name) |
| 912 |
if self.negative: |
| 913 |
assert not has_it, 'Expected was that object {0} does not have attr {1}'.format( |
| 914 |
self.obj, name) |
| 915 |
else: |
| 916 |
assert has_it, 'Expected was that object {0} has attr {1}'.format( |
| 917 |
self.obj, name) |
| 918 |
|
| 919 |
# Build awesome assertion chains |
| 920 |
expect(Foo).having.attribute('magic') |
| 921 |
Foo.doesnt.implement.attribute('nomagic') |
| 922 |
|
| 923 |
Use custom assertion messages with ``ensure`` |
| 924 |
--------------------------------------------- |
| 925 |
|
| 926 |
With the ``ensure`` context manager *sure* provides an easy to use way to override the ``AssertionError`` message raised by ``sure``'s assertion methods. See the following example: |
| 927 |
|
| 928 |
.. code:: python |
| 929 |
|
| 930 |
import sure |
| 931 |
|
| 932 |
name = myapi.do_something_that_returns_string() |
| 933 |
|
| 934 |
with sure.ensure('the return value actually looks like: {0}', name): |
| 935 |
name.should.contain('whatever') |
| 936 |
|
| 937 |
|
| 938 |
In case ``name`` does not contain the string ``whatever`` it will raise an ``AssertionError`` exception |
| 939 |
with the message *the return value actually looks like: <NAME>* (where *<NAME>* would be the actual value of the variable ``name``) instead of *sure*'s default error message in that particular case. |
| 940 |
|
| 941 |
Only ``AssertionError`` exceptions are re-raised by ``sure.ensure()`` with the custom provided message. Every other exception will be ignored and handled as expected. |