@@ -452,43 +452,59 @@ def test_to_html_invalid_justify(justify):
452
452
df .to_html (justify = justify )
453
453
454
454
455
- def test_to_html_index (datapath ):
456
- # TODO: split this test
457
- index = ["foo" , "bar" , "baz" ]
458
- df = DataFrame (
459
- {"A" : [1 , 2 , 3 ], "B" : [1.2 , 3.4 , 5.6 ], "C" : ["one" , "two" , np .nan ]},
460
- columns = ["A" , "B" , "C" ],
461
- index = index ,
462
- )
463
- expected_with_index = expected_html (datapath , "index_1" )
464
- assert df .to_html () == expected_with_index
465
-
466
- expected_without_index = expected_html (datapath , "index_2" )
467
- result = df .to_html (index = False )
468
- for i in index :
469
- assert i not in result
470
- assert result == expected_without_index
471
- df .index = Index (["foo" , "bar" , "baz" ], name = "idx" )
472
- expected_with_index = expected_html (datapath , "index_3" )
473
- assert df .to_html () == expected_with_index
474
- assert df .to_html (index = False ) == expected_without_index
475
-
476
- tuples = [("foo" , "car" ), ("foo" , "bike" ), ("bar" , "car" )]
477
- df .index = MultiIndex .from_tuples (tuples )
478
-
479
- expected_with_index = expected_html (datapath , "index_4" )
480
- assert df .to_html () == expected_with_index
455
+ class TestHTMLIndex :
456
+ @pytest .fixture
457
+ def df (self ):
458
+ index = ["foo" , "bar" , "baz" ]
459
+ df = DataFrame (
460
+ {"A" : [1 , 2 , 3 ], "B" : [1.2 , 3.4 , 5.6 ], "C" : ["one" , "two" , np .nan ]},
461
+ columns = ["A" , "B" , "C" ],
462
+ index = index ,
463
+ )
464
+ return df
481
465
482
- result = df .to_html (index = False )
483
- for i in ["foo" , "bar" , "car" , "bike" ]:
484
- assert i not in result
485
- # must be the same result as normal index
486
- assert result == expected_without_index
487
-
488
- df .index = MultiIndex .from_tuples (tuples , names = ["idx1" , "idx2" ])
489
- expected_with_index = expected_html (datapath , "index_5" )
490
- assert df .to_html () == expected_with_index
491
- assert df .to_html (index = False ) == expected_without_index
466
+ @pytest .fixture
467
+ def expected_without_index (self , datapath ):
468
+ return expected_html (datapath , "index_2" )
469
+
470
+ def test_to_html_flat_index_without_name (
471
+ self , datapath , df , expected_without_index
472
+ ):
473
+ expected_with_index = expected_html (datapath , "index_1" )
474
+ assert df .to_html () == expected_with_index
475
+
476
+ result = df .to_html (index = False )
477
+ for i in df .index :
478
+ assert i not in result
479
+ assert result == expected_without_index
480
+
481
+ def test_to_html_flat_index_with_name (self , datapath , df , expected_without_index ):
482
+ df .index = Index (["foo" , "bar" , "baz" ], name = "idx" )
483
+ expected_with_index = expected_html (datapath , "index_3" )
484
+ assert df .to_html () == expected_with_index
485
+ assert df .to_html (index = False ) == expected_without_index
486
+
487
+ def test_to_html_multiindex_without_names (
488
+ self , datapath , df , expected_without_index
489
+ ):
490
+ tuples = [("foo" , "car" ), ("foo" , "bike" ), ("bar" , "car" )]
491
+ df .index = MultiIndex .from_tuples (tuples )
492
+
493
+ expected_with_index = expected_html (datapath , "index_4" )
494
+ assert df .to_html () == expected_with_index
495
+
496
+ result = df .to_html (index = False )
497
+ for i in ["foo" , "bar" , "car" , "bike" ]:
498
+ assert i not in result
499
+ # must be the same result as normal index
500
+ assert result == expected_without_index
501
+
502
+ def test_to_html_multiindex_with_names (self , datapath , df , expected_without_index ):
503
+ tuples = [("foo" , "car" ), ("foo" , "bike" ), ("bar" , "car" )]
504
+ df .index = MultiIndex .from_tuples (tuples , names = ["idx1" , "idx2" ])
505
+ expected_with_index = expected_html (datapath , "index_5" )
506
+ assert df .to_html () == expected_with_index
507
+ assert df .to_html (index = False ) == expected_without_index
492
508
493
509
494
510
@pytest .mark .parametrize ("classes" , ["sortable draggable" , ["sortable" , "draggable" ]])
0 commit comments