From 635c7bdfa72b58606be49870dbf7bc7447561399 Mon Sep 17 00:00:00 2001 From: Eric Hunsberger Date: Thu, 7 Dec 2023 13:26:40 -0500 Subject: [PATCH] Mock plt.subplots which has multiple outputs --- .github/workflows/ci.yml | 4 ++-- .nengobones.yml | 2 ++ LICENSE.rst | 1 + docs/conf.py | 2 +- pyproject.toml | 2 +- pytest_plt/plugin.py | 15 ++++++++++++--- pytest_plt/tests/test_plt.py | 7 +++++++ setup.py | 8 ++++---- 8 files changed, 30 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4ded5b3..325aea0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: - script: static - script: test coverage-name: oldest - python-version: "3.7" + python-version: "3.8" - script: test coverage-name: newest - script: test @@ -40,7 +40,7 @@ jobs: steps: - uses: nengo/nengo-bones/actions/setup@main with: - python-version: ${{ matrix.python-version || '3.9' }} + python-version: ${{ matrix.python-version || '3.10' }} - uses: nengo/nengo-bones/actions/generate-and-check@main - uses: nengo/nengo-bones/actions/run-script@main with: diff --git a/.nengobones.yml b/.nengobones.yml index 68b672b..717d632 100644 --- a/.nengobones.yml +++ b/.nengobones.yml @@ -24,6 +24,8 @@ setup_py: - nengo_sphinx_theme>1.2.2 - numpydoc>=0.9.2 - sphinx + tests_req: + - pytest-cov # required since `addopts = --cov` in setup.cfg entry_points: pytest11: - "plt = pytest_plt.plugin" diff --git a/LICENSE.rst b/LICENSE.rst index 2613d63..6ac6eac 100644 --- a/LICENSE.rst +++ b/LICENSE.rst @@ -27,3 +27,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/docs/conf.py b/docs/conf.py index 678776c..05842cb 100755 --- a/docs/conf.py +++ b/docs/conf.py @@ -105,7 +105,7 @@ var _paq = window._paq = window._paq || []; _paq.push(["setDocumentTitle", document.domain + "/" + document.title]); _paq.push(["setCookieDomain", "*.appliedbrainresearch.com"]); - _paq.push(["setDomains", ["*.appliedbrainresearch.com","*.edge.nengo.ai","*.forum.nengo.ai","*.labs.nengo.ai","*.nengo.ai"]]); + _paq.push(["setDomains", ["*.appliedbrainresearch.com","*.edge.nengo.ai","*.forum.nengo.ai","*.nengo.ai"]]); _paq.push(["enableCrossDomainLinking"]); _paq.push(["setDoNotTrack", true]); _paq.push(['trackPageView']); diff --git a/pyproject.toml b/pyproject.toml index ffa983f..f311a8e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ requires = ["setuptools<64", "wheel"] [tool.black] -target-version = ['py36'] +target-version = ['py38'] [tool.isort] profile = "black" diff --git a/pytest_plt/plugin.py b/pytest_plt/plugin.py index 2715d97..3198b15 100644 --- a/pytest_plt/plugin.py +++ b/pytest_plt/plugin.py @@ -58,6 +58,8 @@ def pytest_report_teststatus(report): class Mock: + multi_functions = {} + def __init__(self, *args, **kwargs): pass @@ -81,10 +83,18 @@ def __getattr__(cls, name): mockType = type(name, (), {}) mockType.__module__ = __name__ return mockType + elif name in cls.multi_functions: + return lambda *args, **kwargs: tuple( + Mock() for _ in range(cls.multi_functions[name]) + ) else: return Mock() +class PltMock(Mock): + multi_functions = {"subplots": 2} + + class Recorder: def __init__(self, dirname, nodeid, filename_drop=None): self.dirname = dirname @@ -139,7 +149,7 @@ def __enter__(self): if self.record: self.plt = mpl_plt else: - self.plt = Mock() + self.plt = PltMock() self.plt.saveas = self.get_filename(ext="pdf") return self.plt @@ -174,8 +184,7 @@ def save(self, path): @pytest.fixture def plt(request): - """ - A pyplot-compatible plotting interface. + """A pyplot-compatible plotting interface. Use this to create plots in your tests using the ``matplotlib.pyplot`` interface. diff --git a/pytest_plt/tests/test_plt.py b/pytest_plt/tests/test_plt.py index a737097..3ff6923 100644 --- a/pytest_plt/tests/test_plt.py +++ b/pytest_plt/tests/test_plt.py @@ -31,6 +31,13 @@ def test_mock_iter(plt): plt.saveas = None +def test_mock_subplots(plt): + fig, axes = plt.subplots(2, 1) + axes[0].plot(np.arange(10)) + axes[1].plot(-np.arange(10)) + fig.tight_layout() + + def test_simple_plot(plt): plt.plot(np.linspace(0, 1, 20), np.linspace(0, 2, 20)) diff --git a/setup.py b/setup.py index 474bc07..0cceedf 100755 --- a/setup.py +++ b/setup.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - # Automatically generated by nengo-bones, do not edit this file directly import io @@ -40,7 +38,9 @@ def read(*filenames, **kwargs): "sphinx", ] optional_req = [] -tests_req = [] +tests_req = [ + "pytest-cov", +] setup( name="pytest-plt", @@ -61,7 +61,7 @@ def read(*filenames, **kwargs): "optional": optional_req, "tests": tests_req, }, - python_requires=">=3.6", + python_requires=">=3.8", entry_points={ "pytest11": [ "plt = pytest_plt.plugin",