title: How to mock pyplot.show in python (to prevent showing plots)
body: <p>I want to assert that matplotlib.pyplot.show was called in one of my class's methods, but I don't want the method to actually call show() and plot on an external window during the unit test. No matter how I try to mock matplotlib, it doesn't seem to work. I'll list the important parts of my code below:</p>
  
  <pre><code>#In Model Test Class:
  import Model
  import matplotlib
  import unittest
  from unittest.mock import patch
  
  class Model_Test(unittest.TestCase):
  
      @patch('matplotlib.pyplot')
      def test_plot_data(self, mock_pyplot):
          model = Model()
          model.plot_data()
          mock_pyplot.show.assert_called_once()
  </code></pre>
  
  <hr>
  
  <pre><code>#In Model Class:
  import matplotlib.pyplot as plt
  import pandas as pd
  
  class Model(object):
      ... # (__init__ and other methods)
      def plot_data(self):
          self.dataframe.plot(y=self.dataframe.columns, subplots=False, figsize=(15, 8), fontsize=12)
          plt.title('Data for Model Version: ' + self.version, fontsize=20)
          plt.xlabel('timestamp', fontsize=12)
          plt.ylabel('value', fontsize=12)
          plt.show()
  </code></pre>
  
  <p>I also tried setting a return value for mock_plot.show. When I use the debugger, matplotlib within the Model class is not propagated with a Mock object, so I am unsure how to mock the matplotlib module or its member functions. Is it possible to mock matplotlib?</p>
  
  <p>Any ideas for how to properly mock matplotlib in unit tests (mainly to prevent displaying plots)?</p>
  
  <p>EDIT: I was able to get it to work by changing the patch decoration to 
  <code>@patch('matplotlib.pyplot.show')</code>, 
  but I'm still unsure why this worked and my previous attempt didn't.</p>