Skip to content

Commit a4ea66d

Browse files
samamorganbluetech
authored andcommitted
Allow to start LiveServerThread separately from init
1 parent bf9c965 commit a4ea66d

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

pytest_django/live_server_helper.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class LiveServer:
88
The ``live_server`` fixture handles creation and stopping.
99
"""
1010

11-
def __init__(self, addr: str) -> None:
11+
def __init__(self, addr: str, *, start: bool = True) -> None:
1212
from django.db import connections
1313
from django.test.testcases import LiveServerThread
1414
from django.test.utils import modify_settings
@@ -20,8 +20,6 @@ def __init__(self, addr: str) -> None:
2020
# If using in-memory sqlite databases, pass the connections to
2121
# the server thread.
2222
if conn.vendor == "sqlite" and conn.is_in_memory_db():
23-
# Explicitly enable thread-shareability for this connection.
24-
conn.inc_thread_sharing()
2523
connections_override[conn.alias] = conn
2624

2725
liveserver_kwargs["connections_override"] = connections_override
@@ -51,6 +49,16 @@ def __init__(self, addr: str) -> None:
5149
# `_live_server_helper`.
5250

5351
self.thread.daemon = True
52+
53+
if start:
54+
self.start()
55+
56+
def start(self) -> None:
57+
"""Start the server"""
58+
for conn in self.thread.connections_override.values():
59+
# Explicitly enable thread-shareability for this connection.
60+
conn.inc_thread_sharing()
61+
5462
self.thread.start()
5563
self.thread.is_ready.wait()
5664

0 commit comments

Comments
 (0)