disable matplotlib plotting in test code try import matplotlib pyplot as plt mocker patch objectplt gca mocker patch objectplt show except importerror pass pytest fixturescopemodule autousetrue def teardownloadedcorpora yield first wait for the test to end import nltk corpus for name in dirnltk corpus obj getattrnltk corpus name none if isinstanceobj corpusreader and hasattrobj unload obj unload disable matplotlib plotting in test code after each test session ends either doctest or unit test unload any loaded corpora first wait for the test to end
import pytest

from nltk.corpus.reader import CorpusReader


@pytest.fixture(autouse=True)
def mock_plot(mocker):
    

    try:
        import matplotlib.pyplot as plt

        mocker.patch.object(plt, "gca")
        mocker.patch.object(plt, "show")
    except ImportError:
        pass


@pytest.fixture(scope="module", autouse=True)
def teardown_loaded_corpora():
    

    yield  

    import nltk.corpus

    for name in dir(nltk.corpus):
        obj = getattr(nltk.corpus, name, None)
        if isinstance(obj, CorpusReader) and hasattr(obj, "_unload"):
            obj._unload()