summaryrefslogtreecommitdiff
blob: 872f1cfaa51c194caafecb46b070273a1a100167 (plain)
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
<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

    <title>Tests in Python packages &#8212; Gentoo Python Guide  documentation</title>
    <link rel="stylesheet" type="text/css" href="_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="_static/alabaster.css" />
    <script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
    <script src="_static/doctools.js"></script>
    <script src="_static/sphinx_highlight.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="distutils-r1 legacy concepts" href="distutils-legacy.html" />
    <link rel="prev" title="distutils-r1 — standard Python build systems" href="distutils.html" />
   
  <link rel="stylesheet" href="_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <section id="tests-in-python-packages">
<h1>Tests in Python packages<a class="headerlink" href="#tests-in-python-packages" title="Permalink to this heading"></a></h1>
<section id="why-is-running-tests-important">
<h2>Why is running tests important?<a class="headerlink" href="#why-is-running-tests-important" title="Permalink to this heading"></a></h2>
<p>Since Python performs only minimal build-time (or more precisely,
import-time) checking of correctness, it is important to run tests
of Python packages in order to catch any problems early.  This is
especially important for permitting others to verify support for new
Python implementations.</p>
</section>
<section id="using-distutils-enable-tests">
<span id="index-0"></span><h2>Using distutils_enable_tests<a class="headerlink" href="#using-distutils-enable-tests" title="Permalink to this heading"></a></h2>
<section id="basic-use-case">
<h3>Basic use case<a class="headerlink" href="#basic-use-case" title="Permalink to this heading"></a></h3>
<p>The simplest way of enabling tests is to call <code class="docutils literal notranslate"><span class="pre">distutils_enable_tests</span></code>
in global scope, passing the test runner name as the first argument.
This function takes care of declaring test phase, setting appropriate
dependencies and <code class="docutils literal notranslate"><span class="pre">test</span></code> USE flag if necessary.  If called after
setting <code class="docutils literal notranslate"><span class="pre">RDEPEND</span></code>, it also copies it to test dependencies.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="w"> </span><span class="c1"># Copyright 1999-2020 Gentoo Authors</span>
<span class="w"> </span><span class="c1"># Distributed under the terms of the GNU General Public License v2</span>

<span class="w"> </span><span class="nv">EAPI</span><span class="o">=</span><span class="m">7</span>

<span class="w"> </span><span class="nv">PYTHON_COMPAT</span><span class="o">=(</span><span class="w"> </span>python2_7<span class="w"> </span>python3_<span class="o">{</span><span class="m">6</span>,7,8<span class="o">}</span><span class="w"> </span>pypy3<span class="w"> </span><span class="o">)</span>
<span class="w"> </span>inherit<span class="w"> </span>distutils-r1

<span class="w"> </span><span class="nv">DESCRIPTION</span><span class="o">=</span><span class="s2">&quot;An easy whitelist-based HTML-sanitizing tool&quot;</span>
<span class="w"> </span><span class="nv">HOMEPAGE</span><span class="o">=</span><span class="s2">&quot;https://github.com/mozilla/bleach https://pypi.org/project/bleach/&quot;</span>
<span class="w"> </span><span class="nv">SRC_URI</span><span class="o">=</span><span class="s2">&quot;mirror://pypi/</span><span class="si">${</span><span class="nv">PN</span><span class="p">:</span><span class="nv">0</span><span class="p">:</span><span class="nv">1</span><span class="si">}</span><span class="s2">/</span><span class="si">${</span><span class="nv">PN</span><span class="si">}</span><span class="s2">/</span><span class="si">${</span><span class="nv">P</span><span class="si">}</span><span class="s2">.tar.gz&quot;</span>

<span class="w"> </span><span class="nv">LICENSE</span><span class="o">=</span><span class="s2">&quot;Apache-2.0&quot;</span>
<span class="w"> </span><span class="nv">SLOT</span><span class="o">=</span><span class="s2">&quot;0&quot;</span>
<span class="w"> </span><span class="nv">KEYWORDS</span><span class="o">=</span><span class="s2">&quot;~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sparc ~x86&quot;</span>

<span class="w"> </span><span class="nv">RDEPEND</span><span class="o">=</span><span class="s2">&quot;</span>
<span class="s2">     dev-python/six[</span><span class="si">${</span><span class="nv">PYTHON_USEDEP</span><span class="si">}</span><span class="s2">]</span>
<span class="s2">     dev-python/webencodings[</span><span class="si">${</span><span class="nv">PYTHON_USEDEP</span><span class="si">}</span><span class="s2">]</span>
<span class="s2"> &quot;</span>

<span class="hll"><span class="w"> </span>distutils_enable_tests<span class="w"> </span>pytest
</span></pre></div>
</div>
<p>The valid values include:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">nose</span></code> for <code class="docutils literal notranslate"><span class="pre">dev-python/nose</span></code> (<em>deprecated</em>)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">pytest</span></code> for <code class="docutils literal notranslate"><span class="pre">dev-python/pytest</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">setup.py</span></code> to call <code class="docutils literal notranslate"><span class="pre">setup.py</span> <span class="pre">test</span></code> (<em>deprecated</em>)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">unittest</span></code> to use built-in unittest discovery</p></li>
</ul>
<p>See <a class="reference internal" href="#choosing-the-correct-test-runner">choosing the correct test runner</a> for more information.</p>
</section>
<section id="adding-more-test-dependencies">
<h3>Adding more test dependencies<a class="headerlink" href="#adding-more-test-dependencies" title="Permalink to this heading"></a></h3>
<p>Additional test dependencies can be specified in <code class="docutils literal notranslate"><span class="pre">test?</span></code> conditional.
The flag normally does not need to be explicitly declared,
as <code class="docutils literal notranslate"><span class="pre">distutils_enable_tests</span></code> does that in the majority of cases.</p>
<p>Please read the section on <a class="reference internal" href="#undesirable-test-dependencies">undesirable test dependencies</a> too.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="w"> </span><span class="c1"># Copyright 1999-2020 Gentoo Authors</span>
<span class="w"> </span><span class="c1"># Distributed under the terms of the GNU General Public License v2</span>

<span class="w"> </span><span class="nv">EAPI</span><span class="o">=</span><span class="m">6</span>

<span class="w"> </span><span class="nv">PYTHON_COMPAT</span><span class="o">=(</span><span class="w"> </span>python2_7<span class="w"> </span>python3_<span class="o">{</span><span class="m">6</span>,7,8<span class="o">}</span><span class="w"> </span>pypy3<span class="w"> </span><span class="o">)</span>
<span class="w"> </span>inherit<span class="w"> </span>distutils-r1

<span class="w"> </span><span class="nv">DESCRIPTION</span><span class="o">=</span><span class="s2">&quot;Universal encoding detector&quot;</span>
<span class="w"> </span><span class="nv">HOMEPAGE</span><span class="o">=</span><span class="s2">&quot;https://github.com/chardet/chardet https://pypi.org/project/chardet/&quot;</span>
<span class="w"> </span><span class="nv">SRC_URI</span><span class="o">=</span><span class="s2">&quot;https://github.com/chardet/chardet/archive/</span><span class="si">${</span><span class="nv">PV</span><span class="si">}</span><span class="s2">.tar.gz -&gt; </span><span class="si">${</span><span class="nv">P</span><span class="si">}</span><span class="s2">.tar.gz&quot;</span>

