mardi 26 juillet 2016

Patching module level function(generator) in Python unit test.

I have a module level method (which is a generator). I want to supply custom implementation of that method (not just return value while testing another class. I also need to change some parameters(instance variables) of the class at test time. module x:

def zone_file_iterator():
    pass 

module y:

from x import zone_file_iterator()

class PleaseTestMe(CachedSingleTon): def init(): self.a ----- -----

  @classmethod
  def load_data(cls):
    """
    do some thing with large data file.
    """
    ------zone_file_iterator()

To test the above class , I believe using the Mock Framework in Python Unittest, can we do some thing like this ? Please correct me if this is wrong.

   @patch('y.PleaseTestMe.a', customvalue)
   @patch('x.zone_file_iterator()', x.custome_small_data_iterator())
   def test_skip_grams():
      """
      Instantiate the class and write Assert Logic here.
      """
      pass    

Aucun commentaire:

Enregistrer un commentaire