0

Good afternoon! Tell me how not to send a request to a productive server when running pytest. settings.py -

import environ

env = environ.Env(
    DEBUG=(bool, False),
)

PROD_URL = env('PROD_URL')

I use this variable to send a request service.py -

import requests
requests.post(PROD_URL, json=headers)

How can I set this variable to another value when running pytest?

1 Answers1

1

Use mocks in your test cases, when you use mocks in tests the request won't hit the actual server(APIs) for the response, instead it return some data you specified in fixtures, test case it self...

requests-mock provides an external fixture registered with pytest such that it is usable simply by specifying it as a parameter. The following returns text data.

import pytest
import requests
def test_url(requests_mock):
    requests_mock.get('http://test.com', text='data')
    assert 'data' == requests.get('http://test.com').text

In the above example you are testing 'http://test.com' and data returned is 'data'.

mock.get(url, ...)   // the api you testing
adapter.register_uri('GET', 'mock://test.com', text='Success')  //  the response you expect

Following is testing a 'GET' request to the url 'mock://test.com/1' and the data returned is {'a': 'b'}.

adapter.register_uri('GET', 'mock://test.com/1', json={'a': 'b'}, status_code=200)
resp = session.get('mock://test.com/1')
resp.json()
{'a': 'b'}

In your case replace the urls, methods and the data match to yours. Refer: https://requests-mock.readthedocs.io/en/latest/response.html

Jisson
  • 3,566
  • 8
  • 38
  • 71
  • Thanks for the reply! But I still don’t understand how the link to the productive server and the test one will differ in the working and test (pytest) code ... The fact is that I test the api based on signals and, under a certain condition, a request is sent to the productive server. How to replace this link if I send requests from pytest? – Артём Беляков May 17 '23 at 07:39
  • the urls not differ, We use the urls defined in urls.py (eg: /user ) in test case we don't need to use the fully qualified url like ( https://example.com/user). Our aim is not to hit the actual production/staging server while running test case. So when you mock an API, copy the actual production response(some how) and use it in your test case fixture. you can actually use the relevent part of the url from PROD_URL. eg: https://example.com/user , but in your test case user url : user – Jisson May 17 '23 at 11:03
  • In case of signals, please check it helps you https://stackoverflow.com/questions/3817213/proper-way-to-test-django-signals – Jisson May 17 '23 at 11:08
  • It tells how to test your signal, and not how to replace the request link in the logic of the signal when testing! Once again I will explain - in the serializer, when some values are changed, a signal is triggered. In its logic, there is sending a request to the external address of the microservice! That's what I don't want when the tests work. I want to have a fake address when running the tests, and when the application is running, the external address of the microservice – Артём Беляков May 17 '23 at 11:25
  • Perhaps somehow we can override the environment variables when starting the pytest? – Артём Беляков May 17 '23 at 11:26
  • I'm sorry, this solution helped in my problem - – Артём Беляков May 20 '23 at 19:40