<span class="w"> </span><span class="nv">LICENSE</span><span class="o">=</span><span class="s2">&quot;LGPL-2.1&quot;</span>
<span class="w"> </span><span class="nv">SLOT</span><span class="o">=</span><span class="s2">&quot;0&quot;</span>
<span class="w"> </span><span class="nv">KEYWORDS</span><span class="o">=</span><span class="s2">&quot;~alpha amd64 arm arm64 hppa ia64 ~m68k ~mips ppc ppc64 s390 ~sh sparc x86 ~x64-cygwin ~amd64-linux ~x86-linux ~x64-macos ~x86-macos ~x64-solaris&quot;</span>

<span class="w"> </span><span class="nv">DEPEND</span><span class="o">=</span><span class="s2">&quot;</span>
<span class="hll"><span class="s2">     test? ( dev-python/hypothesis[</span><span class="si">${</span><span class="nv">PYTHON_USEDEP</span><span class="si">}</span><span class="s2">] )</span>
</span><span class="s2"> &quot;</span>

<span class="hll"><span class="w"> </span>distutils_enable_tests<span class="w"> </span>pytest
</span></pre></div>
</div>
<p>Note that <code class="docutils literal notranslate"><span class="pre">distutils_enable_tests</span></code> modifies variables directly
in the ebuild environment.  This means that if you wish to change their
values, you need to append to them, i.e. the bottom part of that ebuild
can be rewritten as:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="w"> </span>distutils_enable_tests<span class="w"> </span>pytest

<span class="hll"><span class="w"> </span><span class="nv">DEPEND</span><span class="o">+=</span><span class="s2">&quot;</span>
</span><span class="s2">     test? ( dev-python/hypothesis[</span><span class="si">${</span><span class="nv">PYTHON_USEDEP</span><span class="si">}</span><span class="s2">] )</span>
<span class="s2"> &quot;</span>
</pre></div>
</div>
</section>
<section id="installing-the-package-before-running-tests">
<h3>Installing the package before running tests<a class="headerlink" href="#installing-the-package-before-running-tests" title="Permalink to this heading"></a></h3>
<p>In PEP 517 mode, the eclass automatically exposes a venv-style install
tree to the test phase.  No explicit action in necessary.</p>
<p>In the legacy mode, <code class="docutils literal notranslate"><span class="pre">distutils_enable_tests</span></code> has an optional
<code class="docutils literal notranslate"><span class="pre">--install</span></code> option that can be used to force performing an install
to a temporary directory.  More information can be found in the legacy
section.</p>
</section>
<section id="customizing-the-test-phase">
<h3>Customizing the test phase<a class="headerlink" href="#customizing-the-test-phase" title="Permalink to this heading"></a></h3>
<p>If additional pre-/post-test phase actions need to be performed,
they can be easily injected via overriding <code class="docutils literal notranslate"><span class="pre">src_test()</span></code> and making
it call <code class="docutils literal notranslate"><span class="pre">distutils-r1_src_test</span></code>:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="w"> </span><span class="c1"># Copyright 1999-2020 Gentoo Authors</span>
<span class="w"> </span><span class="c1"># Distributed under the terms of the GNU General Public License v2</span>

<span class="w"> </span><span class="nv">EAPI</span><span class="o">=</span><span class="m">7</span>

<span class="w"> </span><span class="nv">PYTHON_COMPAT</span><span class="o">=(</span><span class="w"> </span>python3_<span class="o">{</span><span class="m">6</span>,7,8<span class="o">}</span><span class="w"> </span><span class="o">)</span>
<span class="w"> </span>inherit<span class="w"> </span>distutils-r1

<span class="w"> </span><span class="nv">DESCRIPTION</span><span class="o">=</span><span class="s2">&quot;Extra features for standard library&#39;s cmd module&quot;</span>
<span class="w"> </span><span class="nv">HOMEPAGE</span><span class="o">=</span><span class="s2">&quot;https://github.com/python-cmd2/cmd2&quot;</span>
<span class="w"> </span><span class="nv">SRC_URI</span><span class="o">=</span><span class="s2">&quot;mirror://pypi/</span><span class="si">${</span><span class="nv">PN</span><span class="p">:</span><span class="nv">0</span><span class="p">:</span><span class="nv">1</span><span class="si">}</span><span class="s2">/</span><span class="si">${</span><span class="nv">PN</span><span class="si">}</span><span class="s2">/</span><span class="si">${</span><span class="nv">P</span><span class="si">}</span><span class="s2">.tar.gz&quot;</span>

<span class="w"> </span><span class="nv">LICENSE</span><span class="o">=</span><span class="s2">&quot;MIT&quot;</span>
<span class="w"> </span><span class="nv">SLOT</span><span class="o">=</span><span class="s2">&quot;0&quot;</span>
<span class="w"> </span><span class="nv">KEYWORDS</span><span class="o">=</span><span class="s2">&quot;~amd64 ~arm ~arm64 ~ppc64 ~x86 ~amd64-linux ~x86-linux&quot;</span>

<span class="w"> </span><span class="nv">RDEPEND</span><span class="o">=</span><span class="s2">&quot;</span>
<span class="s2">     dev-python/attrs[</span><span class="si">${</span><span class="nv">PYTHON_USEDEP</span><span class="si">}</span><span class="s2">]</span>
<span class="s2">     &gt;=dev-python/colorama-0.3.7[</span><span class="si">${</span><span class="nv">PYTHON_USEDEP</span><span class="si">}</span><span class="s2">]</span>
<span class="s2">     &gt;=dev-python/pyperclip-1.6[</span><span class="si">${</span><span class="nv">PYTHON_USEDEP</span><span class="si">}</span><span class="s2">]</span>
<span class="s2">     dev-python/six[</span><span class="si">${</span><span class="nv">PYTHON_USEDEP</span><span class="si">}</span><span class="s2">]</span>
<span class="s2">     dev-python/wcwidth[</span><span class="si">${</span><span class="nv">PYTHON_USEDEP</span><span class="si">}</span><span class="s2">]</span>
<span class="s2"> &quot;</span>
<span class="w"> </span><span class="nv">BDEPEND</span><span class="o">=</span><span class="s2">&quot;</span>
<span class="s2">     dev-python/setuptools_scm[</span><span class="si">${</span><span class="nv">PYTHON_USEDEP</span><span class="si">}</span><span class="s2">]</span>
<span class="s2"> &quot;</span>

<span class="w"> </span>distutils_enable_tests<span class="w"> </span>pytest

