12
12
import pandas as pd
13
13
14
14
15
+ def get_captured_output (capsys ):
16
+ return capsys .readouterr ().out
17
+
18
+
15
19
@pytest .mark .filterwarnings (
16
20
# openpyxl
17
21
"ignore:defusedxml.lxml is no longer supported:DeprecationWarning"
@@ -38,51 +42,41 @@ def test_show_versions(tmpdir):
38
42
39
43
pd .show_versions (as_json = as_json )
40
44
41
- # make sure that the file was created
42
- assert os .path .exists (as_json )
43
-
44
45
with open (as_json ) as fd :
45
- contents = fd .readlines ()
46
- str_contents = "" .join (contents )
47
-
48
- # make sure that there was output to the file
49
- assert str_contents
50
-
51
46
# check if file output is valid JSON, will raise an exception if not
52
- dict_check = json .loads ( str_contents )
47
+ result = json .load ( fd )
53
48
54
49
# Basic check that each version element is found in output
55
- version_elements = {
50
+ expected = {
56
51
"system" : _get_sys_info (),
57
52
"dependencies" : _get_dependency_info (),
58
53
}
59
54
60
- assert version_elements == dict_check
55
+ assert result == expected
61
56
62
57
63
58
def test_show_versions_console_json (capsys ):
64
59
pd .show_versions (as_json = True )
65
- captured = capsys .readouterr ()
66
- result = captured .out
60
+ result = get_captured_output (capsys )
67
61
68
62
# check valid json is printed to the console if as_json is True
69
- dict_check = json .loads (result )
63
+ result = json .loads (result )
70
64
71
65
# Basic check that each version element is found in output
72
- version_elements = {
66
+ expected = {
73
67
"system" : _get_sys_info (),
74
68
"dependencies" : _get_dependency_info (),
75
69
}
76
70
77
- assert version_elements == dict_check
71
+ assert result == expected
78
72
79
73
80
74
def test_show_versions_console (capsys ):
81
75
# gh-32041
82
76
pd .show_versions (as_json = False )
83
- captured = capsys .readouterr ()
84
- result = captured .out
77
+ result = get_captured_output (capsys )
85
78
79
+ # check header
86
80
assert "INSTALLED VERSIONS" in result
87
81
88
82
# check full commit hash
@@ -98,11 +92,11 @@ def test_show_versions_console(capsys):
98
92
99
93
def test_json_output_match (capsys , tmpdir ):
100
94
pd .show_versions (as_json = True )
101
- result_console = capsys . readouterr (). out
95
+ result_console = get_captured_output ( capsys )
102
96
103
97
out_path = os .path .join (tmpdir , "test_json.json" )
104
98
pd .show_versions (as_json = out_path )
105
99
with open (out_path ) as out_fd :
106
- result_file = "" . join ( out_fd .readlines () )
100
+ result_file = out_fd .read ( )
107
101
108
102
assert result_console == result_file
0 commit comments