python unittest, pytest fixture – mocked object not passing into function under test and creating new object instead

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

New contributor

Srini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật