Skip to content

Commit 2ed08c1

Browse files
TST: coverage of backend fallback in headless mode
Co-authored-by: Ian Thomas <ianthomas23@gmail.com>
1 parent f45707d commit 2ed08c1

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

lib/matplotlib/tests/test_rcparams.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,11 @@ def test_rcparams_reset_after_fail():
521521

522522

523523
@pytest.mark.skipif(sys.platform != "linux", reason="Linux only")
524-
def test_backend_fallback_headless(tmp_path):
524+
def test_backend_fallback_headless_invalid_backend(tmp_path):
525525
env = {**os.environ,
526526
"DISPLAY": "", "WAYLAND_DISPLAY": "",
527527
"MPLBACKEND": "", "MPLCONFIGDIR": str(tmp_path)}
528+
# plotting should fail with the tkagg backend selected in a headless environment
528529
with pytest.raises(subprocess.CalledProcessError):
529530
subprocess_run_for_testing(
530531
[sys.executable, "-c",
@@ -536,6 +537,28 @@ def test_backend_fallback_headless(tmp_path):
536537
env=env, check=True, stderr=subprocess.DEVNULL)
537538

538539

540+
@pytest.mark.skipif(sys.platform != "linux", reason="Linux only")
541+
def test_backend_fallback_headless_auto_backend(tmp_path):
542+
# specify a headless mpl environment, but request a graphical (tk) backend
543+
env = {**os.environ,
544+
"DISPLAY": "", "WAYLAND_DISPLAY": "",
545+
"MPLBACKEND": "TkAgg", "MPLCONFIGDIR": str(tmp_path)}
546+
547+
# allow fallback to an available interactive backend explicitly in configuration
548+
rc_path = tmp_path / "matplotlibrc"
549+
rc_path.write_text("backend_fallback: true")
550+
551+
# plotting should succeed, by falling back to use the generic agg backend
552+
backend = subprocess_run_for_testing(
553+
[sys.executable, "-c",
554+
"import matplotlib.pyplot;"
555+
"matplotlib.pyplot.plot(42);"
556+
"print(matplotlib.get_backend());"
557+
],
558+
env=env, text=True, check=True, capture_output=True).stdout
559+
assert backend.strip().lower() == "agg"
560+
561+
539562
@pytest.mark.skipif(
540563
sys.platform == "linux" and not _c_internal_utils.xdisplay_is_valid(),
541564
reason="headless")

0 commit comments

Comments
 (0)