Answer the question
In order to leave comments, you need to log in
Pytest: how to lock a method variable?
There is a dzhangovsky manager command in which the csv file is processed.
app/my_app/my_command.py
class Command(BaseCommand):
def handle(self, *args, **options):
path = (os.path.join(os.path.abspath(os.path.dirname(__name__)), 'data.csv'))
# остальная логика
@pytest.fixture
def create_test_csv_file(tmpdir_factory):
# логка по созданию и заполнению тестового файла
# фикстура возвращает путь к тестовому файлу
return str(path)
@pytest.mark.django_db
def test_function(mocker, create_test_csv_file):
# здесь как то надо замокать path
что то типа mock_path = create_test_csv_file
call_command('my_command')
Answer the question
In order to leave comments, you need to log in
Michael thanks for the help. It looks like you still have to change the manager command. Now I settled on such a solution through an additional variable with a default value, maybe it will come in handy for someone
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
"--path",
dest="path",
default=os.path.join(os.path.abspath(os.path.dirname(__name__)), 'data.csv')
)
def handle(self, *args, **options):
path = options.get("path")
...
call_command('my_command', path=create_test_csv_file)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question