Skip to content

Commit 23d34c9

Browse files
committed
BUG: Various minor improvements on test_show_versions (pandas-dev#39701)
1 parent 691b1a2 commit 23d34c9

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

pandas/tests/util/test_show_versions.py

+15-21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
import pandas as pd
1313

1414

15+
def get_captured_output(capsys):
16+
return capsys.readouterr().out
17+
18+
1519
@pytest.mark.filterwarnings(
1620
# openpyxl
1721
"ignore:defusedxml.lxml is no longer supported:DeprecationWarning"
@@ -38,51 +42,41 @@ def test_show_versions(tmpdir):
3842

3943
pd.show_versions(as_json=as_json)
4044

41-
# make sure that the file was created
42-
assert os.path.exists(as_json)
43-
4445
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-
5146
# 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)
5348

5449
# Basic check that each version element is found in output
55-
version_elements = {
50+
expected = {
5651
"system": _get_sys_info(),
5752
"dependencies": _get_dependency_info(),
5853
}
5954

60-
assert version_elements == dict_check
55+
assert result == expected
6156

6257

6358
def test_show_versions_console_json(capsys):
6459
pd.show_versions(as_json=True)
65-
captured = capsys.readouterr()
66-
result = captured.out
60+
result = get_captured_output(capsys)
6761

6862
# check valid json is printed to the console if as_json is True
69-
dict_check = json.loads(result)
63+
result = json.loads(result)
7064

7165
# Basic check that each version element is found in output
72-
version_elements = {
66+
expected = {
7367
"system": _get_sys_info(),
7468
"dependencies": _get_dependency_info(),
7569
}
7670

77-
assert version_elements == dict_check
71+
assert result == expected
7872

7973

8074
def test_show_versions_console(capsys):
8175
# gh-32041
8276
pd.show_versions(as_json=False)
83-
captured = capsys.readouterr()
84-
result = captured.out
77+
result = get_captured_output(capsys)
8578

79+
# check header
8680
assert "INSTALLED VERSIONS" in result
8781

8882
# check full commit hash
@@ -98,11 +92,11 @@ def test_show_versions_console(capsys):
9892

9993
def test_json_output_match(capsys, tmpdir):
10094
pd.show_versions(as_json=True)
101-
result_console = capsys.readouterr().out
95+
result_console = get_captured_output(capsys)
10296

10397
out_path = os.path.join(tmpdir, "test_json.json")
10498
pd.show_versions(as_json=out_path)
10599
with open(out_path) as out_fd:
106-
result_file = "".join(out_fd.readlines())
100+
result_file = out_fd.read()
107101

108102
assert result_console == result_file

0 commit comments

Comments
 (0)