<span class="hll"><span class="w"> </span>src_test<span class="o">()</span><span class="w"> </span><span class="o">{</span>
</span><span class="hll"><span class="w">     </span><span class="c1"># tests rely on very specific text wrapping...</span>
</span><span class="hll"><span class="w">     </span><span class="nb">local</span><span class="w"> </span>-x<span class="w"> </span><span class="nv">COLUMNS</span><span class="o">=</span><span class="m">80</span>
</span><span class="hll"><span class="w">     </span>distutils-r1_src_test
</span><span class="hll"><span class="w"> </span><span class="o">}</span>
</span></pre></div>
</div>
<p>If the actual test command needs to be customized, or a non-standard
test tool needs to be used, you can define a <code class="docutils literal notranslate"><span class="pre">python_test()</span></code> sub-phase
function.  This function is called for every enabled Python target
by the default <code class="docutils literal notranslate"><span class="pre">src_test</span></code> implementation.  This can either be combined
with <code class="docutils literal notranslate"><span class="pre">distutils_enable_tests</span></code> call, or used instead of it.  In fact,
the former function simply defines a <code class="docutils literal notranslate"><span class="pre">python_test()</span></code> function as part
of its logic.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="w"> </span><span class="c1"># Copyright 1999-2020 Gentoo Authors</span>
<span class="w"> </span><span class="c1"># Distributed under the terms of the GNU General Public License v2</span>

<span class="w"> </span><span class="nv">EAPI</span><span class="o">=</span><span class="m">7</span>

<span class="w"> </span><span class="nv">PYTHON_COMPAT</span><span class="o">=(</span><span class="w"> </span>python<span class="o">{</span>2_7,3_6,3_7,3_8<span class="o">}</span><span class="w"> </span>pypy3<span class="w"> </span><span class="o">)</span>
<span class="w"> </span>inherit<span class="w"> </span>distutils-r1

<span class="w"> </span><span class="nv">DESCRIPTION</span><span class="o">=</span><span class="s2">&quot;Bash tab completion for argparse&quot;</span>
<span class="w"> </span><span class="nv">HOMEPAGE</span><span class="o">=</span><span class="s2">&quot;https://pypi.org/project/argcomplete/&quot;</span>
<span class="w"> </span><span class="nv">SRC_URI</span><span class="o">=</span><span class="s2">&quot;mirror://pypi/</span><span class="si">${</span><span class="nv">PN</span><span class="p">:</span><span class="nv">0</span><span class="p">:</span><span class="nv">1</span><span class="si">}</span><span class="s2">/</span><span class="si">${</span><span class="nv">PN</span><span class="si">}</span><span class="s2">/</span><span class="si">${</span><span class="nv">P</span><span class="si">}</span><span class="s2">.tar.gz&quot;</span>

