4
4
import os
5
5
import random
6
6
import re
7
+ import urllib .parse
7
8
from base64 import b64encode
8
9
from pathlib import Path
9
10
26
27
}
27
28
28
29
29
- def run (pytester , path = "report.html" , * args ):
30
+ def run (pytester , path = "report.html" , cmd_flags = None , query_params = None ):
31
+ if cmd_flags is None :
32
+ cmd_flags = []
33
+
34
+ if query_params is None :
35
+ query_params = {}
36
+ query_params = urllib .parse .urlencode (query_params )
37
+
30
38
path = pytester .path .joinpath (path )
31
- pytester .runpytest ("--html" , path , * args )
39
+ pytester .runpytest ("--html" , path , * cmd_flags )
32
40
33
41
chrome_options = webdriver .ChromeOptions ()
34
42
if os .environ .get ("CI" , False ):
@@ -48,7 +56,7 @@ def run(pytester, path="report.html", *args):
48
56
continue
49
57
# End workaround
50
58
51
- driver .get (f"file:///reports{ path } " )
59
+ driver .get (f"file:///reports{ path } ? { query_params } " )
52
60
return BeautifulSoup (driver .page_source , "html.parser" )
53
61
finally :
54
62
driver .quit ()
@@ -91,6 +99,10 @@ def get_text(page, selector):
91
99
return get_element (page , selector ).string
92
100
93
101
102
+ def is_collapsed (page , test_name ):
103
+ return get_element (page , f".summary tbody[id$='{ test_name } '] .expander" )
104
+
105
+
94
106
def get_log (page , test_id = None ):
95
107
# TODO(jim) move to get_text (use .contents)
96
108
if test_id :
@@ -267,7 +279,7 @@ def pytest_html_report_title(report):
267
279
268
280
def test_resources_inline_css (self , pytester ):
269
281
pytester .makepyfile ("def test_pass(): pass" )
270
- page = run (pytester , "report.html" , " --self-contained-html" )
282
+ page = run (pytester , cmd_flags = [ " --self-contained-html"] )
271
283
272
284
content = file_content ()
273
285
@@ -349,7 +361,7 @@ def pytest_runtest_makereport(item, call):
349
361
)
350
362
351
363
pytester .makepyfile ("def test_pass(): pass" )
352
- page = run (pytester , "report.html" , " --self-contained-html" )
364
+ page = run (pytester , cmd_flags = [ " --self-contained-html"] )
353
365
354
366
element = page .select_one (".summary a[class='col-links__extra text']" )
355
367
assert_that (element .string ).is_equal_to ("Text" )
@@ -374,7 +386,7 @@ def pytest_runtest_makereport(item, call):
374
386
)
375
387
376
388
pytester .makepyfile ("def test_pass(): pass" )
377
- page = run (pytester , "report.html" , " --self-contained-html" )
389
+ page = run (pytester , cmd_flags = [ " --self-contained-html"] )
378
390
379
391
content_str = json .dumps (content )
380
392
data = b64encode (content_str .encode ("utf-8" )).decode ("ascii" )
@@ -435,7 +447,7 @@ def pytest_runtest_makereport(item, call):
435
447
"""
436
448
)
437
449
pytester .makepyfile ("def test_pass(): pass" )
438
- page = run (pytester , "report.html" , " --self-contained-html" )
450
+ page = run (pytester , cmd_flags = [ " --self-contained-html"] )
439
451
440
452
# element = page.select_one(".summary a[class='col-links__extra image']")
441
453
src = f"data:{ mime_type } ;base64,{ data } "
@@ -463,7 +475,7 @@ def pytest_runtest_makereport(item, call):
463
475
"""
464
476
)
465
477
pytester .makepyfile ("def test_pass(): pass" )
466
- page = run (pytester , "report.html" , " --self-contained-html" )
478
+ page = run (pytester , cmd_flags = [ " --self-contained-html"] )
467
479
468
480
# element = page.select_one(".summary a[class='col-links__extra video']")
469
481
src = f"data:{ mime_type } ;base64,{ data } "
@@ -477,7 +489,7 @@ def pytest_runtest_makereport(item, call):
477
489
478
490
def test_xdist (self , pytester ):
479
491
pytester .makepyfile ("def test_xdist(): pass" )
480
- page = run (pytester , "report.html" , " -n1" )
492
+ page = run (pytester , cmd_flags = [ " -n1"] )
481
493
assert_results (page , passed = 1 )
482
494
483
495
def test_results_table_hook_insert (self , pytester ):
@@ -552,7 +564,7 @@ def test_streams(setup):
552
564
assert True
553
565
"""
554
566
)
555
- page = run (pytester , "report.html" , no_capture )
567
+ page = run (pytester , "report.html" , cmd_flags = [ no_capture ] )
556
568
assert_results (page , passed = 1 )
557
569
558
570
log = get_log (page )
@@ -657,3 +669,95 @@ def test_no_log(self, test_file, pytester):
657
669
assert_that (log ).contains ("No log output captured." )
658
670
for when in ["setup" , "test" , "teardown" ]:
659
671
assert_that (log ).does_not_match (self .LOG_LINE_REGEX .format (when ))
672
+
673
+
674
+ class TestCollapsedQueryParam :
675
+ @pytest .fixture
676
+ def test_file (self ):
677
+ return """
678
+ import pytest
679
+ @pytest.fixture
680
+ def setup():
681
+ error
682
+
683
+ def test_error(setup):
684
+ assert True
685
+
686
+ def test_pass():
687
+ assert True
688
+
689
+ def test_fail():
690
+ assert False
691
+ """
692
+
693
+ def test_default (self , pytester , test_file ):
694
+ pytester .makepyfile (test_file )
695
+ page = run (pytester )
696
+ assert_results (page , passed = 1 , failed = 1 , error = 1 )
697
+
698
+ assert_that (is_collapsed (page , "test_pass" )).is_true ()
699
+ assert_that (is_collapsed (page , "test_fail" )).is_false ()
700
+ assert_that (is_collapsed (page , "test_error::setup" )).is_false ()
701
+
702
+ @pytest .mark .parametrize ("param" , ["failed,error" , "FAILED,eRRoR" ])
703
+ def test_specified (self , pytester , test_file , param ):
704
+ pytester .makepyfile (test_file )
705
+ page = run (pytester , query_params = {"collapsed" : param })
706
+ assert_results (page , passed = 1 , failed = 1 , error = 1 )
707
+
708
+ assert_that (is_collapsed (page , "test_pass" )).is_false ()
709
+ assert_that (is_collapsed (page , "test_fail" )).is_true ()
710
+ assert_that (is_collapsed (page , "test_error::setup" )).is_true ()
711
+
712
+ def test_all (self , pytester , test_file ):
713
+ pytester .makepyfile (test_file )
714
+ page = run (pytester , query_params = {"collapsed" : "all" })
715
+ assert_results (page , passed = 1 , failed = 1 , error = 1 )
716
+
717
+ for test_name in ["test_pass" , "test_fail" , "test_error::setup" ]:
718
+ assert_that (is_collapsed (page , test_name )).is_true ()
719
+
720
+ @pytest .mark .parametrize ("param" , ["" , 'collapsed=""' , "collapsed=''" ])
721
+ def test_falsy (self , pytester , test_file , param ):
722
+ pytester .makepyfile (test_file )
723
+ page = run (pytester , query_params = {"collapsed" : param })
724
+ assert_results (page , passed = 1 , failed = 1 , error = 1 )
725
+
726
+ assert_that (is_collapsed (page , "test_pass" )).is_false ()
727
+ assert_that (is_collapsed (page , "test_fail" )).is_false ()
728
+ assert_that (is_collapsed (page , "test_error::setup" )).is_false ()
729
+
730
+ def test_render_collapsed (self , pytester , test_file ):
731
+ pytester .makeini (
732
+ """
733
+ [pytest]
734
+ render_collapsed = failed,error
735
+ """
736
+ )
737
+ pytester .makepyfile (test_file )
738
+ page = run (pytester )
739
+ assert_results (page , passed = 1 , failed = 1 , error = 1 )
740
+
741
+ assert_that (is_collapsed (page , "test_pass" )).is_false ()
742
+ assert_that (is_collapsed (page , "test_fail" )).is_true ()
743
+ assert_that (is_collapsed (page , "test_error::setup" )).is_true ()
744
+
745
+ def test_render_collapsed_precedence (self , pytester , test_file ):
746
+ pytester .makeini (
747
+ """
748
+ [pytest]
749
+ render_collapsed = failed,error
750
+ """
751
+ )
752
+ test_file += """
753
+ def test_skip():
754
+ pytest.skip('meh')
755
+ """
756
+ pytester .makepyfile (test_file )
757
+ page = run (pytester , query_params = {"collapsed" : "skipped" })
758
+ assert_results (page , passed = 1 , failed = 1 , error = 1 , skipped = 1 )
759
+
760
+ assert_that (is_collapsed (page , "test_pass" )).is_false ()
761
+ assert_that (is_collapsed (page , "test_fail" )).is_false ()
762
+ assert_that (is_collapsed (page , "test_error::setup" )).is_false ()
763
+ assert_that (is_collapsed (page , "test_skip" )).is_true ()
0 commit comments