I would like to get the default domain name of Client()
in Django unittests. I saw a way to change the default one. But didn't find a way to get the default domain name.
Asked
Active
Viewed 2,054 times
2

Savad KP
- 1,625
- 3
- 28
- 40

Vipul Vishnu av
- 486
- 1
- 5
- 15
-
Assuming you have an instance of `Client()` called `client`, does `client.environ['SERVER_NAME']` contain what you want? – ChrisGPT was on strike Jan 04 '16 at 12:37
-
I think **Client()** doesn't have `environ` attribute – Vipul Vishnu av Jan 04 '16 at 12:42
-
Do you _think_ so, or did you test? – ChrisGPT was on strike Jan 04 '16 at 12:43
-
Yes. I just tired. I tried running my test now, – Vipul Vishnu av Jan 04 '16 at 12:45
3 Answers
8
The default domain name for Django's test client is testserver
. It's hardcoded in the RequestFactory
base class.
If you want to change the domain for the specific request, you can just pass it as the kwarg:
self.client.get('/some-path', SERVER_NAME="anotherdomain.com")

Alex Morozov
- 5,823
- 24
- 28
-
Ah, I get it. Anyway, please consider marking this answer as accepted, as it answers the exact question you've asked: the default domain of Django's test `Client()`. Or rephrase your initial question so that your own answer gets in line with it. – Alex Morozov Jan 10 '16 at 19:23
0
I just tried https://docs.djangoproject.com/en/1.9/topics/testing/tools/#liveservertestcase
The default test URL will be http://localhost:8082 and we can request to the same while running tests. And its working for me.

Vipul Vishnu av
- 486
- 1
- 5
- 15
-
2Sure, but your question was about the domain name in the `Client` class, not about the bind port of the `LiveServerTestCase`. That's a completely different thing. – Alex Morozov Jan 05 '16 at 13:43
-
Yes you are right . Actually I needed to run the test locally and send requests manually to the urls. LiveServerTestCase returns me the uri by self.live_server_url – Vipul Vishnu av Jan 06 '16 at 05:28
0
is this the (pytest) answer you are looking for?
@pytest.fixture()
def client() -> "django.test.client.Client":
"""like the default pytest client with a new domain name."""
skip_if_no_django()
return Client(SERVER_NAME = 'my-server.com')

George
- 1
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – HPringles Feb 16 '22 at 23:02