Skip to content

Commit

Permalink
Mock plt.subplots which has multiple outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
hunse committed Dec 7, 2023
1 parent ff174b2 commit 635c7bd
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions .nengobones.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions LICENSE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
requires = ["setuptools<64", "wheel"]

[tool.black]
target-version = ['py36']
target-version = ['py38']

[tool.isort]
profile = "black"
Expand Down
15 changes: 12 additions & 3 deletions pytest_plt/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def pytest_report_teststatus(report):


class Mock:
multi_functions = {}

def __init__(self, *args, **kwargs):
pass

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions pytest_plt/tests/test_plt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python

# Automatically generated by nengo-bones, do not edit this file directly

import io
Expand Down Expand Up @@ -40,7 +38,9 @@ def read(*filenames, **kwargs):
"sphinx",
]
optional_req = []
tests_req = []
tests_req = [
"pytest-cov",
]

setup(
name="pytest-plt",
Expand All @@ -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",
Expand Down

0 comments on commit 635c7bd

Please sign in to comment.