Matplotlib tests + helpers methods

* Maplotlib is an important package. It has now its own test checking that a plot can be generated and exported has an image.
* Some helpers methods added to the `Makefile` mainly permitting to clean containers and images
* Improved assertion messages of some tests
This commit is contained in:
romainx
2020-04-28 15:17:56 +02:00
parent bca04790b4
commit 4e1bcc8236
7 changed files with 101 additions and 11 deletions

View File

@@ -0,0 +1,24 @@
# Matplotlit: Create a simple plot example.
# Refs: https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/simple_plot.html
# Optional test with [Matplotlib Jupyter Integration](https://github.com/matplotlib/ipympl)
# %matplotlib widget
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import os
# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
ax.grid()
# Note that the test can be run headless by checking if an image is produced
file_path = os.path.join("/tmp", "test.png")
fig.savefig(file_path)
print(f"File {file_path} saved")

View File

@@ -0,0 +1,35 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import logging
import pytest
import os
LOGGER = logging.getLogger(__name__)
def test_matplotlib(container):
"""Test that matplotlib is able to plot a graph and write it as an image"""
host_data_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data")
cont_data_dir = "/home/jovyan/data"
test_file = "matplotlib_1.py"
output_dir = "/tmp"
LOGGER.info(f"Test that matplotlib is able to plot a graph and write it as an image ...")
command = "sleep infinity"
running_container = container.run(
volumes={host_data_dir: {"bind": cont_data_dir, "mode": "ro"}},
tty=True,
command=["start.sh", "bash", "-c", command],
)
command = f"python {cont_data_dir}/{test_file}"
cmd = running_container.exec_run(command)
assert cmd.exit_code == 0, f"Command {command} failed"
LOGGER.debug(cmd.output.decode("utf-8"))
# Checking if the file is generated
# https://stackoverflow.com/a/15895594/4413446
expected_file = f"{output_dir}/test.png"
command = f"test -s {expected_file}"
cmd = running_container.exec_run(command)
assert cmd.exit_code == 0, f"Command {command} failed"
LOGGER.debug(cmd.output.decode("utf-8"))

View File

@@ -21,6 +21,6 @@ def test_pandas(container, name, command):
LOGGER.info(f"Testing pandas: {name} ...")
c = container.run(tty=True, command=["start.sh", "python", "-c", command])
rv = c.wait(timeout=30)
assert rv == 0 or rv["StatusCode"] == 0
assert rv == 0 or rv["StatusCode"] == 0, f"Command {command} failed"
logs = c.logs(stdout=True).decode("utf-8")
LOGGER.debug(logs)