<span class="w"> </span><span class="nv">LICENSE</span><span class="o">=</span><span class="s2">&quot;Apache-2.0&quot;</span>
<span class="w"> </span><span class="nv">SLOT</span><span class="o">=</span><span class="s2">&quot;0&quot;</span>
<span class="w"> </span><span class="nv">KEYWORDS</span><span class="o">=</span><span class="s2">&quot;~amd64 ~arm ~arm64 ~hppa ~x86 ~amd64-linux ~x86-linux ~x64-macos&quot;</span>
<span class="hll"><span class="w"> </span><span class="nv">IUSE</span><span class="o">=</span><span class="s2">&quot;test&quot;</span>
</span><span class="hll"><span class="w"> </span><span class="nv">RESTRICT</span><span class="o">=</span><span class="s2">&quot;!test? ( test )&quot;</span>
</span>
<span class="w"> </span><span class="nv">RDEPEND</span><span class="o">=</span><span class="s2">&quot;</span>
<span class="s2">     </span><span class="k">$(</span>python_gen_cond_dep<span class="w"> </span><span class="s1">&#39;</span>
<span class="s1">         &lt;dev-python/importlib_metadata-2[${PYTHON_USEDEP}]</span>
<span class="s1">     &#39;</span><span class="w"> </span>-2<span class="w"> </span>python3_<span class="o">{</span><span class="m">5</span>,6,7<span class="o">}</span><span class="w"> </span>pypy3<span class="k">)</span><span class="s2">&quot;</span>
<span class="w"> </span><span class="c1"># pip is called as an external tool</span>
<span class="w"> </span><span class="nv">BDEPEND</span><span class="o">=</span><span class="s2">&quot;</span>
<span class="s2">     dev-python/setuptools[</span><span class="si">${</span><span class="nv">PYTHON_USEDEP</span><span class="si">}</span><span class="s2">]</span>
<span class="hll"><span class="s2">     test? (</span>
</span><span class="hll"><span class="s2">         app-shells/fish</span>
</span><span class="hll"><span class="s2">         app-shells/tcsh</span>
</span><span class="hll"><span class="s2">         dev-python/pexpect[</span><span class="si">${</span><span class="nv">PYTHON_USEDEP</span><span class="si">}</span><span class="s2">]</span>
</span><span class="hll"><span class="s2">         dev-python/pip</span>
</span><span class="hll"><span class="s2">     )&quot;</span>
</span>
<span class="hll"><span class="w"> </span>python_test<span class="o">()</span><span class="w"> </span><span class="o">{</span>
</span><span class="hll"><span class="w">     </span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">EPYTHON</span><span class="si">}</span><span class="s2">&quot;</span><span class="w"> </span>test/test.py<span class="w"> </span>-v<span class="w"> </span><span class="o">||</span><span class="w"> </span>die
</span><span class="hll"><span class="w"> </span><span class="o">}</span>
</span></pre></div>
</div>
<p>Note that <code class="docutils literal notranslate"><span class="pre">python_test</span></code> is called by <code class="docutils literal notranslate"><span class="pre">distutils-r1_src_test</span></code>,
so you must make sure to call it if you override <code class="docutils literal notranslate"><span class="pre">src_test</span></code>.</p>
</section>
<section id="customizing-the-test-phase-for-pytest">
<span id="index-1"></span><h3>Customizing the test phase for pytest<a class="headerlink" href="#customizing-the-test-phase-for-pytest" title="Permalink to this heading"></a></h3>
<p>For the relatively frequent case of pytest-based packages needing
additional customization, a <code class="docutils literal notranslate"><span class="pre">epytest</span></code> helper is provided.  The helper
runs <code class="docutils literal notranslate"><span class="pre">pytest</span></code> with a standard set of options and automatic handling
of test failures.</p>
<p>For example, if upstream uses <code class="docutils literal notranslate"><span class="pre">network</span></code> marker to disable
network-based tests, you can override the test phase in the following
way:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>distutils_enable_tests<span class="w"> </span>pytest

python_test<span class="o">()</span><span class="w"> </span><span class="o">{</span>
<span class="w">    </span>epytest<span class="w"> </span>-m<span class="w"> </span><span class="s1">&#39;not network&#39;</span>
<span class="o">}</span>
</pre></div>
</div>
</section>
<section id="running-tests-with-virtualx">
<span id="index-2"></span><h3>Running tests with virtualx<a class="headerlink" href="#running-tests-with-virtualx" title="Permalink to this heading"></a></h3>
<p>Test suites requiring a display to work correctly can often be appeased
usng Xvfb.  If the package in question does not start Xvfb directly,
<code class="docutils literal notranslate"><span class="pre">virtualx.eclass</span></code> can be used to do that.  Whenever possible, it is
preferable to run a single server in <code class="docutils literal notranslate"><span class="pre">src_test()</span></code> for all passes
of the test suite, e.g.:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>distutils_enable_tests<span class="w"> </span>pytest

src_test<span class="o">()</span><span class="w"> </span><span class="o">{</span>
<span class="w">    </span>virtx<span class="w"> </span>distutils-r1_src_test
<span class="o">}</span>
</pre></div>
</div>
<p>Note that <code class="docutils literal notranslate"><span class="pre">virtx</span></code> implicitly enables nonfatal mode.  This means that
e.g. <code class="docutils literal notranslate"><span class="pre">epytest</span></code> will no longer terminate the ebuild on failure, and you
need to use <code class="docutils literal notranslate"><span class="pre">die</span></code> explicitly for it:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>src_test<span class="o">()</span><span class="w"> </span><span class="o">{</span>
<span class="w">    </span>virtx<span class="w"> </span>distutils-r1_src_test
<span class="o">}</span>

python_test<span class="o">()</span><span class="w"> </span><span class="o">{</span>
<span class="w">    </span>epytest<span class="w"> </span>-m<span class="w"> </span><span class="s2">&quot;not network&quot;</span><span class="w"> </span><span class="o">||</span><span class="w"> </span>die<span class="w"> </span><span class="s2">&quot;Tests failed with </span><span class="si">${</span><span class="nv">EPYTHON</span><span class="si">}</span><span class="s2">&quot;</span>
<span class="o">}</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>Explicit <code class="docutils literal notranslate"><span class="pre">||</span> <span class="pre">die</span></code> is only necessary when overriding <code class="docutils literal notranslate"><span class="pre">python_test</span></code>
and running <code class="docutils literal notranslate"><span class="pre">epytest</span></code> inside a <code class="docutils literal notranslate"><span class="pre">nonfatal</span></code>.  The <code class="docutils literal notranslate"><span class="pre">virtx</span></code> command
runs its arguments via a <code class="docutils literal notranslate"><span class="pre">nonfatal</span></code>.  The default <code class="docutils literal notranslate"><span class="pre">python_test</span></code>
implementation created by <code class="docutils literal notranslate"><span class="pre">distutils_enable_tests</span></code> accounts for
this.  In other contexts, <code class="docutils literal notranslate"><span class="pre">epytest</span></code> will die on its own.</p>
</div>
</section>
</section>
<section id="choosing-the-correct-test-runner">
<h2>Choosing the correct test runner<a class="headerlink" href="#choosing-the-correct-test-runner" title="Permalink to this heading"></a></h2>
<p>There are a few modules used to run tests in Python packages.  The most
common include the built-in <a class="reference external" href="https://docs.python.org/3/library/unittest.html">unittest</a> module, <a class="reference external" href="https://docs.pytest.org/en/latest/">pytest</a> and <a class="reference external" href="https://github.com/nose-devs/nose">nose</a>.  There
are also some rarely used test tools and domain-specific solutions,
e.g. <a class="reference external" href="https://www.djangoproject.com/">django</a> has its own test runner.  This section will help you
determining which test runner to use and depend on.</p>
<p>Firstly, it is a good idea to look at test sources.  Explicit imports
clearly indicate that a particular test runner needs to be installed,
and most likely used.  For example, if at least one test file has
<code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">pytest</span></code>, pytest is the obvious choice.  If it has <code class="docutils literal notranslate"><span class="pre">import</span>
<span class="pre">nose</span></code>, same goes for nosetests.</p>
<p>In some rare cases the tests may use multiple test packages
simultaneously.  In this case, you need to choose one of the test
runners (see other suggestions) but depend on all of them.</p>
<p>Secondly, some test suites are relying on <em>implicit</em> features of a test
runner.  For example, pytest and nose have less strict naming
and structural requirements for test cases.  In some cases, unittest
runner will simply be unable to find all tests.</p>
<p>Thirdly, there are cases when a particular feature of a test runner
is desired even if it is not strictly necessary to run tests.  This
is particularly the case with pytest’s output capture that can make
test output much more readable with particularly verbose packages.</p>
<p>Upstream documentation, tox configuration, CI pipelines can provide tips
on the test runner to be used.  However, you should establish whether
this information is wholly correct and up-to-date, and whether
the particular test tool is really desirable.</p>
<p>If the test suite requires no particular runner (i.e. works with
built-in unittest module), using it is preferable to avoid unnecessary
dependencies.  However, you need to make sure that it finds all tests
correctly (i.e. runs no less tests than the alternative) and that it
does not spew too much irrelevant output.</p>
<p>If both pytest and nose seem equally good, the former is recommended
as the latter has ceased development and requires downstream patching.
If you have some free time, convincing upstream to switch from nose
to pytest is a worthwhile goal.</p>
</section>
<section id="undesirable-test-dependencies">
<h2>Undesirable test dependencies<a class="headerlink" href="#undesirable-test-dependencies" title="Permalink to this heading"></a></h2>
<p>There is a number of packages that are frequently listed as test
dependencies upstream but have little to no value for Gentoo users.
It is recommended to skip those test dependencies whenever possible.
If tests fail to run without them, it is often preferable to strip
the dependencies and/or configuration values enforcing them.</p>
<p><em>Coverage testing</em> establishes how much of the package’s code is covered
by the test suite.  While this is useful statistic upstream, it has
no value for Gentoo users who just want to install the package.  This
is often represented by dependencies on <code class="docutils literal notranslate"><span class="pre">dev-python/coverage</span></code>,
<code class="docutils literal notranslate"><span class="pre">dev-python/pytest-cov</span></code>.  In the latter case, you usually need
to strip <code class="docutils literal notranslate"><span class="pre">--cov</span></code> parameter from <code class="docutils literal notranslate"><span class="pre">pytest.ini</span></code>.</p>
<p><em>PEP-8 testing</em> enforces standard coding style across Python programs.
Issues found by it are relevant to upstream but entirely irrelevant
to Gentoo users.  If the package uses <code class="docutils literal notranslate"><span class="pre">dev-python/pep8</span></code>,
<code class="docutils literal notranslate"><span class="pre">dev-python/pycodestyle</span></code>, <code class="docutils literal notranslate"><span class="pre">dev-python/flake8</span></code>, strip that
dependency.</p>
<p><code class="docutils literal notranslate"><span class="pre">dev-python/pytest-runner</span></code> is a thin wrapper to run pytest
from <code class="docutils literal notranslate"><span class="pre">setup.py</span></code>.  Do not use it, just call pytest directly.</p>
<p><code class="docutils literal notranslate"><span class="pre">dev-python/tox</span></code> is a convenient wrapper to run tests for multiple
Python versions, in a virtualenv.  The eclass already provides the logic
for the former, and an environment close enough to the latter.  Do not
use tox in ebuilds.</p>
</section>
<section id="missing-test-files-in-pypi-packages">
<h2>Missing test files in PyPI packages<a class="headerlink" href="#missing-test-files-in-pypi-packages" title="Permalink to this heading"></a></h2>
<p>One of the more common test-related problems is that PyPI packages
(generated via <code class="docutils literal notranslate"><span class="pre">setup.py</span> <span class="pre">sdist</span></code>) often miss some or all test files.
The latter results in no tests being run, the former in test failures
or errors.</p>
<p>The simplest solution is to use a VCS snapshot instead of the PyPI
tarball:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="c1"># pypi tarballs are missing test data</span>
<span class="c1">#SRC_URI=&quot;mirror://pypi/${PN:0:1}/${PN}/${P}.tar.gz&quot;</span>
<span class="nv">SRC_URI</span><span class="o">=</span><span class="s2">&quot;https://github.com/</span><span class="si">${</span><span class="nv">PN</span><span class="si">}</span><span class="s2">/</span><span class="si">${</span><span class="nv">PN</span><span class="si">}</span><span class="s2">/archive/</span><span class="si">${</span><span class="nv">PV</span><span class="si">}</span><span class="s2">.tar.gz -&gt; </span><span class="si">${</span><span class="nv">P</span><span class="si">}</span><span class="s2">.gh.tar.gz&quot;</span>
</pre></div>
</div>
</section>
<section id="importerrors-for-c-extensions">
<h2>ImportErrors for C extensions<a class="headerlink" href="#importerrors-for-c-extensions" title="Permalink to this heading"></a></h2>
<p>Tests are often invoked in such a way that the Python packages
and modules from the current directory take precedence over these found
in the staging area or build directory.  In fact, this is often
necessary to prevent import collisions — e.g. when modules would
be loaded first from the staging area due to explicit imports
then again from the current directory due to test discovery.</p>
<p>Unfortunately, this does not work correctly if C extensions are built
as part of these packages.  Since the package imported relatively
does not include the necessary extensions, the imports fail, e.g.:</p>
<div class="highlight-pytb notranslate"><div class="highlight"><pre><span></span><span class="x">____________________ ERROR collecting systemd/test/test_login.py ____________________</span>
<span class="x">ImportError while importing test module &#39;/tmp/portage/dev-python/python-systemd-234-r</span>
<span class="x">2/work/python-systemd-234/systemd/test/test_login.py&#39;.</span>
<span class="x">Hint: make sure your test modules/packages have valid Python names.</span>
<span class="x">Traceback:</span>
<span class="x">/usr/lib/python3.8/site-packages/_pytest/python.py:578: in _importtestmodule</span>
<span class="x">    mod = import_path(self.fspath, mode=importmode)</span>
<span class="x">/usr/lib/python3.8/site-packages/_pytest/pathlib.py:524: in import_path</span>
<span class="x">    importlib.import_module(module_name)</span>
<span class="x">/usr/lib/python3.8/importlib/__init__.py:127: in import_module</span>
<span class="x">    return _bootstrap._gcd_import(name[level:], package, level)</span>
<span class="x">&lt;frozen importlib._bootstrap&gt;:1014: in _gcd_import</span>
<span class="x">    ???</span>
<span class="x">&lt;frozen importlib._bootstrap&gt;:991: in _find_and_load</span>
<span class="x">    ???</span>
<span class="x">&lt;frozen importlib._bootstrap&gt;:975: in _find_and_load_unlocked</span>
<span class="x">    ???</span>
<span class="x">&lt;frozen importlib._bootstrap&gt;:671: in _load_unlocked</span>
<span class="x">    ???</span>
<span class="x">/usr/lib/python3.8/site-packages/_pytest/assertion/rewrite.py:170: in exec_module</span>
<span class="x">    exec(co, module.__dict__)</span>
<span class="x">systemd/test/test_login.py:6: in &lt;module&gt;</span>
<span class="x">    from systemd import login</span>
<span class="x">E   ImportError: cannot import name &#39;login&#39; from &#39;systemd&#39; (/tmp/portage/dev-python/python-systemd-234-r2/work/python-systemd-234/systemd/__init__.py)</span>
</pre></div>
</div>
<p>The preferred solution is to change the working directory before running
the tests.  If tests are installed as a part of the package, they can
be discovered through package search e.g. using pytest:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>python_test<span class="o">()</span><span class="w"> </span><span class="o">{</span>
<span class="w">    </span><span class="nb">cd</span><span class="w"> </span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">T</span><span class="si">}</span><span class="s2">&quot;</span><span class="w"> </span><span class="o">||</span><span class="w"> </span>die
<span class="w">    </span>epytest<span class="w"> </span>--pyargs<span class="w"> </span>systemd
<span class="o">}</span>
</pre></div>
</div>
<p>If the tests are in a separate directory, an absolute path can be used
instead:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>python_test<span class="o">()</span><span class="w"> </span><span class="o">{</span>
<span class="w">    </span><span class="nb">cd</span><span class="w"> </span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">T</span><span class="si">}</span><span class="s2">&quot;</span><span class="w"> </span><span class="o">||</span><span class="w"> </span>die
<span class="w">    </span>epytest<span class="w"> </span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">S</span><span class="si">}</span><span class="s2">&quot;</span>/test
<span class="o">}</span>
</pre></div>
</div>
</section>
<section id="checklist-for-dealing-with-test-failures">
<h2>Checklist for dealing with test failures<a class="headerlink" href="#checklist-for-dealing-with-test-failures" title="Permalink to this heading"></a></h2>
<p>If you see some test failures but do not have a guess as to why they
would be happening, try the following for a start:</p>
<ol class="arabic simple">
<li><p>Check upstream CI (if any).  That’s a quick way of verifying that
there is no known breakage at the relevant tag.</p></li>
<li><p>Try running tests as your regular user, the way upstream suggests
(e.g. via <code class="docutils literal notranslate"><span class="pre">tox</span></code>).  Try using a git checkout at the specific tag.
This is the basic way of determining whether the package is actually
broken or if it is something on our end.</p></li>
<li><p>If the tests fail at the specified tag, try upstream master branch.
Maybe there was a fix committed.</p></li>
</ol>
<p>If it seems that the issue is on our end, try the following and see
if it causes the subset of failing tests to change:</p>
<ol class="arabic">
<li><p>Make sure that the test runner is started via <code class="docutils literal notranslate"><span class="pre">${EPYTHON}</span></code>
(the eclass-provided <code class="docutils literal notranslate"><span class="pre">epytest</span></code> and <code class="docutils literal notranslate"><span class="pre">eunittest</span></code> wrappers do that).
Calling system executables directly (either Python via absolute path
or system-installed tools that use absolute path in their shebangs)
may cause just-built modules not to be imported correctly.</p></li>
<li><p>Try running the test suite from another directory.  If you’re seeing
failures to load compiled extensions, Python may be wrongly importing
modules from the current directory instead of the build/install tree.
Some test suite also depend on paths relative to where upstream run
tests.</p></li>
<li><p>Either switch to PEP 517 mode (preferred), or
add <code class="docutils literal notranslate"><span class="pre">distutils_install_for_testing</span></code> to the test sub-phase or
<code class="docutils literal notranslate"><span class="pre">--install</span></code> to the <code class="docutils literal notranslate"><span class="pre">distutils_enable_tests</span></code> call.
This resolves the majority of problems that arise from the test suite
requiring the package to be installed prior to testing.</p></li>
<li><p>Actually install the package to the system (with tests disabled).
This can confirm cases of package for whom the above function
does not work.  In the worst case, you can set a test self-dependency
to force users to install the package before testing:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>test?<span class="w"> </span><span class="o">(</span><span class="w"> </span>~dev-python/myself-<span class="si">${</span><span class="nv">PV</span><span class="si">}</span><span class="w"> </span><span class="o">)</span>
</pre></div>
</div>
</li>
<li><p>Try testing a different Python implementation.  If a subset of tests
is failing with Python 3.6, see if it still happens with 3.7 or 3.8.
If 3.8 is passing but 3.9 is not, it’s most likely some
incompatibility upstream did not account for.</p></li>
<li><p>Run tests with <code class="docutils literal notranslate"><span class="pre">FEATURES=-network-sandbox</span></code>.  Sometimes lack
of Internet access causes non-obvious failures.</p></li>
<li><p>Try a different test runner.  Sometimes the subtle differences
in how tests are executed can lead to test failures.  But beware:
some test runners may not run the full set of tests, so verify
that you have actually fixed them and not just caused them to
be skipped.</p></li>
</ol>
</section>
<section id="skipping-problematic-tests">
<h2>Skipping problematic tests<a class="headerlink" href="#skipping-problematic-tests" title="Permalink to this heading"></a></h2>
<p>While generally it is preferable to fix tests, sometimes you will face
failures that cannot be easily resolved.  This especially applies
to tests that are broken themselves rather than indicating real problems
with the software.  However, in some cases you will even find yourself
ignoring minor test failures.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>When possible, it is preferable to use pytest along with its
convenient ignore/deselect options to skip problematic tests.
Using pytest instead of unittest is usually possible.</p>
</div>
<p>Tests that are known to fail reliably can be marked as <em>expected
failures</em>.  This has the advantage that the test in question will
continue being run and the test suite will report when it unexpectedly
starts passing again.</p>
<p>Expected failures are not supported by the standard Python unittest
module.  It is supported e.g. by pytest.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>sed<span class="w"> </span>-i<span class="w"> </span>-e<span class="w"> </span><span class="se">\</span>
<span class="w">    </span><span class="s2">&quot;/def test_start_params_bug():/i@pytest.mark.xfail(reason=&#39;Known to fail on Gentoo&#39;)&quot;</span><span class="w"> </span><span class="se">\</span>
<span class="w">    </span>statsmodels/tsa/tests/test_arima.py<span class="w"> </span><span class="o">||</span><span class="w"> </span>die
</pre></div>
</div>
<p>Tests that cause inconsistent results, trigger errors, consume
horrendous amounts of disk space or cause another kind of undesirable
mayhem can be <em>skipped</em> instead.  Skipping means that they will not be
run at all.</p>
<p>There are multiple ways to skip a test.  You can patch it to use a skip
decorator, possibly with a condition:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="c1"># broken on py2.7, upstream knows</span>
sed<span class="w"> </span>-i<span class="w"> </span>-e<span class="w"> </span><span class="s1">&#39;5a\</span>
<span class="s1">import sys&#39;</span><span class="w"> </span><span class="se">\</span>
<span class="w">    </span>-e<span class="w"> </span><span class="s1">&#39;/test_null_bytes/i\</span>
<span class="s1">@pytest.mark.skipif(sys.hexversion &lt; 0x03000000, reason=&quot;broken on py2&quot;)&#39;</span><span class="w"> </span><span class="se">\</span>
<span class="w">    </span>test/server.py<span class="w"> </span><span class="o">||</span><span class="w"> </span>die
</pre></div>
</div>
<p>The easy way to skip a test unconditioanlly is to prefix its name with
an underscore:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="c1"># tests requiring specific locales</span>
sed<span class="w"> </span>-i<span class="w"> </span>-e<span class="w"> </span><span class="s1">&#39;s:test_babel_with_language_:_&amp;:&#39;</span><span class="w"> </span><span class="se">\</span>
<span class="w">    </span>tests/test_build_latex.py<span class="w"> </span><span class="o">||</span><span class="w"> </span>die
sed<span class="w"> </span>-i<span class="w"> </span>-e<span class="w"> </span><span class="s1">&#39;s:test_polyglossia_with_language_:_&amp;:&#39;</span><span class="w"> </span><span class="se">\</span>
<span class="w">    </span>tests/test_build_latex.py<span class="w"> </span><span class="o">||</span><span class="w"> </span>die
</pre></div>
</div>
<p>Finally, if all tests in a particular file are problematic, you can
simply remove that file.  If all tests belonging to the package
are broken, you can use <code class="docutils literal notranslate"><span class="pre">RESTRICT=test</span></code> to disable testing altogether.</p>
</section>
<section id="tests-requiring-internet-access">
<h2>Tests requiring Internet access<a class="headerlink" href="#tests-requiring-internet-access" title="Permalink to this heading"></a></h2>
<p>One of more common causes of test failures are attempts to use Internet.
With Portage blocking network access by default, packages performing
tests against remote servers often fail.</p>
<p>Ideally, packages would use mocking or replay tests rather than using
real Internet services.  Devmanual provides a detailed explanation <a class="reference external" href="https://devmanual.gentoo.org/ebuild-writing/functions/src_test/#tests-that-require-network-or-service-access">why
tests must not use Internet</a>.</p>
<p>Some packages provide explicit methods of disabling network-based tests.
For example, <code class="docutils literal notranslate"><span class="pre">dev-python/tox</span></code> provides a switch for that:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>python_test<span class="o">()</span><span class="w"> </span><span class="o">{</span>
<span class="w">    </span>distutils_install_for_testing
<span class="w">    </span>epytest<span class="w"> </span>--no-network
<span class="o">}</span>
</pre></div>
</div>
<p>There are packages that skip tests if they fail specifically due to
connection errors, or detect whether Internet is accessible.  Ideally,
you should modify those packages to disable network tests
unconditionally.  For example, <code class="docutils literal notranslate"><span class="pre">dev-python/pygit2</span></code> ebuild does this:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="c1"># unconditionally prevent it from using network</span>
sed<span class="w"> </span>-i<span class="w"> </span>-e<span class="w"> </span><span class="s1">&#39;/def no_network/a \</span>
<span class="s1">    return True&#39;</span><span class="w"> </span>test/utils.py<span class="w"> </span><span class="o">||</span><span class="w"> </span>die
</pre></div>
</div>
<p>In other cases, you will have to explicitly disable these tests.
In some cases, it will be reasonable to remove whole test files or even
restrict tests entirely.</p>
<p>If the package’s test suite relies on Internet access entirely and there
is no point in running even a subset of tests, please implement running
tests and combine test restriction with <code class="docutils literal notranslate"><span class="pre">PROPERTIES=test_network</span></code>
to allow interested users to run tests when possible:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="c1"># users can use ALLOW_TEST=network to override this</span>
<span class="nv">PROPERTIES</span><span class="o">=</span><span class="s2">&quot;test_network&quot;</span>
<span class="nv">RESTRICT</span><span class="o">=</span><span class="s2">&quot;test&quot;</span>

distutils_enable_tests<span class="w"> </span>pytest
</pre></div>
</div>
</section>
<section id="tests-aborting-due-to-assertions">
<h2>Tests aborting (due to assertions)<a class="headerlink" href="#tests-aborting-due-to-assertions" title="Permalink to this heading"></a></h2>
<p>There are cases of package’s tests terminating with an unclear error
message and backtrace similar to the following:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">=============================</span><span class="w"> </span><span class="nb">test</span><span class="w"> </span>session<span class="w"> </span><span class="nv">starts</span><span class="w"> </span><span class="o">==============================</span>
platform<span class="w"> </span>linux<span class="w"> </span>--<span class="w"> </span>Python<span class="w"> </span><span class="m">3</span>.7.8,<span class="w"> </span>pytest-6.0.1,<span class="w"> </span>py-1.9.0,<span class="w"> </span>pluggy-0.13.1<span class="w"> </span>--<span class="w"> </span>/usr/bin/python3.7
cachedir:<span class="w"> </span>.pytest_cache
rootdir:<span class="w"> </span>/var/tmp/portage/dev-python/sabyenc-4.0.2/work/sabyenc-4.0.2,<span class="w"> </span>configfile:<span class="w"> </span>pytest.ini
collecting<span class="w"> </span>...<span class="w"> </span>collected<span class="w"> </span><span class="m">24</span><span class="w"> </span>items

<span class="o">[</span>...<span class="o">]</span>
tests/test_decoder.py::test_crc_pickles<span class="w"> </span>PASSED<span class="w">                           </span><span class="o">[</span><span class="w"> </span><span class="m">54</span>%<span class="o">]</span>
tests/test_decoder.py::test_empty_size_pickles<span class="w"> </span>Fatal<span class="w"> </span>Python<span class="w"> </span>error:<span class="w"> </span>Aborted

Current<span class="w"> </span>thread<span class="w"> </span>0x00007f748bc47740<span class="w"> </span><span class="o">(</span>most<span class="w"> </span>recent<span class="w"> </span>call<span class="w"> </span>first<span class="o">)</span>:
<span class="w">  </span>File<span class="w"> </span><span class="s2">&quot;/var/tmp/portage/dev-python/sabyenc-4.0.2/work/sabyenc-4.0.2/tests/testsupport.py&quot;</span>,<span class="w"> </span>line<span class="w"> </span><span class="m">74</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>sabyenc3_wrapper
<span class="w">  </span>File<span class="w"> </span><span class="s2">&quot;/var/tmp/portage/dev-python/sabyenc-4.0.2/work/sabyenc-4.0.2/tests/test_decoder.py&quot;</span>,<span class="w"> </span>line<span class="w"> </span><span class="m">119</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>test_empty_size_pickles
<span class="w">  </span>File<span class="w"> </span><span class="s2">&quot;/usr/lib/python3.7/site-packages/_pytest/python.py&quot;</span>,<span class="w"> </span>line<span class="w"> </span><span class="m">180</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>pytest_pyfunc_call
<span class="w">  </span>File<span class="w"> </span><span class="s2">&quot;/usr/lib/python3.7/site-packages/pluggy/callers.py&quot;</span>,<span class="w"> </span>line<span class="w"> </span><span class="m">187</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>_multicall
<span class="w">  </span><span class="o">[</span>...<span class="o">]</span>
<span class="w">  </span>File<span class="w"> </span><span class="s2">&quot;/usr/lib/python-exec/python3.7/pytest&quot;</span>,<span class="w"> </span>line<span class="w"> </span><span class="m">11</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>&lt;module&gt;
/var/tmp/portage/dev-python/sabyenc-4.0.2/temp/environment:<span class="w"> </span>line<span class="w"> </span><span class="m">2934</span>:<span class="w">    </span><span class="m">66</span><span class="w"> </span>Aborted<span class="w">                 </span><span class="o">(</span>core<span class="w"> </span>dumped<span class="o">)</span><span class="w"> </span>pytest<span class="w"> </span>-vv
</pre></div>
</div>
<p>This usually indicates that the C code of some Python extension failed
an assertion.  Since pytest does not print captured output when exiting
due to a signal, you need to disable output capture (using <code class="docutils literal notranslate"><span class="pre">-s</span></code>)
to get a more useful error, e.g.:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ python3.7 -m pytest -s
=============================================================== test session starts ===============================================================
platform linux -- Python 3.7.8, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /tmp/sabyenc, configfile: pytest.ini
plugins: asyncio-0.14.0, forked-1.3.0, xdist-1.34.0, hypothesis-5.23.9, mock-3.2.0, flaky-3.7.0, timeout-1.4.2, freezegun-0.4.2
collected 25 items

tests/test_decoder.py .............python3.7: src/sabyenc3.c:596: decode_usenet_chunks: Assertion `PyByteArray_Check(PyList_GetItem(Py_input_list, lp))&#39; failed.
Fatal Python error: Aborted

Current thread 0x00007fb5db746740 (most recent call first):
  File &quot;/tmp/sabyenc/tests/testsupport.py&quot;, line 73 in sabyenc3_wrapper
  File &quot;/tmp/sabyenc/tests/test_decoder.py&quot;, line 117 in test_empty_size_pickles
  File &quot;/usr/lib/python3.7/site-packages/_pytest/python.py&quot;, line 180 in pytest_pyfunc_call
  File &quot;/usr/lib/python3.7/site-packages/pluggy/callers.py&quot;, line 187 in _multicall
  File &quot;/usr/lib/python3.7/site-packages/pluggy/manager.py&quot;, line 87 in &lt;lambda&gt;
  [...]
  File &quot;/usr/lib/python3.7/site-packages/pytest/__main__.py&quot;, line 7 in &lt;module&gt;
  File &quot;/usr/lib/python3.7/runpy.py&quot;, line 85 in _run_code
  File &quot;/usr/lib/python3.7/runpy.py&quot;, line 193 in _run_module_as_main
Aborted (core dumped)
</pre></div>
</div>
<p>Now the message clearly indicates the failed assertion.</p>
<p>It is also common that upstream is initially unable to reproduce
the bug.  This is because Ubuntu and many other common distributions
build Python with <code class="docutils literal notranslate"><span class="pre">-DNDEBUG</span></code> and the flag leaks to extension builds.
As a result, all assertions are stripped at build time.  Upstream
can work around that by explicitly setting <code class="docutils literal notranslate"><span class="pre">CFLAGS</span></code> for the build,
e.g.:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span><span class="nv">CFLAGS</span><span class="o">=</span><span class="s1">&#39;-O0 -g&#39;</span><span class="w"> </span>python<span class="w"> </span>setup.py<span class="w"> </span>build<span class="w"> </span>build_ext<span class="w"> </span>-i
$<span class="w"> </span>pytest<span class="w"> </span>-s
</pre></div>
</div>
</section>
<section id="installing-extra-dependencies-in-test-environment-pep-517-mode">
<h2>Installing extra dependencies in test environment (PEP 517 mode)<a class="headerlink" href="#installing-extra-dependencies-in-test-environment-pep-517-mode" title="Permalink to this heading"></a></h2>
<p>Rarely, the test suite expects some package being installed that
does not fit being packaged and installed system-wide.  For example,
isort’s tests use a few example plugins that are not useful to end
users, or pip’s test suite still requires old virtualenv that collides
with the modern versions.  These problems can be resolved by installing
the packages locally within the ebuild.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">distutils-r1.eclass</span></code> provides a <code class="docutils literal notranslate"><span class="pre">distutils_pep517_install</span></code>
helper that can be used to install additional packages.  Please note
that this helper is intended for expert users only, and special care
needs to be taken when using it.  The function takes a single argument
specifying the destination install root, and installs the package
from the current directory.</p>
<p>For example, <code class="docutils literal notranslate"><span class="pre">dev-python/isort</span></code> uses the following test phase
to duplicate the install tree and then install additional packages
into it for the purpose of testing.  Note that <code class="docutils literal notranslate"><span class="pre">PATH</span></code> is manipulated
(rather than <code class="docutils literal notranslate"><span class="pre">PYTHONPATH</span></code>) to use virtualenv-style install root.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>python_test<span class="o">()</span><span class="w"> </span><span class="o">{</span>
<span class="w">    </span>cp<span class="w"> </span>-a<span class="w"> </span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">BUILD_DIR</span><span class="si">}</span><span class="s2">&quot;</span>/<span class="o">{</span>install,test<span class="o">}</span><span class="w"> </span><span class="o">||</span><span class="w"> </span>die
<span class="w">    </span><span class="nb">local</span><span class="w"> </span>-x<span class="w"> </span><span class="nv">PATH</span><span class="o">=</span><span class="si">${</span><span class="nv">BUILD_DIR</span><span class="si">}</span>/test/usr/bin:<span class="si">${</span><span class="nv">PATH</span><span class="si">}</span>

<span class="w">    </span><span class="c1"># Install necessary plugins</span>
<span class="w">    </span><span class="nb">local</span><span class="w"> </span>p
<span class="w">    </span><span class="k">for</span><span class="w"> </span>p<span class="w"> </span><span class="k">in</span><span class="w"> </span>example*/<span class="p">;</span><span class="w"> </span><span class="k">do</span>
<span class="w">        </span><span class="nb">pushd</span><span class="w"> </span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">p</span><span class="si">}</span><span class="s2">&quot;</span><span class="w"> </span>&gt;/dev/null<span class="w"> </span><span class="o">||</span><span class="w"> </span>die
<span class="w">        </span>distutils_pep517_install<span class="w"> </span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">BUILD_DIR</span><span class="si">}</span><span class="s2">&quot;</span>/test
<span class="w">        </span><span class="nb">popd</span><span class="w"> </span>&gt;/dev/null<span class="w"> </span><span class="o">||</span><span class="w"> </span>die
<span class="w">    </span><span class="k">done</span>

<span class="w">    </span>epytest
<span class="o">}</span>
</pre></div>
</div>
</section>
</section>


          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="index.html">Gentoo Python Guide</a></h1>








<h3>Navigation</h3>
<p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="preface.html">Preface</a></li>
<li class="toctree-l1"><a class="reference internal" href="interpreter.html">Python interpreters</a></li>
<li class="toctree-l1"><a class="reference internal" href="eclass.html">Choosing between Python eclasses</a></li>
<li class="toctree-l1"><a class="reference internal" href="basic.html">Common basics</a></li>
<li class="toctree-l1"><a class="reference internal" href="any.html">python-any-r1 — build-time dependency</a></li>
<li class="toctree-l1"><a class="reference internal" href="single.html">python-single-r1 — single-impl packages</a></li>
<li class="toctree-l1"><a class="reference internal" href="multi.html">python-r1 — multi-impl packages</a></li>
<li class="toctree-l1"><a class="reference internal" href="distutils.html">distutils-r1 — standard Python build systems</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Tests in Python packages</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#why-is-running-tests-important">Why is running tests important?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#using-distutils-enable-tests">Using distutils_enable_tests</a></li>
<li class="toctree-l2"><a class="reference internal" href="#choosing-the-correct-test-runner">Choosing the correct test runner</a></li>
<li class="toctree-l2"><a class="reference internal" href="#undesirable-test-dependencies">Undesirable test dependencies</a></li>
<li class="toctree-l2"><a class="reference internal" href="#missing-test-files-in-pypi-packages">Missing test files in PyPI packages</a></li>
<li class="toctree-l2"><a class="reference internal" href="#importerrors-for-c-extensions">ImportErrors for C extensions</a></li>
<li class="toctree-l2"><a class="reference internal" href="#checklist-for-dealing-with-test-failures">Checklist for dealing with test failures</a></li>
<li class="toctree-l2"><a class="reference internal" href="#skipping-problematic-tests">Skipping problematic tests</a></li>
<li class="toctree-l2"><a class="reference internal" href="#tests-requiring-internet-access">Tests requiring Internet access</a></li>
<li class="toctree-l2"><a class="reference internal" href="#tests-aborting-due-to-assertions">Tests aborting (due to assertions)</a></li>
<li class="toctree-l2"><a class="reference internal" href="#installing-extra-dependencies-in-test-environment-pep-517-mode">Installing extra dependencies in test environment (PEP 517 mode)</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="distutils-legacy.html">distutils-r1 legacy concepts</a></li>
<li class="toctree-l1"><a class="reference internal" href="pypi.html">pypi — helper eclass for PyPI archives</a></li>
<li class="toctree-l1"><a class="reference internal" href="helper.html">Common helper functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="depend.html">Advanced dependencies</a></li>
<li class="toctree-l1"><a class="reference internal" href="pytest.html">pytest recipes</a></li>
<li class="toctree-l1"><a class="reference internal" href="concept.html">Advanced concepts</a></li>
<li class="toctree-l1"><a class="reference internal" href="expert-multi.html">Expert python-r1 usage</a></li>
<li class="toctree-l1"><a class="reference internal" href="buildsys.html">Integration with build systems written in Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="porting.html">Porting tips</a></li>
<li class="toctree-l1"><a class="reference internal" href="migration.html">Migration guides</a></li>
<li class="toctree-l1"><a class="reference internal" href="qawarn.html">QA checks and warnings</a></li>
<li class="toctree-l1"><a class="reference internal" href="package-maintenance.html">Python package maintenance</a></li>
<li class="toctree-l1"><a class="reference internal" href="interpreter-maintenance.html">Maintenance of Python implementations</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="index.html">Documentation overview</a><ul>
      <li>Previous: <a href="distutils.html" title="previous chapter">distutils-r1 — standard Python build systems</a></li>
      <li>Next: <a href="distutils-legacy.html" title="next chapter">distutils-r1 legacy concepts</a></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2020, Michał Górny, license: CC BY 4.0.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 7.0.1</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.13</a>
      
      |
      <a href="_sources/test.rst.txt"
          rel="nofollow">Page source</a>
    </div>

    

    
  </body>
</html>