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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
|
Comparing objects and sequences
===============================
.. currentmodule:: testfixtures
.. invisible-code-block: python
from testfixtures.compat import PY_39_PLUS, PY_312_PLUS
The helpers here provide ways of making assertions about object equality
even when those objects don't natively support comparison. Where differences
are found, feedback is provided in a way that makes it quick and easy to see what the
difference was, even in the case of deeply nested data structures.
The compare function
--------------------
The :func:`compare` function can be used as a replacement for
:meth:`~unittest.TestCase.assertEqual` or pytest-style assert statements.
It raises an :class:`AssertionError` when its parameters are not equal, which will be
reported as a test failure:
>>> from testfixtures import compare
>>> compare(1, 2)
Traceback (most recent call last):
...
AssertionError: 1 != 2
It allows you to specify a prefix for the message to be used
in the event of failure:
>>> compare(1, 2, prefix='wrong number of orders')
Traceback (most recent call last):
...
AssertionError: wrong number of orders: 1 != 2
You can also optionally specify a suffix, which will be appended to the
message on a new line:
>>> compare(1, 2, suffix='(Except for very large values of 1)')
Traceback (most recent call last):
...
AssertionError: 1 != 2
(Except for very large values of 1)
The expected and actual value can also be explicitly supplied, making it
clearer as to what has gone wrong:
>>> compare(expected=1, actual=2)
Traceback (most recent call last):
...
AssertionError: 1 (expected) != 2 (actual)
The real strengths of this function come when comparing more complex
data types. A number of common python data types will give more
detailed output when a comparison fails as described below:
sets
~~~~
Comparing sets that aren't the same will attempt to
highlight where the differences lie:
>>> compare(set([1, 2]), set([2, 3]))
Traceback (most recent call last):
...
AssertionError: set not as expected:
<BLANKLINE>
in first but not second:
[1]
<BLANKLINE>
in second but not first:
[3]
<BLANKLINE>
<BLANKLINE>
dicts
~~~~~
Comparing dictionaries that aren't the same will attempt to
highlight where the differences lie:
>>> compare(dict(x=1, y=2, a=4), dict(x=1, z=3, a=5))
Traceback (most recent call last):
...
AssertionError: dict not as expected:
<BLANKLINE>
same:
['x']
<BLANKLINE>
in first but not second:
'y': 2
<BLANKLINE>
in second but not first:
'z': 3
<BLANKLINE>
values differ:
'a': 4 != 5
lists and tuples
~~~~~~~~~~~~~~~~
Comparing lists or tuples that aren't the same will attempt to highlight
where the differences lie:
>>> compare([1, 2, 3], [1, 2, 4])
Traceback (most recent call last):
...
AssertionError: sequence not as expected:
<BLANKLINE>
same:
[1, 2]
<BLANKLINE>
first:
[3]
<BLANKLINE>
second:
[4]
namedtuples
~~~~~~~~~~~
When two :func:`~collections.namedtuple` instances are compared, if
they are of the same type, the description given will highlight which
elements were the same and which were different:
>>> from collections import namedtuple
>>> TestTuple = namedtuple('TestTuple', 'x y z')
>>> compare(TestTuple(1, 2, 3), TestTuple(1, 4, 3))
Traceback (most recent call last):
...
AssertionError: TestTuple not as expected:
<BLANKLINE>
same:
['x', 'z']
<BLANKLINE>
values differ:
'y': 2 != 4
generators
~~~~~~~~~~
When two generators are compared, they are both first unwound into
tuples and those tuples are then compared.
The :ref:`generator <generator>` helper is useful for creating a
generator to represent the expected results:
>>> from testfixtures import generator
>>> def my_gen(t):
... i = 0
... while i<t:
... i += 1
... yield i
>>> compare(generator(1, 2, 3), my_gen(2))
Traceback (most recent call last):
...
AssertionError: sequence not as expected:
<BLANKLINE>
same:
(1, 2)
<BLANKLINE>
first:
(3,)
<BLANKLINE>
second:
()
.. warning::
If you wish to assert that a function returns a generator, say, for
performance reasons, then you should use
:ref:`strict comparison <strict-comparison>`.
strings
~~~~~~~
Comparison of strings can be tricky, particularly when those strings
contain multiple lines; spotting the differences between the expected
and actual values can be hard.
To help with this, long strings give a more helpful representation
when comparison fails:
>>> compare("1234567891011", "1234567789")
Traceback (most recent call last):
...
AssertionError:
'1234567891011'
!=
'1234567789'
Likewise, multi-line strings give unified diffs when their comparison
fails:
>>> compare("""
... This is line 1
... This is line 2
... This is line 3
... """,
... """
... This is line 1
... This is another line
... This is line 3
... """)
Traceback (most recent call last):
...
AssertionError:
--- first
+++ second
@@ -1,5 +1,5 @@
<BLANKLINE>
This is line 1
- This is line 2
+ This is another line
This is line 3
<BLANKLINE>
Such comparisons can still be confusing as white space is taken into
account. If you need to care about whitespace characters, you can make
spotting the differences easier as follows:
>>> compare("\tline 1\r\nline 2"," line1 \nline 2", show_whitespace=True)
Traceback (most recent call last):
...
AssertionError:
--- first
+++ second
@@ -1,2 +1,2 @@
-'\tline 1\r\n'
+' line1 \n'
'line 2'
However, you may not care about some of the whitespace involved. To
help with this, :func:`compare` has two options that can be set to
ignore certain types of whitespace.
If you wish to compare two strings that contain blank lines or lines
containing only whitespace characters, but where you only care about
the content, you can use the following:
.. code-block:: python
compare('line1\nline2', 'line1\n \nline2\n\n',
blanklines=False)
If you wish to compare two strings made up of lines that may have
trailing whitespace that you don't care about, you can do so with the
following:
.. code-block:: python
compare('line1\nline2', 'line1 \t\nline2 \n',
trailing_whitespace=False)
.. _compare-datetime:
datetimes and times
~~~~~~~~~~~~~~~~~~~
.. skip: start if(not PY_39_PLUS, reason="zoneinfo arrived in 3.9")
Given the following two :class:`~datetime.datetime` objects:
>>> from datetime import datetime
>>> from zoneinfo import ZoneInfo
>>> t1 = datetime(2024, 10, 27, 1, fold=0, tzinfo=ZoneInfo('Europe/London'))
>>> str(t1)
'2024-10-27 01:00:00+01:00'
>>> t2 = datetime(2024, 10, 27, 1, fold=1, tzinfo=ZoneInfo('Europe/London'))
>>> str(t2)
'2024-10-27 01:00:00+00:00'
It may well be surprising to find out that Python considers them equivalent:
>>> t1 == t2
True
Unfortunately, that also means that :func:`compare` will also consider them
equal:
>>> compare(t1, t2)
If it is important for you to be able to check you have the correct point in time,
then you can use strict comparison, which will highlight the difference:
>>> compare(t1, t2, strict=True)
Traceback (most recent call last):
...
AssertionError: datetime.datetime(2024, 10, 27, 1, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/London')) != datetime.datetime(2024, 10, 27, 1, 0, fold=1, tzinfo=zoneinfo.ZoneInfo(key='Europe/London'))
.. skip: end
This problem can also be seen with :class:`~datetime.time` objects, where given
the following two times:
>>> from datetime import time
>>> t1 = time(1, 30, fold=0)
>>> str(t1)
'01:30:00'
>>> t2 = time(1, 30, fold=1)
>>> str(t2)
'01:30:00'
The times will be considered equal:
>>> t1 == t2
True
>>> compare(t1, t2)
However, once again, strict comparison will highlight the difference:
>>> compare(t1, t2, strict=True)
Traceback (most recent call last):
...
AssertionError: datetime.time(1, 30) != datetime.time(1, 30, fold=1)
.. _comparer-objects:
objects
~~~~~~~
Even if your objects do not natively support comparison, when they are compared
they will be considered identical if they are of the same type and have identical
attributes. Take instances of this class as an example:
.. code-block:: python
class MyObject:
def __init__(self, name):
self.name = name
def __repr__(self):
return '<MyObject>'
If the attributes and type of instances are the same, they will be considered equal:
>>> compare(MyObject('foo'), MyObject('foo'))
However, if their attributes differ, you will get an informative error:
>>> compare(MyObject('foo'), MyObject('bar'))
Traceback (most recent call last):
...
AssertionError: MyObject not as expected:
<BLANKLINE>
attributes differ:
'name': 'foo' != 'bar'
<BLANKLINE>
While comparing .name: 'foo' != 'bar'
This type of comparison is also used on objects that make use of ``__slots__``.
Recursive comparison
~~~~~~~~~~~~~~~~~~~~
Where :func:`compare` is able to provide a descriptive comparison for
a particular type, it will then recurse to do the same for the
elements contained within objects of that type.
For example, when comparing a list of dictionaries, the description
will not only tell you where in the list the difference occurred, but
also what the differences were within the dictionaries that weren't
equal:
>>> compare([{'one': 1}, {'two': 2, 'text':'foo\nbar\nbaz'}],
... [{'one': 1}, {'two': 2, 'text':'foo\nbob\nbaz'}])
Traceback (most recent call last):
...
AssertionError: sequence not as expected:
<BLANKLINE>
same:
[{'one': 1}]
<BLANKLINE>
first:
[{'text': 'foo\nbar\nbaz', 'two': 2}]
<BLANKLINE>
second:
[{'text': 'foo\nbob\nbaz', 'two': 2}]
<BLANKLINE>
While comparing [1]: dict not as expected:
<BLANKLINE>
same:
['two']
<BLANKLINE>
values differ:
'text': 'foo\nbar\nbaz' != 'foo\nbob\nbaz'
<BLANKLINE>
While comparing [1]['text']:
--- first
+++ second
@@ -1,3 +1,3 @@
foo
-bar
+bob
baz
This also applies to any comparers you have provided, as can be seen
in the next section.
.. _comparer-register:
Providing your own comparers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When using :meth:`compare` frequently for your own complex objects,
it can be beneficial to give more descriptive output when two objects
don't compare as equal.
.. note::
If you are reading this section as a result of needing to test
objects that don't natively support comparison, or as a result of
needing to infrequently compare your own subclasses of python
basic types, take a look at :ref:`comparison-objects` as this may
well be an easier solution.
.. invisible-code-block: python
from testfixtures.comparison import _registry, compare_sequence
from testfixtures import Replacer
r = Replacer()
r.replace('testfixtures.comparison._registry', {
list: compare_sequence
})
As an example, suppose you have a class whose instances have a
timestamp and a name as attributes, but you'd like to ignore the
timestamp when comparing:
.. code-block:: python
from datetime import datetime
class MyObject:
def __init__(self, name):
self.timestamp = datetime.now()
self.name = name
To compare lots of these, you would first write a comparer:
.. code-block:: python
def compare_my_object(x, y, context):
if x.name == y.name:
return
return 'MyObject named %s != MyObject named %s' % (
context.label('x', repr(x.name)),
context.label('y', repr(y.name)),
)
Then you'd register that comparer for your type:
.. code-block:: python
from testfixtures.comparison import register
register(MyObject, compare_my_object)
.. invisible-code-block: python
import testfixtures.comparison
assert testfixtures.comparison._registry == {
MyObject: compare_my_object, list: compare_sequence,
}
Now, it'll get used when comparing objects of that type,
even if they're contained within other objects:
>>> compare([1, MyObject('foo')], [1, MyObject('bar')])
Traceback (most recent call last):
...
AssertionError: sequence not as expected:
<BLANKLINE>
same:
[1]
<BLANKLINE>
first:
[<MyObject ...>]
<BLANKLINE>
second:
[<MyObject ...>]
<BLANKLINE>
While comparing [1]: MyObject named 'foo' != MyObject named 'bar'
From this example, you can also see that a comparer can indicate that
two objects are equal, for :func:`compare`'s purposes, by returning
``None``:
>>> MyObject('foo') == MyObject('foo')
False
>>> compare(MyObject('foo'), MyObject('foo'))
You can also see that you can, and should, use the context object passed in
to add labels to the representations of the objects being compared if the
comparison fails:
>>> compare(expected=MyObject('foo'), actual=MyObject('bar'))
Traceback (most recent call last):
...
AssertionError: MyObject named 'foo' (expected) != MyObject named 'bar' (actual)
.. invisible-code-block: python
r.restore()
# set up for the next test
r = Replacer()
r.replace('testfixtures.comparison._registry', {})
It may be that you only want to use a comparer or set of
comparers for a particular test. If that's the case, you can pass
:func:`compare` a ``comparers`` parameter consisting of a
dictionary that maps types to comparers. These will be added to the
global registry for the duration of the call:
>>> compare(MyObject('foo'), MyObject('bar'),
... comparers={MyObject: compare_my_object})
Traceback (most recent call last):
...
AssertionError: MyObject named 'foo' != MyObject named 'bar'
.. invisible-code-block: python
import testfixtures.comparison
assert testfixtures.comparison._registry == {}
r.restore()
A full list of the available comparers included can be found below the
API documentation for :func:`compare`. These make good candidates for
registering for your own classes, if they provide the necessary
behaviour, and their source is also good to read when wondering how to
implement your own comparers.
You may be wondering what the ``context`` object passed to the
comparer is for; it allows you to hand off comparison of parts of the
two objects currently being compared back to the :func:`compare`
machinery, it also allows you to pass options to your comparison
function.
For example, you may have an object that has a couple of dictionaries
as attributes:
.. code-block:: python
class Request:
def __init__(self, uri, headers, body):
self.uri = uri
self.headers = headers
self.body = body
When your tests encounter instances of these that are not as expected,
you want feedback about which bits of the request or response weren't
as expected. This can be achieved by implementing a comparer as
follows:
.. code-block:: python
def compare_request(x, y, context):
uri_different = x.uri != y.uri
headers_different = context.different(x.headers, y.headers, '.headers')
body_different = context.different(x.body, y.body, '.body')
if uri_different or headers_different or body_different:
return 'Request for %r != Request for %r' % (
x.uri, y.uri
)
.. note::
A comparer should always return some text when it considers
the two objects it is comparing to be different.
This comparer can either be registered globally or passed to each
:func:`compare` call and will give detailed feedback about how the
requests were different:
>>> compare(Request('/foo', {'method': 'POST'}, {'my_field': 'value_1'}),
... Request('/foo', {'method': 'GET'}, {'my_field': 'value_2'}),
... comparers={Request: compare_request})
Traceback (most recent call last):
...
AssertionError: Request for '/foo' != Request for '/foo'
<BLANKLINE>
While comparing .headers: dict not as expected:
<BLANKLINE>
values differ:
'method': 'POST' != 'GET'
<BLANKLINE>
While comparing .headers['method']: 'POST' != 'GET'
<BLANKLINE>
While comparing .body: dict not as expected:
<BLANKLINE>
values differ:
'my_field': 'value_1' != 'value_2'
<BLANKLINE>
While comparing .body['my_field']: 'value_1' != 'value_2'
As an example of passing options through to a comparer, suppose you
wanted to compare all decimals in a nested data structure by rounding
them to a number of decimal places that varies from test to test. The
comparer could be implemented and registered as follows:
.. invisible-code-block: python
from testfixtures.comparison import _registry
r = Replacer()
r.replace('testfixtures.comparison._registry', dict(_registry))
.. code-block:: python
from decimal import Decimal
from testfixtures.comparison import register
def compare_decimal(x, y, context):
precision = context.get_option('precision', 2)
if round(x, precision) != round(y, precision):
return '%r != %r when rounded to %i places' % (
x, y, precision
)
register(Decimal, compare_decimal)
Now, this comparer will be used for comparing all decimals and the
precision used will be that passed to :func:`compare`:
>>> expected_order = {'price': Decimal('1.234'), 'quantity': 5}
>>> actual_order = {'price': Decimal('1.236'), 'quantity': 5}
>>> compare(expected_order, actual_order, precision=1)
>>> compare(expected_order, actual_order, precision=3)
Traceback (most recent call last):
...
AssertionError: dict not as expected:
<BLANKLINE>
same:
['quantity']
<BLANKLINE>
values differ:
'price': Decimal('1.234') != Decimal('1.236')
<BLANKLINE>
While comparing ['price']: Decimal('1.234') != Decimal('1.236') when rounded to 3 places
If no precision is passed, the default of ``2`` will be used:
>>> compare(Decimal('2.006'), Decimal('2.009'))
>>> compare(Decimal('2.001'), Decimal('2.009'))
Traceback (most recent call last):
...
AssertionError: Decimal('2.001') != Decimal('2.009') when rounded to 2 places
.. invisible-code-block: python
r.restore()
.. _strict-comparison:
Ignoring ``__eq__``
~~~~~~~~~~~~~~~~~~~
Some objects, such as those from the Django ORM, have pretty broken
implementations of ``__eq__``. Since :func:`compare` normally relies on this,
it can result in objects appearing to be equal when they are not.
Take this class, for example:
.. code-block:: python
class OrmObj:
def __init__(self, a):
self.a = a
def __eq__(self, other):
return True
def __repr__(self):
return 'OrmObj: '+str(self.a)
If we compare normally, we erroneously understand the objects to be equal:
>>> compare(actual=OrmObj(1), expected=OrmObj(2))
In order to get a sane comparison, we need to both supply a custom comparer
as described above, and use the ``ignore_eq`` parameter:
.. code-block:: python
def compare_orm_obj(x, y, context):
if x.a != y.a:
return 'OrmObj: %s != %s' % (x.a, y.a)
>>> compare(actual=OrmObj(1), expected=OrmObj(2),
... comparers={OrmObj: compare_orm_obj}, ignore_eq=True)
Traceback (most recent call last):
...
AssertionError: OrmObj: 2 != 1
Strict comparison
~~~~~~~~~~~~~~~~~
If is it important that the two values being compared are of exactly
the same type, rather than just being equal as far as Python is
concerned, then the strict mode of :func:`compare` should be used.
For example, these two instances will normally appear to be equal
provided the elements within them are the same:
>>> TypeA = namedtuple('A', 'x')
>>> TypeB = namedtuple('B', 'x')
>>> compare(TypeA(1), TypeB(1))
If this type difference is important, then the `strict` parameter
should be used:
>>> compare(TypeA(1), TypeB(1), strict=True)
Traceback (most recent call last):
...
AssertionError: A(x=1) (<class '__test__.A'>) != B(x=1) (<class '__test__.B'>)
.. _comparison-objects:
Comparison objects
------------------
Another common problem with the checking in tests is that you may only want to make
assertions about the type of an object that is nested in a data structure, or even just compare
a subset of an object's attributes. Testfixtures provides the :class:`~testfixtures.Comparison`
class to help in situations like these.
Comparisons will appear to be equal to any object they are compared
with that matches their specification. For example, take the following
class:
.. code-block:: python
class SomeClass:
def __init__(self, x, y):
self.x, self.y = x, y
When a comparison fails, the :class:`~testfixtures.Comparison` will not equal the object it
was compared with and its representation changes to give information about what went wrong:
>>> from testfixtures import Comparison as C
>>> c = C(SomeClass, x=2)
>>> print(repr(c))
<C:...SomeClass>x: 2</>
>>> c == SomeClass(1, 2)
False
>>> print(repr(c))
<BLANKLINE>
<C:...SomeClass(failed)>
attributes in actual but not Comparison:
'y': 2
<BLANKLINE>
attributes differ:
'x': 2 (Comparison) != 1 (actual)
</C:...SomeClass>
.. note::
Some test frameworks and helpers, including :meth:`~unittest.TestCase.assertEqual`,
truncate the text shown in assertions. Use :func:`compare` instead, which will
give you other desirable behaviour as well as showing you the full
output of failed comparisons.
Types of comparison
~~~~~~~~~~~~~~~~~~~
There are several ways a comparison can be set up depending on what
you want to check.
If you only care about the type of an object, you can set up the
comparison with only the class:
>>> C(SomeClass) == SomeClass(1, 2)
True
This can also be achieved by specifying the type of the object as a
dotted name:
>>> import sys
>>> C('types.ModuleType') == sys
True
Alternatively, if you happen to have an object already
around, comparison can be done with it:
>>> C(SomeClass(1, 2)) == SomeClass(1, 2)
True
If you only care about certain attributes, this can also easily be
achieved by doing a partial comparison:
>>> C(SomeClass, x=1, partial=True) == SomeClass(1, 2)
True
The above can be problematic if you want to compare an object with
attributes that share names with parameters to the :class:`~testfixtures.Comparison`
constructor. For this reason, you can pass the attributes in a
dictionary:
>>> compare(C(SomeClass, {'partial': 3}, partial=True), SomeClass(1, 2))
Traceback (most recent call last):
...
AssertionError:
<C:...SomeClass(failed)>
attributes in Comparison but not actual:
'partial': 3
</C:...SomeClass> != <...SomeClass...>
Gotchas
~~~~~~~
- If the object being compared has an ``__eq__`` method, such as
Django model instances, then the :class:`~testfixtures.Comparison`
must be the first object in the equality check.
The following class is an example of this:
.. code-block:: python
class SomeModel:
def __eq__(self,other):
if isinstance(other, SomeModel):
return True
return False
It will not work correctly if used as the second object in the
expression:
>>> SomeModel() == C(SomeModel)
False
However, if the comparison is correctly placed first, then
everything will behave as expected:
>>> C(SomeModel)==SomeModel()
True
- It probably goes without saying, but comparisons should not be used
on both sides of an equality check:
>>> C(SomeClass) == C(SomeClass)
False
Mapping Comparison objects
---------------------------
When comparing mappings such as :class:`dict` and :class:`~collections.OrderedDict`,
you may need to check the order of the keys is as you expect.
:class:`MappingComparison` objects can be used for this:
.. skip: start if(not PY_312_PLUS, reason="Python 3.12 has nicer reprs")
>>> from collections import OrderedDict
>>> from testfixtures import compare, MappingComparison as M
>>> compare(expected=M((('a', 1), ('c', 3), ('d', 2)), ordered=True),
... actual=OrderedDict((('a', 1), ('d', 2), ('c', 3))))
Traceback (most recent call last):
...
AssertionError:...
<MappingComparison(ordered=True, partial=False)(failed)>
wrong key order:
<BLANKLINE>
same:
['a']
<BLANKLINE>
expected:
['c', 'd']
<BLANKLINE>
actual:
['d', 'c']
</MappingComparison(ordered=True, partial=False)> (expected) != OrderedDict({'a': 1, 'd': 2, 'c': 3}) (actual)
You may also only care about certain keys being present in a mapping. This
can also be achieved with :class:`MappingComparison` objects:
>>> compare(expected=M(a=1, d=2, partial=True), actual={'a': 1, 'c': 3})
Traceback (most recent call last):
...
AssertionError:...
<MappingComparison(ordered=False, partial=True)(failed)>
ignored:
['c']
<BLANKLINE>
same:
['a']
<BLANKLINE>
in expected but not actual:
'd': 2
</MappingComparison(ordered=False, partial=True)> (expected) != {'a': 1, 'c': 3} (actual)
Where there are differences, they may be hard to spot. In this case, you can ask for a more
detailed explanation of what wasn't as expected:
>>> compare(expected=M((('a', [1, 2]), ('d', [1, 3])), ordered=True, recursive=True),
... actual=OrderedDict((('a', [1, 2]), ('d', [1, 4]))))
Traceback (most recent call last):
...
AssertionError:...
<MappingComparison(ordered=True, partial=False)(failed)>
same:
['a']
<BLANKLINE>
values differ:
'd': [1, 3] (expected) != [1, 4] (actual)
<BLANKLINE>
While comparing ['d']: sequence not as expected:
<BLANKLINE>
same:
[1]
<BLANKLINE>
expected:
[3]
<BLANKLINE>
actual:
[4]
</MappingComparison(ordered=True, partial=False)> (expected) != OrderedDict({'a': [1, 2], 'd': [1, 4]}) (actual)
.. skip: end
Round Comparison objects
-------------------------
When comparing numerics you often want to be able to compare to a
given precision to allow for rounding issues which make precise
equality impossible.
For these situations, you can use :class:`RoundComparison` objects
wherever you would use floats or Decimals, and they will compare equal to
any float or Decimal that matches when both sides are rounded to the
specified precision.
Here's an example:
.. code-block:: python
from testfixtures import compare, RoundComparison as R
compare(expected=R(1234.5678, 2), actual=1234.5681)
.. note::
You should always pass the same type of object to the
:class:`RoundComparison` object as you intend to compare it with. If
the type of the rounded expected value is not the same as the type of
the rounded value it is being compared to, a :class:`TypeError`
will be raised.
Range Comparison objects
-------------------------
When comparing numbers, dates, times and any other type that can be ordered, you may only
want to assert what range a value will fall into. :class:`RangeComparison` objects
let you confirm a value is within a certain tolerance or range.
Here's an example with numbers:
.. code-block:: python
from testfixtures import compare, RangeComparison as R
compare(expected=R(123.456, 789), actual=Decimal(555.01))
Here's an example with dates:
.. code-block:: python
from datetime import date
from testfixtures import compare, RangeComparison as R
compare(expected=R(date(1978, 6, 13), date(1978, 10, 31)), actual=date(1978, 7, 1))
.. note::
:class:`RangeComparison` is inclusive of both the lower and upper bound.
Sequence Comparison objects
---------------------------
When comparing sequences, you may not care about the order of items in the
sequence. While this type of comparison can often be achieved by pouring
the sequence into a :class:`set`, this may not be possible if the items in the
sequence are unhashable, or part of a nested data structure.
:class:`SequenceComparison` objects can be used in this case:
>>> from testfixtures import compare, SequenceComparison as S
>>> compare(expected={'k': S({1}, {2}, ordered=False)}, actual={'k': [{2}, {1}]})
You may also only care about certain items being present in a sequence, but where
it is important that those items are in the order you expected. This
can also be achieved with :class:`SequenceComparison` objects:
>>> compare(expected=S(1, 3, 5, partial=True), actual=[1, 2, 3, 4, 6])
Traceback (most recent call last):
...
AssertionError:...
<SequenceComparison(ordered=True, partial=True)(failed)>
ignored:
[2, 4, 6]
<BLANKLINE>
same:
[1, 3]
<BLANKLINE>
expected:
[5]
<BLANKLINE>
actual:
[]
</SequenceComparison(ordered=True, partial=True)> (expected) != [1, 2, 3, 4, 6] (actual)
Where there are differences, they may be hard to spot. In this case, you can ask for a more
detailed explanation of what wasn't as expected:
>>> compare(expected=S({1: 'a'}, {2: 'c'}, recursive=True), actual=[{1: 'a'}, {2: 'd'}])
Traceback (most recent call last):
...
AssertionError:...
<SequenceComparison(ordered=True, partial=False)(failed)>
same:
[{1: 'a'}]
<BLANKLINE>
expected:
[{2: 'c'}]
<BLANKLINE>
actual:
[{2: 'd'}]
<BLANKLINE>
While comparing [1]: dict not as expected:
<BLANKLINE>
values differ:
2: 'c' (expected) != 'd' (actual)
<BLANKLINE>
While comparing [1][2]: 'c' (expected) != 'd' (actual)
</SequenceComparison(ordered=True, partial=False)> (expected) != [{1: 'a'}, {2: 'd'}] (actual)
There are also the :class:`Subset` and :class:`Permutation` shortcuts:
>>> from testfixtures import Subset, Permutation
>>> assert Subset({1}, {2}) == [{1}, {2}, {3}]
>>> assert Permutation({1}, {2}) == [{2}, {1}]
.. _stringcomparison:
String Comparison objects
-------------------------
When comparing sequences of strings, particularly those coming from
things like the python logging package, you often end up wanting to
express a requirement that one string should be almost like another,
or maybe fit a particular pattern expressed as a regular expression.
For these situations, you can use :class:`StringComparison` objects
wherever you would use normal strings, and they will compare equal to
any string that matches the regular expression they are created with.
Here's an example:
.. code-block:: python
from testfixtures import compare, StringComparison as S
compare(expected=S(r'Starting thread \d+'), actual='Starting thread 132356')
If you need to specify flags, this can be done in one of three ways:
- As parameters:
.. code-block:: python
compare(expected=S(".*BaR", dotall=True, ignorecase=True), actual="foo\nbar")
- As you would to :func:`re.compile`:
.. code-block:: python
import re
compare(expected=S(".*BaR", re.DOTALL|re.IGNORECASE), actual="foo\nbar")
- Inline:
.. code-block:: python
compare(expected=S("(?s:.*bar)"), actual="foo\nbar")
Differentiating chunks of text
------------------------------
Testfixtures provides a function that will compare two strings and
give a unified diff as a result. This can be handy as a third
parameter to :meth:`~unittest.TestCase.assertEqual` or just as a
general utility function for comparing two lumps of text.
As an example:
>>> from testfixtures import diff
>>> print(diff('line1\nline2\nline3',
... 'line1\nlineA\nline3'))
--- first
+++ second
@@ -1,3 +1,3 @@
line1
-line2
+lineA
line3
|