Testing module git_commons.py is the objective, below is code from the file. I am not able to pass pytest fixture into test function. More details provided below.
import json
from github import Github
import logging
import boto3
import os
# ENV VARIABLES
ES_REGION: str = os.environ['ES_REGION']
GIT_CREDS_ARN: str = os.environ['GIT_CREDS_ARN']
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# Constants
GIT_ORG: str = ‘some-org’
GIT_REPO = ‘some-repo’
GIT_ENDPOINT = ‘some-end-point’
def get_token():
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager',
region_name=ES_REGION
)
get_secret_value_response = client.get_secret_value(
SecretId=GIT_CREDS_ARN
)
return get_secret_value_response['SecretString']
git_token = get_token()
class GitCommons:
def read_template_from_git(self, template_path, git_branch):
git = Github(base_url=GIT_ENDPOINT, login_or_token=git_token)
org = git.get_organization(GIT_ORG)
repo = org.get_repo(GIT_REPO)
contents = repo.get_contents(template_path, ref=git_branch)
return json.loads(contents.decoded_content.decode())
I have tried below unit test implementations with an expectation to pass
Below test fixtures and functions are from git_commons_2_test.py
@pytest.fixture(scope="session")
def env_var():
os.environ["ES_REGION"] = "us-east-1"
os.environ["GIT_CREDS_ARN"] = "12121"
os.environ["GIT_ORG"] = “some-org”
os.environ["GIT_REPO"] = ‘some-repo’
os.environ['GIT_ENDPOINT'] = 'https://dummy'
@pytest.fixture(scope="session")
@mock.patch("boto3.session.Session")
def secrt(mock_session_class, env_var):
mock_session_object = mock.Mock()
mock_client = mock.Mock()
mock_client.get_secret_value.return_value = {'SecretString': 12121212}
mock_session_object.client.return_value = mock_client
mock_session_class.return_value = mock_session_object
from src.search_utils import git_commons
return git_commons.get_token()
@pytest.fixture()
@mock.patch("github.MainClass.Github")
def github(mock_github_class, secrt, env_var):
mock_github = mock.Mock()
mock_org = mock.Mock()
mock_repo = mock.Mock()
mock_repo.get_contents.return_value = {"something":"something"}
mock_org.get_repo.return_value = mock_repo
mock_github.get_organization.return_value = mock_org
mock_github_class.return_value = mock_github
yield mock_github_class
def test_read_template_from_git(github, secrt,env_var ):
from src.search_utils.git_commons import GitCommons
template = GitCommons.read_template_from_git(None, "temp", "test")
print(template)
I am getting below error while executing above test case
platform darwin — Python 3.12.6, pytest-8.3.3, pluggy-1.5.0 — /Library/Frameworks/Python.framework/Versions/3.12/bin/python3.12
cachedir: .pytest_cache
__________________________________________________________________________________________________________________________________________________________________________________________________ test_read_template_from_git __________________________________________________________________________________________________________________________________________________________________________________________________
github = <generator object github at 0x106691540>, secrt = 12121212, env_var = None
def test_read_template_from_git(github, secrt,env_var )
from src.search_utils.git_commons import GitCommons
template = GitCommons.read_template_from_git(None, "temp", "test")
tests/search_utils/git_commons_2_test.py:51:
src/search_utils/git_commons.py:36: in read_template_from_git
git = Github(base_url=GIT_ENDPOINT, login_or_token=git_token)
self = <github.MainClass.Github object at 0x1071bb560>, login_or_token = 12121212, password = None, jwt = None, app_auth = None, base_url = ‘https://github.com/api/v3’ (Since mock object is not effective, new object is created with actual values), timeout = 15, user_agent = ‘PyGithub/Python’, per_page = 30, verify = True, retry = GithubRetry(total=10, connect=None, read=None, redirect=None, status=None), pool_size = None, seconds_between_requests = 0.25, seconds_between_writes = 1.0
auth = None
assert login_or_token is None or isinstance(login_or_token, str), login_or_token
E AssertionError: 12121212
/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/github/MainClass.py:204: AssertionError
——————————————————————————————————————————————————————————————————- Captured log call ——————————————————————————————————————————————————————————————————-
INFO root:git_commons.py:35 read the template from git
==================================================================================================================================================================================================== short test summary info ====================================================================================================================================================================================================
FAILED tests/search_utils/git_commons_2_test.py::test_read_template_from_git – AssertionError: 12121212
Srini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1