Python3 Fixtures

Posted: 2018-08-10 18:33:31

Just a reminder using pickle I can easily make fixtures for testing.

When I get results from AWS SDK for example I can just

import pickle

    with open("fixtures/describe_task_defination.pickle", 'wb') as fixture:
      pickle.dump(self.stored_state_to_save, fixture)

Then when I want to feed it back in my test

import pickle
from unittest import TestCase
from unittest.mock import patch
from unittest import mock
import pickle
from task_image import TaskImage

def getFixture(name):
  with open("fixtures/" + name, 'rb') as fixture:
    return pickle.load(fixture)

class TestTaskImage(TestCase)
    def setUp(self):
        self.task_image = TaskImage()

@patch('task_image.TaskImage._getTaskDefinition', return_value=getFixture('describe_task_defination.pickle'))
    @patch('task_image.TaskImage._getService', return_value=getFixture('describe_services.pickle'))
    def test_gets_service(self, _getService, _getTaskDefinition):
        self.results = self.task_image.handle('default', 'cat-teamdocs-production-122')
        self.assertEqual('cat-teamdocs:latest', self.results)

Pretty easy and then I am not hitting the service