Pytest Integration

To help with testing and reporting, Genalyzer provides a pytest plugin that allows you to create and attach plots directly to your test reports. This is particularly useful for visualizing signal analysis results during automated testing.

Installation

To use the pytest plugin, install the optional pytest dependency:

pip install genalyzer[pytest]

Usage

To use the pytest plugin, you need to include the gn_plot_manager in your test functions. This fixture provides methods to create and attach plots to your test reports.

Here is an example of how to use the gn_plot_manager in a test function:

import numpy as np

def test_signal_analysis(gn_plot_manager):
    # Generate a sample signal
    t = np.linspace(0, 1, 500)
    signal = np.sin(2 * np.pi * 50 * t) + 0.5 * np.random.randn(500)

    # Create a plot of the signal
    fig, ax = gn_plot_manager.create_figure()
    ax.plot(t, signal)
    ax.set_title("Sample Signal")
    ax.set_xlabel("Time [s]")
    ax.set_ylabel("Amplitude")

    # Attach the plot to the test report
    gn_plot_manager.attach_figure(fig, "Sample Signal Plot")

Addition methods

The gn_plot_manager fixture provides the following methods:

Special plot implementations for pytest-genalyzer.

genalyzer.pytest.plots.add_plot(self, fig, name=None)

Add a plotly figure to the current test report.

Args:

fig (plotly.graph_objects.Figure): The plotly figure to add.

name (str, optional): Name of the plot. If None, a default name will be assigned.

genalyzer.pytest.plots.add_plot_html(self, html_str, name=None)

Add raw HTML content as a plot to the current test report.

Args:

html_str (str): HTML content as a string.

name (str, optional): Name of the plot. If None, a default name will be assigned.

genalyzer.pytest.plots.add_plot_img_file(self, img_path, name=None)

Add an image file as a plot to the current test report.

Args:

img_path (str): Path to the image file.

name (str, optional): Name of the plot. If None, a default name will be assigned.

genalyzer.pytest.plots.gn_plot_fft_single_tone(self, data, full_scale, sample_rate, tone_frequency, ssb_rest=3, ssb_fund=4, nfft=None, navg=1, plot_height_px=1000, title='SFDR Plot')

Plot SFDR (Spurious-Free Dynamic Range) using plotly.

Args:

data (dict): Dictionary containing frequency and magnitude data.

full_scale (float): Full scale value for normalization.

sample_rate (float): Sample rate of the signal.

tone_frequency (float): Frequency of the tone to analyze.

ssb_rest (int, optional): Sideband setting for rest of the spectrum. Defaults to 3.

ssb_fund (int, optional): Sideband setting for fundamental tone. Defaults to 4.

nfft (int, optional): Number of FFT points. If None, it will be set to len(data)//2. Defaults to None.

navg (int, optional): Number of averages. Defaults to 1.

plot_height_px (int, optional): Height of the plot in pixels. Defaults to 1000.

title (str): Title of the plot.