Пример кода ниже
ниже приведен код модуля из тестируемого файла
Код: Выделить всё
import boto3
import os
from github import Github
from github.Auth import Auth
X_REGION: str=os.environ['X_REGION']
X_ID: str = os.environ['X_ID']
session = boto3.session.Session()
client = session.client(
service_name='test-module',
region_name=X_REGION
)
get_secret_value_response = client.get_secret_value(
SecretId=X_ID
)
git_token = get_secret_value_response['secret']
class CommonGit:
# functions to fetch file from git repo using 'git_token'
def get_file_from_repo(self):
g = Github(git_token)
# Get the authenticated user
user = g.get_user()
# Print all repositories for the authenticated user
for repo in user.get_repos():
print(repo.name)
Код: Выделить всё
import os
from unittest.mock import patch
import pytest
@pytest.fixture()
def setup():
os.environ["SOME_REGION"] = "abc"
os.environ["GIT_CREDS"] = "GIT_CREDS"
os.environ["GIT_ORG"] = "some-org"
os.environ["GIT_REPO"] = 'some-repo'
os.environ['GIT_ENDPOINT'] = 'https://dummy'
@pytest.fixture
def util(setup):
from abc import GitCommons
return GitCommons()
@patch("client.get_secret_value")
def test_read_template_from_git(util, setup):
util.read_template_from_git("test", "test")
Код: Выделить всё
@pytest.fixture(scope="module")
def util(monkeypatch):
os.environ["SOME_REGION"] = "abc"
os.environ["SOME_ID"] = "SOME_ID"
os.environ["GIT_ORG"] = "SOME ORG"
os.environ["GIT_REPO"] = 'SOME REPO'
os.environ['GIT_ENDPOINT'] = 'https://dummy'
# @patch("src.packg.git_commons.get_token", return_value='test123')
session = boto3.session.Session
client = session.client()
get_secret_value_response = client.get_secret_value()
monkeypatch.setattr("src.search_utils.git_commons","get_secret_value_response", get_secret_value_response)
from src.search_utils.git_commons import GitCommons
return GitCommons()
Я получаю сообщение об ошибке "ModuleNotFoundError: нет модуля с именем "клиент""
p>
Во втором варианте я попробовал @pytest.fixture(scope="module") и Monkeypatch вместе, но это не сработало, поскольку при загрузке модуля для динамического кода уровня модуля требуются вышеупомянутые константы, с помощью приспособления в области модуля мы не можем использовать Monkeypatch в области функции. Я пробовал разные области, но мне очень повезло.
Пожалуйста, помогите мне, если у кого-то из вас есть опыт подобного издевательства.
Пожалуйста, дайте мне знать, если вам нужна дополнительная информация. Постарался уточнить более подробно.
Подробнее здесь: https://stackoverflow.com/questions/790 ... e-variable