This file is indexed.

/usr/share/doc/python-testfixtures-doc/html/comparing.html is in python-testfixtures-doc 4.14.3-1.

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
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Comparing objects and sequences &#8212; testfixtures 4.14.3 documentation</title>
    
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '4.14.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Mocking out objects and methods" href="mocking.html" />
    <link rel="prev" title="TestFixtures documentation" href="index.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="mocking.html" title="Mocking out objects and methods"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="index.html" title="TestFixtures documentation"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">testfixtures 4.14.3 documentation</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="comparing-objects-and-sequences">
<h1>Comparing objects and sequences<a class="headerlink" href="#comparing-objects-and-sequences" title="Permalink to this headline"></a></h1>
<p>Python&#8217;s <a class="reference external" href="https://docs.python.org/3.5/library/unittest.html#module-unittest" title="(in Python v3.5)"><code class="xref py py-mod docutils literal"><span class="pre">unittest</span></code></a> package often fails to give very useful
feedback when comparing long sequences or chunks of text. It also has
trouble dealing with objects that don&#8217;t natively support
comparison. The functions and classes described here alleviate these
problems.</p>
<div class="section" id="the-compare-function">
<h2>The compare function<a class="headerlink" href="#the-compare-function" title="Permalink to this headline"></a></h2>
<p>The <a class="reference internal" href="api.html#testfixtures.compare" title="testfixtures.compare"><code class="xref py py-func docutils literal"><span class="pre">compare()</span></code></a> function can be used as a replacement for
<a class="reference external" href="https://docs.python.org/3.5/library/unittest.html#unittest.TestCase.assertEqual" title="(in Python v3.5)"><code class="xref py py-meth docutils literal"><span class="pre">assertEqual()</span></code></a>. It raises an
<code class="docutils literal"><span class="pre">AssertionError</span></code> when its parameters are not equal, which will be
reported as a test failure:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">testfixtures</span> <span class="k">import</span> <span class="n">compare</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">1 != 2</span>
</pre></div>
</div>
<p>However, it allows you to specify a prefix for the message to be used
in the event of failure:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s1">&#39;wrong number of orders&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">wrong number of orders: 1 != 2</span>
</pre></div>
</div>
<p>This is recommended as it makes the reason for the failure more
apparent without having to delve into the code or tests.</p>
<p>You can also optionally specify a suffix, which will be appended to the
message on a new line:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s1">&#39;(Except for very large values of 1)&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">1 != 2</span>
<span class="go">(Except for very large values of 1)</span>
</pre></div>
</div>
<p>The expected and actual value can also be explicitly supplied, making it
clearer as to what has gone wrong:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">expected</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">actual</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">1 (expected) != 2 (actual)</span>
</pre></div>
</div>
<p>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:</p>
<div class="section" id="sets">
<h3>sets<a class="headerlink" href="#sets" title="Permalink to this headline"></a></h3>
<p>Comparing sets that aren&#8217;t the same will attempt to
highlight where the differences lie:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="nb">set</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]),</span> <span class="nb">set</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]))</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">set not as expected:</span>

<span class="go">in first but not second:</span>
<span class="go">[1]</span>

<span class="go">in second but not first:</span>
<span class="go">[3]</span>

</pre></div>
</div>
</div>
<div class="section" id="dicts">
<h3>dicts<a class="headerlink" href="#dicts" title="Permalink to this headline"></a></h3>
<p>Comparing dictionaries that aren&#8217;t the same will attempt to
highlight where the differences lie:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="mi">4</span><span class="p">),</span> <span class="nb">dict</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">z</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="mi">5</span><span class="p">))</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">dict not as expected:</span>

<span class="go">same:</span>
<span class="go">[&#39;x&#39;]</span>

<span class="go">in first but not second:</span>
<span class="go">&#39;y&#39;: 2</span>

<span class="go">in second but not first:</span>
<span class="go">&#39;z&#39;: 3</span>

<span class="go">values differ:</span>
<span class="go">&#39;a&#39;: 4 != 5</span>
</pre></div>
</div>
</div>
<div class="section" id="lists-and-tuples">
<h3>lists and tuples<a class="headerlink" href="#lists-and-tuples" title="Permalink to this headline"></a></h3>
<p>Comparing lists or tuples that aren&#8217;t the same will attempt to highlight
where the differences lie:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">sequence not as expected:</span>

<span class="go">same:</span>
<span class="go">[1, 2]</span>

<span class="go">first:</span>
<span class="go">[3]</span>

<span class="go">second:</span>
<span class="go">[4]</span>
</pre></div>
</div>
</div>
<div class="section" id="namedtuples">
<h3>namedtuples<a class="headerlink" href="#namedtuples" title="Permalink to this headline"></a></h3>
<p>When two <a class="reference external" href="https://docs.python.org/3.5/library/collections.html#collections.namedtuple" title="(in Python v3.5)"><code class="xref py py-func docutils literal"><span class="pre">namedtuple()</span></code></a> instances are compared, if
they are of the same type, the description given will highlight which
elements were the same and which were different:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">namedtuple</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">TestTuple</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;TestTuple&#39;</span><span class="p">,</span> <span class="s1">&#39;x y z&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">TestTuple</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">TestTuple</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">TestTuple not as expected:</span>

<span class="go">same:</span>
<span class="go">[&#39;x&#39;, &#39;z&#39;]</span>

<span class="go">values differ:</span>
<span class="go">&#39;y&#39;: 2 != 4</span>
</pre></div>
</div>
</div>
<div class="section" id="generators">
<h3>generators<a class="headerlink" href="#generators" title="Permalink to this headline"></a></h3>
<p>When two generators are compared, they are both first unwound into
tuples and those tuples are then compared.</p>
<p>The <a class="reference internal" href="utilities.html#generator"><span class="std std-ref">generator</span></a> helper is useful for creating a
generator to represent the expected results:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">testfixtures</span> <span class="k">import</span> <span class="n">generator</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">my_gen</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
<span class="gp">... </span>    <span class="k">while</span> <span class="n">i</span><span class="o">&lt;</span><span class="n">t</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="gp">... </span>        <span class="k">yield</span> <span class="n">i</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">generator</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">my_gen</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">sequence not as expected:</span>

<span class="go">same:</span>
<span class="go">(1, 2)</span>

<span class="go">first:</span>
<span class="go">(3,)</span>

<span class="go">second:</span>
<span class="go">()</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">If you wish to assert that a function returns a generator, say, for
performance reasons, then you should use
<a class="reference internal" href="#strict-comparison"><span class="std std-ref">strict comparison</span></a>.</p>
</div>
</div>
<div class="section" id="strings-and-unicodes">
<h3>strings and unicodes<a class="headerlink" href="#strings-and-unicodes" title="Permalink to this headline"></a></h3>
<p>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.</p>
<p>To help with this, long strings give a more helpful representation
when comparison fails:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="s2">&quot;1234567891011&quot;</span><span class="p">,</span> <span class="s2">&quot;1234567789&quot;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>:
<span class="go">&#39;1234567891011&#39;</span>
<span class="go">!=</span>
<span class="go">&#39;1234567789&#39;</span>
</pre></div>
</div>
<p>Likewise, multi-line strings give unified diffs when their comparison
fails:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
<span class="gp">... </span><span class="s2">        This is line 1</span>
<span class="gp">... </span><span class="s2">        This is line 2</span>
<span class="gp">... </span><span class="s2">        This is line 3</span>
<span class="gp">... </span><span class="s2">        &quot;&quot;&quot;</span><span class="p">,</span>
<span class="gp">... </span>        <span class="sd">&quot;&quot;&quot;</span>
<span class="gp">... </span><span class="sd">        This is line 1</span>
<span class="gp">... </span><span class="sd">        This is another line</span>
<span class="gp">... </span><span class="sd">        This is line 3</span>
<span class="gp">... </span><span class="sd">        &quot;&quot;&quot;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>:
<span class="go">--- first</span>
<span class="go">+++ second</span>
<span class="go">@@ -1,5 +1,5 @@</span>

<span class="go">         This is line 1</span>
<span class="go">-        This is line 2</span>
<span class="go">+        This is another line</span>
<span class="go">         This is line 3</span>
</pre></div>
</div>
<p>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:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\t</span><span class="s2">line 1</span><span class="se">\r\n</span><span class="s2">line 2&quot;</span><span class="p">,</span><span class="s2">&quot; line1 </span><span class="se">\n</span><span class="s2">line 2&quot;</span><span class="p">,</span> <span class="n">show_whitespace</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>:
<span class="go">--- first</span>
<span class="go">+++ second</span>
<span class="go">@@ -1,2 +1,2 @@</span>
<span class="go">-&#39;\tline 1\r\n&#39;</span>
<span class="go">+&#39; line1 \n&#39;</span>
<span class="go"> &#39;line 2&#39;</span>
</pre></div>
</div>
<p>However, you may not care about some of the whitespace involved. To
help with this, <a class="reference internal" href="api.html#testfixtures.compare" title="testfixtures.compare"><code class="xref py py-func docutils literal"><span class="pre">compare()</span></code></a> has two options that can be set to
ignore certain types of whitespace.</p>
<p>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:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">compare</span><span class="p">(</span><span class="s1">&#39;line1</span><span class="se">\n</span><span class="s1">line2&#39;</span><span class="p">,</span> <span class="s1">&#39;line1</span><span class="se">\n</span><span class="s1"> </span><span class="se">\n</span><span class="s1">line2</span><span class="se">\n\n</span><span class="s1">&#39;</span><span class="p">,</span>
        <span class="n">blanklines</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</pre></div>
</div>
<p>If you wish to compare two strings made up of lines that may have
trailing whitespace that you don&#8217;t care about, you can do so with the
following:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">compare</span><span class="p">(</span><span class="s1">&#39;line1</span><span class="se">\n</span><span class="s1">line2&#39;</span><span class="p">,</span> <span class="s1">&#39;line1 </span><span class="se">\t\n</span><span class="s1">line2   </span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span>
        <span class="n">trailing_whitespace</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="recursive-comparison">
<h3>Recursive comparison<a class="headerlink" href="#recursive-comparison" title="Permalink to this headline"></a></h3>
<p>Where <a class="reference internal" href="api.html#testfixtures.compare" title="testfixtures.compare"><code class="xref py py-func docutils literal"><span class="pre">compare()</span></code></a> 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&#8217;t
equal:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">([{</span><span class="s1">&#39;one&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="p">{</span><span class="s1">&#39;two&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;text&#39;</span><span class="p">:</span><span class="s1">&#39;foo</span><span class="se">\n</span><span class="s1">bar</span><span class="se">\n</span><span class="s1">baz&#39;</span><span class="p">}],</span>
<span class="gp">... </span>        <span class="p">[{</span><span class="s1">&#39;one&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="p">{</span><span class="s1">&#39;two&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;text&#39;</span><span class="p">:</span><span class="s1">&#39;foo</span><span class="se">\n</span><span class="s1">bob</span><span class="se">\n</span><span class="s1">baz&#39;</span><span class="p">}])</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">sequence not as expected:</span>

<span class="go">same:</span>
<span class="go">[{&#39;one&#39;: 1}]</span>

<span class="go">first:</span>
<span class="go">[{&#39;text&#39;: &#39;foo\nbar\nbaz&#39;, &#39;two&#39;: 2}]</span>

<span class="go">second:</span>
<span class="go">[{&#39;text&#39;: &#39;foo\nbob\nbaz&#39;, &#39;two&#39;: 2}]</span>

<span class="go">While comparing [1]: dict not as expected:</span>

<span class="go">same:</span>
<span class="go">[&#39;two&#39;]</span>

<span class="go">values differ:</span>
<span class="go">&#39;text&#39;: &#39;foo\nbar\nbaz&#39; != &#39;foo\nbob\nbaz&#39;</span>

<span class="go">While comparing [1][&#39;text&#39;]:</span>
<span class="go">--- first</span>
<span class="go">+++ second</span>
<span class="go">@@ -1,3 +1,3 @@</span>
<span class="go"> foo</span>
<span class="go">-bar</span>
<span class="go">+bob</span>
<span class="go"> baz</span>
</pre></div>
</div>
<p>This also applies to any comparers you have provided, as can be seen
in the next section.</p>
</div>
<div class="section" id="providing-your-own-comparers">
<span id="comparer-register"></span><h3>Providing your own comparers<a class="headerlink" href="#providing-your-own-comparers" title="Permalink to this headline"></a></h3>
<p>When using <a class="reference internal" href="api.html#testfixtures.compare" title="testfixtures.compare"><code class="xref py py-meth docutils literal"><span class="pre">compare()</span></code></a> frequently for your own complex objects,
it can be beneficial to give more descriptive output when two objects
don&#8217;t compare as equal.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you are reading this section as a result of needing to test
objects that don&#8217;t natively support comparison, or as a result of
needing to infrequently compare your own subclasses of python
basic types, take a look at <a class="reference internal" href="#comparison-objects"><span class="std std-ref">Comparison objects</span></a> as this may
well be an easier solution.</p>
</div>
<p>As an example, suppose you have a class whose instances have a
timestamp and a name as attributes, but you&#8217;d like to ignore the
timestamp when comparing:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>

<span class="k">class</span> <span class="nc">MyObject</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">timestamp</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
</pre></div>
</div>
<p>To compare lots of these, you would first write a comparer:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">compare_my_object</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="n">y</span><span class="o">.</span><span class="n">name</span><span class="p">:</span>
        <span class="k">return</span>
    <span class="k">return</span> <span class="s1">&#39;MyObject named </span><span class="si">%s</span><span class="s1"> != MyObject named </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
        <span class="n">context</span><span class="o">.</span><span class="n">label</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">name</span><span class="p">)),</span>
        <span class="n">context</span><span class="o">.</span><span class="n">label</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">y</span><span class="o">.</span><span class="n">name</span><span class="p">)),</span>
        <span class="p">)</span>
</pre></div>
</div>
<p>Then you&#8217;d register that comparer for your type:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">testfixtures.comparison</span> <span class="kn">import</span> <span class="n">register</span>
<span class="n">register</span><span class="p">(</span><span class="n">MyObject</span><span class="p">,</span> <span class="n">compare_my_object</span><span class="p">)</span>
</pre></div>
</div>
<p>Now, it&#8217;ll get used when comparing objects of that type,
even if they&#8217;re contained within other objects:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="n">MyObject</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="n">MyObject</span><span class="p">(</span><span class="s1">&#39;bar&#39;</span><span class="p">)])</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">sequence not as expected:</span>

<span class="go">same:</span>
<span class="go">[1]</span>

<span class="go">first:</span>
<span class="go">[&lt;MyObject ...&gt;]</span>

<span class="go">second:</span>
<span class="go">[&lt;MyObject ...&gt;]</span>

<span class="go">While comparing [1]: MyObject named &#39;foo&#39; != MyObject named &#39;bar&#39;</span>
</pre></div>
</div>
<p>From this example, you can also see that a comparer can indicate that
two objects are equal, for <a class="reference internal" href="api.html#testfixtures.compare" title="testfixtures.compare"><code class="xref py py-func docutils literal"><span class="pre">compare()</span></code></a>&#8216;s purposes, by returning
<code class="docutils literal"><span class="pre">None</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">MyObject</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="n">MyObject</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">MyObject</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">),</span> <span class="n">MyObject</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>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:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">expected</span><span class="o">=</span><span class="n">MyObject</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">),</span> <span class="n">actual</span><span class="o">=</span><span class="n">MyObject</span><span class="p">(</span><span class="s1">&#39;bar&#39;</span><span class="p">))</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">MyObject named &#39;foo&#39; (expected) != MyObject named &#39;bar&#39; (actual)</span>
</pre></div>
</div>
<p>It may be that you only want to use a comparer or set of
comparers for a particular test. If that&#8217;s the case, you can pass
<a class="reference internal" href="api.html#testfixtures.compare" title="testfixtures.compare"><code class="xref py py-func docutils literal"><span class="pre">compare()</span></code></a> a <code class="docutils literal"><span class="pre">comparers</span></code> parameter consisting of a
dictionary that maps types to comparers. These will be added to the
global registry for the duration of the call:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">MyObject</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">),</span> <span class="n">MyObject</span><span class="p">(</span><span class="s1">&#39;bar&#39;</span><span class="p">),</span>
<span class="gp">... </span>        <span class="n">comparers</span><span class="o">=</span><span class="p">{</span><span class="n">MyObject</span><span class="p">:</span> <span class="n">compare_my_object</span><span class="p">})</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">MyObject named &#39;foo&#39; != MyObject named &#39;bar&#39;</span>
</pre></div>
</div>
<p>A full list of the available comparers included can be found below the
API documentation for <a class="reference internal" href="api.html#testfixtures.compare" title="testfixtures.compare"><code class="xref py py-func docutils literal"><span class="pre">compare()</span></code></a>. 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.</p>
<p>You may be wondering what the <code class="docutils literal"><span class="pre">context</span></code> 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 <a class="reference internal" href="api.html#testfixtures.compare" title="testfixtures.compare"><code class="xref py py-func docutils literal"><span class="pre">compare()</span></code></a>
machinery, it also allows you to pass options to your comparison
function.</p>
<p>For example, you may have an object that has a couple of dictionaries
as attributes:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>

<span class="k">class</span> <span class="nc">Request</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">uri</span><span class="p">,</span> <span class="n">headers</span><span class="p">,</span> <span class="n">body</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">uri</span> <span class="o">=</span> <span class="n">uri</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">headers</span> <span class="o">=</span> <span class="n">headers</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">body</span> <span class="o">=</span> <span class="n">body</span>
</pre></div>
</div>
<p>When your tests encounter instances of these that are not as expected,
you want feedback about which bits of the request or response weren&#8217;t
as expected. This can be achieved by implementing a comparer as
follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">compare_request</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
    <span class="n">uri_different</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">uri</span> <span class="o">!=</span> <span class="n">y</span><span class="o">.</span><span class="n">uri</span>
    <span class="n">headers_different</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">different</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">headers</span><span class="p">,</span> <span class="n">y</span><span class="o">.</span><span class="n">headers</span><span class="p">,</span> <span class="s1">&#39;.headers&#39;</span><span class="p">)</span>
    <span class="n">body_different</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">different</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">body</span><span class="p">,</span> <span class="n">y</span><span class="o">.</span><span class="n">body</span><span class="p">,</span> <span class="s1">&#39;.body&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">uri_different</span> <span class="ow">or</span> <span class="n">headers_different</span> <span class="ow">or</span> <span class="n">body_different</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;Request for </span><span class="si">%r</span><span class="s1"> != Request for </span><span class="si">%r</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
            <span class="n">x</span><span class="o">.</span><span class="n">uri</span><span class="p">,</span> <span class="n">y</span><span class="o">.</span><span class="n">uri</span>
        <span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">A comparer should always return some text when it considers
the two objects it is comparing to be different.</p>
</div>
<p>This comparer can either be registered globally or passed to each
<a class="reference internal" href="api.html#testfixtures.compare" title="testfixtures.compare"><code class="xref py py-func docutils literal"><span class="pre">compare()</span></code></a> call and will give detailed feedback about how the
requests were different:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">Request</span><span class="p">(</span><span class="s1">&#39;/foo&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;method&#39;</span><span class="p">:</span> <span class="s1">&#39;POST&#39;</span><span class="p">},</span> <span class="p">{</span><span class="s1">&#39;my_field&#39;</span><span class="p">:</span> <span class="s1">&#39;value_1&#39;</span><span class="p">}),</span>
<span class="gp">... </span>        <span class="n">Request</span><span class="p">(</span><span class="s1">&#39;/foo&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;method&#39;</span><span class="p">:</span> <span class="s1">&#39;GET&#39;</span><span class="p">},</span> <span class="p">{</span><span class="s1">&#39;my_field&#39;</span><span class="p">:</span> <span class="s1">&#39;value_2&#39;</span><span class="p">}),</span>
<span class="gp">... </span>        <span class="n">comparers</span><span class="o">=</span><span class="p">{</span><span class="n">Request</span><span class="p">:</span> <span class="n">compare_request</span><span class="p">})</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">Request for &#39;/foo&#39; != Request for &#39;/foo&#39;</span>

<span class="go">While comparing .headers: dict not as expected:</span>

<span class="go">values differ:</span>
<span class="go">&#39;method&#39;: &#39;POST&#39; != &#39;GET&#39;</span>

<span class="go">While comparing .headers[&#39;method&#39;]: &#39;POST&#39; != &#39;GET&#39;</span>

<span class="go">While comparing .body: dict not as expected:</span>

<span class="go">values differ:</span>
<span class="go">&#39;my_field&#39;: &#39;value_1&#39; != &#39;value_2&#39;</span>

<span class="go">While comparing .body[&#39;my_field&#39;]: &#39;value_1&#39; != &#39;value_2&#39;</span>
</pre></div>
</div>
<p>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:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">decimal</span> <span class="kn">import</span> <span class="n">Decimal</span>
<span class="kn">from</span> <span class="nn">testfixtures.comparison</span> <span class="kn">import</span> <span class="n">register</span>

<span class="k">def</span> <span class="nf">compare_decimal</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
     <span class="n">precision</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">get_option</span><span class="p">(</span><span class="s1">&#39;precision&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
     <span class="k">if</span> <span class="nb">round</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">precision</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">round</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">precision</span><span class="p">):</span>
         <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%r</span><span class="s1"> != </span><span class="si">%r</span><span class="s1"> when rounded to </span><span class="si">%i</span><span class="s1"> decimal places&#39;</span> <span class="o">%</span> <span class="p">(</span>
             <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">precision</span>
         <span class="p">)</span>

<span class="n">register</span><span class="p">(</span><span class="n">Decimal</span><span class="p">,</span> <span class="n">compare_decimal</span><span class="p">)</span>
</pre></div>
</div>
<p>Now, this comparer will be used for comparing all decimals and the
precision used will be that passed to <a class="reference internal" href="api.html#testfixtures.compare" title="testfixtures.compare"><code class="xref py py-func docutils literal"><span class="pre">compare()</span></code></a>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expected_order</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;price&#39;</span><span class="p">:</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;1.234&#39;</span><span class="p">),</span> <span class="s1">&#39;quantity&#39;</span><span class="p">:</span> <span class="mi">5</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">actual_order</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;price&#39;</span><span class="p">:</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;1.236&#39;</span><span class="p">),</span> <span class="s1">&#39;quantity&#39;</span><span class="p">:</span> <span class="mi">5</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">expected_order</span><span class="p">,</span> <span class="n">actual_order</span><span class="p">,</span> <span class="n">precision</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">expected_order</span><span class="p">,</span> <span class="n">actual_order</span><span class="p">,</span> <span class="n">precision</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">dict not as expected:</span>

<span class="go">same:</span>
<span class="go">[&#39;quantity&#39;]</span>

<span class="go">values differ:</span>
<span class="go">&#39;price&#39;: Decimal(&#39;1.234&#39;) != Decimal(&#39;1.236&#39;)</span>

<span class="go">While comparing [&#39;price&#39;]: Decimal(&#39;1.234&#39;) != Decimal(&#39;1.236&#39;) when rounded to 3 decimal places</span>
</pre></div>
</div>
<p>If no precision is passed, the default of <code class="docutils literal"><span class="pre">2</span></code> will be used:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;2.006&#39;</span><span class="p">),</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;2.009&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;2.001&#39;</span><span class="p">),</span> <span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;2.009&#39;</span><span class="p">))</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">Decimal(&#39;2.001&#39;) != Decimal(&#39;2.009&#39;) when rounded to 2 decimal places</span>
</pre></div>
</div>
</div>
<div class="section" id="ignoring-eq">
<span id="strict-comparison"></span><h3>Ignoring <code class="docutils literal"><span class="pre">__eq__</span></code><a class="headerlink" href="#ignoring-eq" title="Permalink to this headline"></a></h3>
<p>Some objects, such as those from the Django ORM, have pretty broken
implementations or <code class="docutils literal"><span class="pre">__eq__</span></code>. Since <a class="reference internal" href="api.html#testfixtures.compare" title="testfixtures.compare"><code class="xref py py-func docutils literal"><span class="pre">compare()</span></code></a> normally relies on this,
it can result in objects appearing to be equal when they are not.</p>
<p>Take this class, for example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">OrmObj</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="n">a</span>
    <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">True</span>
    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;OrmObj: &#39;</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">a</span><span class="p">)</span>
</pre></div>
</div>
<p>If we compare normally, we erroneously understand the objects to be equal:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">actual</span><span class="o">=</span><span class="n">OrmObj</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">expected</span><span class="o">=</span><span class="n">OrmObj</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>
</pre></div>
</div>
<p>In order to get a sane comparison, we need to both supply a custom comparer
as described above, and use the <code class="docutils literal"><span class="pre">ignore_eq</span></code> parameter:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">compare_orm_obj</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">a</span> <span class="o">!=</span> <span class="n">y</span><span class="o">.</span><span class="n">a</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;OrmObj: </span><span class="si">%s</span><span class="s1"> != </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">a</span><span class="p">,</span> <span class="n">y</span><span class="o">.</span><span class="n">a</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">actual</span><span class="o">=</span><span class="n">OrmObj</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">expected</span><span class="o">=</span><span class="n">OrmObj</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span>
<span class="gp">... </span>        <span class="n">comparers</span><span class="o">=</span><span class="p">{</span><span class="n">OrmObj</span><span class="p">:</span> <span class="n">compare_orm_obj</span><span class="p">},</span> <span class="n">ignore_eq</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">OrmObj: 2 != 1</span>
</pre></div>
</div>
</div>
<div class="section" id="id1">
<h3>Strict comparison<a class="headerlink" href="#id1" title="Permalink to this headline"></a></h3>
<p>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 <a class="reference internal" href="api.html#testfixtures.compare" title="testfixtures.compare"><code class="xref py py-func docutils literal"><span class="pre">compare()</span></code></a> should be used.</p>
<p>For example, these two instances will normally appear to be equal
provided the elements within them are the same:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">TypeA</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;A&#39;</span><span class="p">,</span> <span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">TypeB</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;B&#39;</span><span class="p">,</span> <span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">TypeA</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">TypeB</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
</pre></div>
</div>
<p>If this type difference is important, then the <cite>strict</cite> parameter
should be used:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">TypeA</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">TypeB</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">strict</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">A(x=1) (&lt;class &#39;__main__.A&#39;&gt;) != B(x=1) (&lt;class &#39;__main__.B&#39;&gt;)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="comparison-objects">
<span id="id2"></span><h2>Comparison objects<a class="headerlink" href="#comparison-objects" title="Permalink to this headline"></a></h2>
<p>Another common problem with the checking in tests is that not all
objects support comparison and nor should they need to. For this
reason, TextFixtures provides the <a class="reference internal" href="api.html#testfixtures.Comparison" title="testfixtures.Comparison"><code class="xref py py-class docutils literal"><span class="pre">Comparison</span></code></a>
class.</p>
<p>This class lets you instantiate placeholders that can be used to
compare expected results with actual results where objects in the
actual results do not support useful comparison.</p>
<p>Comparisons will appear to be equal to any object they are compared
with that matches their specification. For example, take the following
class:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">SomeClass</span><span class="p">:</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
       <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
</pre></div>
</div>
<p>Normal comparison doesn&#8217;t work, which makes testing tricky:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">SomeClass</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="o">==</span> <span class="n">SomeClass</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<p>Here&#8217;s how this comparison can be done:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">testfixtures</span> <span class="k">import</span> <span class="n">Comparison</span> <span class="k">as</span> <span class="n">C</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="n">SomeClass</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> <span class="o">==</span> <span class="n">SomeClass</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Perhaps even more importantly, when a comparison fails, its
representation changes to give information about what went wrong. The
common idiom for using comparisons is in conjuction with
<a class="reference external" href="https://docs.python.org/3.5/library/unittest.html#unittest.TestCase.assertEqual" title="(in Python v3.5)"><code class="xref py py-meth docutils literal"><span class="pre">assertEqual()</span></code></a> or
<a class="reference internal" href="api.html#testfixtures.compare" title="testfixtures.compare"><code class="xref py py-meth docutils literal"><span class="pre">compare()</span></code></a>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">C</span><span class="p">(</span><span class="n">SomeClass</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">2</span><span class="p">),</span> <span class="n">SomeClass</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>:
<span class="go">  &lt;C(failed):...SomeClass&gt;</span>
<span class="go">  x:2 != 1</span>
<span class="go">  y:2 not in Comparison</span>
<span class="go">  &lt;/C&gt; != &lt;...SomeClass...&gt;</span>
</pre></div>
</div>
<p>The key is that the comparison object actually stores information
about what it was last compared with. The following example shows this
more clearly:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">C</span><span class="p">(</span><span class="n">SomeClass</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">c</span><span class="p">))</span>

<span class="go">  &lt;C:...SomeClass&gt;</span>
<span class="go">  x:2</span>
<span class="go">  &lt;/C&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">==</span> <span class="n">SomeClass</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">c</span><span class="p">))</span>

<span class="go">  &lt;C(failed):...SomeClass&gt;</span>
<span class="go">  x:2 != 1</span>
<span class="go">  y:2 not in Comparison</span>
<span class="go">  &lt;/C&gt;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><a class="reference external" href="https://docs.python.org/3.5/library/unittest.html#unittest.TestCase.assertEqual" title="(in Python v3.5)"><code class="xref py py-meth docutils literal"><span class="pre">assertEqual()</span></code></a> has regressed in Python 3.4
and will now truncate the text shown in assertions with no way to
configure this behaviour. Use <a class="reference internal" href="api.html#testfixtures.compare" title="testfixtures.compare"><code class="xref py py-func docutils literal"><span class="pre">compare()</span></code></a> instead, which will
give you other desirable behaviour as well as showing you the full
output of failed comparisons.</p>
</div>
<div class="section" id="types-of-comparison">
<h3>Types of comparison<a class="headerlink" href="#types-of-comparison" title="Permalink to this headline"></a></h3>
<p>There are several ways a comparison can be set up depending on what
you want to check.</p>
<p>If you only care about the class of an object, you can set up the
comparison with only the class:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="n">SomeClass</span><span class="p">)</span> <span class="o">==</span> <span class="n">SomeClass</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>This can also be achieved by specifying the type of the object as a
dotted name:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">sys</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="s1">&#39;types.ModuleType&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="n">sys</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Alternatively, if you happen to have a non-comparable object already
around, comparison can be done with it:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="n">SomeClass</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span> <span class="o">==</span> <span class="n">SomeClass</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>If you only care about certain attributes, this can also easily be
achieved with the <cite>strict</cite> parameter:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="n">SomeClass</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> <span class="o">==</span> <span class="n">SomeClass</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>The above can be problematic if you want to compare an object with
attibutes that share names with parameters to the <a class="reference internal" href="api.html#testfixtures.Comparison" title="testfixtures.Comparison"><code class="xref py py-class docutils literal"><span class="pre">Comparison</span></code></a>
constructor. For this reason, you can pass the attributes in a
dictionary:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="n">C</span><span class="p">(</span><span class="n">SomeClass</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;strict&#39;</span><span class="p">:</span><span class="mi">3</span><span class="p">},</span> <span class="n">strict</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span> <span class="n">SomeClass</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">AssertionError</span>:
<span class="go">  &lt;C(failed):...SomeClass&gt;</span>
<span class="go">  strict:3 not in other</span>
<span class="go">  &lt;/C&gt; != &lt;...SomeClass...&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="gotchas">
<h3>Gotchas<a class="headerlink" href="#gotchas" title="Permalink to this headline"></a></h3>
<p>There are a few things to be careful of when using comparisons:</p>
<ul>
<li><p class="first">The default strict comparison cannot be used with a class such as
the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">NoVars</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
     <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>If you try, you will get an error that explains the problem:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="n">NoVars</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="o">==</span> <span class="n">NoVars</span><span class="p">()</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">&lt;NoVars object at ...&gt; does not support vars() so cannot do strict comparison</span>
</pre></div>
</div>
<p>Comparisons can still be done with classes that don&#8217;t support
<code class="docutils literal"><span class="pre">vars()</span></code>, they just need to be non-strict:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">nv</span> <span class="o">=</span> <span class="n">NoVars</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nv</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="n">NoVars</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> <span class="o">==</span> <span class="n">nv</span>
<span class="go">True</span>
</pre></div>
</div>
</li>
</ul>
<ul>
<li><p class="first">If the object being compared has an <code class="docutils literal"><span class="pre">__eq__</span></code> method, such as
Django model instances, then the <a class="reference internal" href="api.html#testfixtures.Comparison" title="testfixtures.Comparison"><code class="xref py py-class docutils literal"><span class="pre">Comparison</span></code></a>
must be the first object in the equality check.</p>
<p>The following class is an example of this:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">SomeModel</span><span class="p">:</span>
    <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span><span class="n">SomeModel</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">True</span>
        <span class="k">return</span> <span class="bp">False</span>
</pre></div>
</div>
<p>It will not work correctly if used as the second object in the
expression:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">SomeModel</span><span class="p">()</span><span class="o">==</span><span class="n">C</span><span class="p">(</span><span class="n">SomeModel</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<p>However, if the comparison is correctly placed first, then
everything will behave as expected:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="n">SomeModel</span><span class="p">)</span><span class="o">==</span><span class="n">SomeModel</span><span class="p">()</span>
<span class="go">True</span>
</pre></div>
</div>
</li>
<li><p class="first">It probably goes without saying, but comparisons should not be used
on both sides of an equality check:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">(</span><span class="n">SomeClass</span><span class="p">)</span><span class="o">==</span><span class="n">C</span><span class="p">(</span><span class="n">SomeClass</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
</li>
</ul>
</div>
</div>
<div class="section" id="round-comparison-objects">
<h2>Round Comparison objects<a class="headerlink" href="#round-comparison-objects" title="Permalink to this headline"></a></h2>
<p>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.</p>
<p>For these situations, you can use <a class="reference internal" href="api.html#testfixtures.RoundComparison" title="testfixtures.RoundComparison"><code class="xref py py-class docutils literal"><span class="pre">RoundComparison</span></code></a> 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.</p>
<p>Here&#8217;s an example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">testfixtures</span> <span class="kn">import</span> <span class="n">compare</span><span class="p">,</span> <span class="n">RoundComparison</span> <span class="k">as</span> <span class="n">R</span>

<span class="n">compare</span><span class="p">(</span><span class="n">R</span><span class="p">(</span><span class="mf">1234.5678</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="mf">1234.5681</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">You should always pass the same type of object to the
<a class="reference internal" href="api.html#testfixtures.RoundComparison" title="testfixtures.RoundComparison"><code class="xref py py-class docutils literal"><span class="pre">RoundComparison</span></code></a> object as you intend compare it with. If
the type of the rounded expected value is not the same as the type of
the rounded value being compared against it, a <a class="reference external" href="https://docs.python.org/3.5/library/exceptions.html#TypeError" title="(in Python v3.5)"><code class="xref py py-class docutils literal"><span class="pre">TypeError</span></code></a>
will be raised.</p>
</div>
</div>
<div class="section" id="range-comparison-objects">
<h2>Range Comparison objects<a class="headerlink" href="#range-comparison-objects" title="Permalink to this headline"></a></h2>
<p>When comparing orderable types just as numbers, dates and time, you may only
know what range a value will fall into. <a class="reference internal" href="api.html#testfixtures.RangeComparison" title="testfixtures.RangeComparison"><code class="xref py py-class docutils literal"><span class="pre">RangeComparison</span></code></a> objects
let you confirm a value is within a certain tolerance or range.</p>
<p>Here&#8217;s an example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">testfixtures</span> <span class="kn">import</span> <span class="n">compare</span><span class="p">,</span> <span class="n">RangeComparison</span> <span class="k">as</span> <span class="n">R</span>

<span class="n">compare</span><span class="p">(</span><span class="n">R</span><span class="p">(</span><span class="mf">123.456</span><span class="p">,</span> <span class="mi">789</span><span class="p">),</span> <span class="n">Decimal</span><span class="p">(</span><span class="mf">555.01</span><span class="p">))</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><a class="reference internal" href="api.html#testfixtures.RangeComparison" title="testfixtures.RangeComparison"><code class="xref py py-class docutils literal"><span class="pre">RangeComparison</span></code></a> is inclusive of both the lower and upper bound.</p>
</div>
</div>
<div class="section" id="string-comparison-objects">
<h2>String Comparison objects<a class="headerlink" href="#string-comparison-objects" title="Permalink to this headline"></a></h2>
<p>When comparing sequences of strings, particularly those comping 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 regular expression.</p>
<p>For these situations, you can use <a class="reference internal" href="api.html#testfixtures.StringComparison" title="testfixtures.StringComparison"><code class="xref py py-class docutils literal"><span class="pre">StringComparison</span></code></a> objects
wherever you would use normal strings, and they will compare equal to
any string that matches the regular expression they are created with.</p>
<p>Here&#8217;s an example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">testfixtures</span> <span class="kn">import</span> <span class="n">compare</span><span class="p">,</span> <span class="n">StringComparison</span> <span class="k">as</span> <span class="n">S</span>

<span class="n">compare</span><span class="p">(</span><span class="n">S</span><span class="p">(</span><span class="s1">&#39;Starting thread \d+&#39;</span><span class="p">),</span><span class="s1">&#39;Starting thread 132356&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="differentiating-chunks-of-text">
<h2>Differentiating chunks of text<a class="headerlink" href="#differentiating-chunks-of-text" title="Permalink to this headline"></a></h2>
<p>TextFixtures 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 <a class="reference external" href="https://docs.python.org/3.5/library/unittest.html#unittest.TestCase.assertEqual" title="(in Python v3.5)"><code class="xref py py-meth docutils literal"><span class="pre">assertEqual()</span></code></a> or just as a
general utility function for comparing two lumps of text.</p>
<p>As an example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">testfixtures</span> <span class="k">import</span> <span class="n">diff</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">diff</span><span class="p">(</span><span class="s1">&#39;line1</span><span class="se">\n</span><span class="s1">line2</span><span class="se">\n</span><span class="s1">line3&#39;</span><span class="p">,</span>
<span class="gp">... </span>           <span class="s1">&#39;line1</span><span class="se">\n</span><span class="s1">lineA</span><span class="se">\n</span><span class="s1">line3&#39;</span><span class="p">))</span>
<span class="go">--- first</span>
<span class="go">+++ second</span>
<span class="go">@@ -1,3 +1,3 @@</span>
<span class="go"> line1</span>
<span class="go">-line2</span>
<span class="go">+lineA</span>
<span class="go"> line3</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Comparing objects and sequences</a><ul>
<li><a class="reference internal" href="#the-compare-function">The compare function</a><ul>
<li><a class="reference internal" href="#sets">sets</a></li>
<li><a class="reference internal" href="#dicts">dicts</a></li>
<li><a class="reference internal" href="#lists-and-tuples">lists and tuples</a></li>
<li><a class="reference internal" href="#namedtuples">namedtuples</a></li>
<li><a class="reference internal" href="#generators">generators</a></li>
<li><a class="reference internal" href="#strings-and-unicodes">strings and unicodes</a></li>
<li><a class="reference internal" href="#recursive-comparison">Recursive comparison</a></li>
<li><a class="reference internal" href="#providing-your-own-comparers">Providing your own comparers</a></li>
<li><a class="reference internal" href="#ignoring-eq">Ignoring <code class="docutils literal"><span class="pre">__eq__</span></code></a></li>
<li><a class="reference internal" href="#id1">Strict comparison</a></li>
</ul>
</li>
<li><a class="reference internal" href="#comparison-objects">Comparison objects</a><ul>
<li><a class="reference internal" href="#types-of-comparison">Types of comparison</a></li>
<li><a class="reference internal" href="#gotchas">Gotchas</a></li>
</ul>
</li>
<li><a class="reference internal" href="#round-comparison-objects">Round Comparison objects</a></li>
<li><a class="reference internal" href="#range-comparison-objects">Range Comparison objects</a></li>
<li><a class="reference internal" href="#string-comparison-objects">String Comparison objects</a></li>
<li><a class="reference internal" href="#differentiating-chunks-of-text">Differentiating chunks of text</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="index.html"
                        title="previous chapter">TestFixtures documentation</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="mocking.html"
                        title="next chapter">Mocking out objects and methods</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/comparing.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="mocking.html" title="Mocking out objects and methods"
             >next</a> |</li>
        <li class="right" >
          <a href="index.html" title="TestFixtures documentation"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">testfixtures 4.14.3 documentation</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2008-2017 Simplistix Ltd, 2016-2017 Chris Withers.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.6.
    </div>
  </body>
</html>