Mocking, Python, Pytest
Ok this is going to be a collection of notes to self. This post does a good job of summing up some mock patterns with PyTest. I then had to mock the Snyk client which depended on the Requests library. I chose not to mock the requests library but the methods in the snyk library. The thing was the method I mocked returned the “Response” object and I wanted to use the json() feature of that object/class to return the results.
Mocking in Python
Serious note to self. I wanted to mock the Github module for python. My simple class will get the ID of the team. from github import Github, Team from dotenv import load_dotenv import os import sys import json class GetTeamId: def __init__(self): load_dotenv() self.team_id = 5555 self.token = os.environ.get("GITHUB_TOKEN") self.client = Github(self.token) self.org = self.client.get_organization('FooBar') def handle(self, team_name): teams = self.org.get_teams() for team in teams: if team_name == team.name: print("Team id", team.