0
# Live Server Testing
1
2
Live server fixtures for integration testing with a running Django server. These fixtures provide a real HTTP server for testing client-server interactions, AJAX requests, and external API integrations.
3
4
## Capabilities
5
6
### Live Server
7
8
Fixture that provides a running Django test server for integration testing.
9
10
```python { .api }
11
def live_server(request: pytest.FixtureRequest):
12
"""
13
Django live server for integration testing.
14
15
Starts a real Django development server in a separate thread,
16
allowing tests to make actual HTTP requests to the application.
17
Useful for testing AJAX, external API interactions, or full
18
integration scenarios.
19
20
The server is automatically started before the test and stopped
21
after test completion. Server address is configurable via
22
--liveserver command line option.
23
24
Args:
25
request: pytest fixture request object
26
27
Returns:
28
LiveServer: Live server instance with URL and utilities
29
"""
30
```
31
32
Usage example:
33
```python
34
import requests
35
36
def test_live_server_integration(live_server):
37
# Make real HTTP request to running Django server
38
response = requests.get(f"{live_server.url}/api/data/")
39
assert response.status_code == 200
40
assert response.json()["status"] == "ok"
41
42
# Test POST request
43
data = {"name": "test", "value": 123}
44
response = requests.post(f"{live_server.url}/api/submit/", json=data)
45
assert response.status_code == 201
46
47
def test_ajax_functionality(live_server, selenium_driver):
48
# Test with Selenium WebDriver
49
selenium_driver.get(f"{live_server.url}/page-with-ajax/")
50
51
# Click button that makes AJAX request
52
button = selenium_driver.find_element("id", "ajax-button")
53
button.click()
54
55
# Wait for AJAX response and verify results
56
result = selenium_driver.find_element("id", "ajax-result")
57
assert result.text == "Success"
58
59
def test_websocket_connection(live_server):
60
import websocket
61
62
# Test WebSocket connection to live server
63
ws_url = live_server.url.replace("http://", "ws://") + "/ws/chat/"
64
ws = websocket.create_connection(ws_url)
65
66
ws.send(json.dumps({"message": "hello"}))
67
response = json.loads(ws.recv())
68
assert response["status"] == "received"
69
70
ws.close()
71
```
72
73
## Live Server Configuration
74
75
The live server can be configured via command line options:
76
77
```bash
78
# Use specific address and port
79
pytest --liveserver=127.0.0.1:8081
80
81
# Use specific port range
82
pytest --liveserver=127.0.0.1:8081-8089
83
84
# Use any available port
85
pytest --liveserver=127.0.0.1:0
86
```
87
88
## Live Server Types
89
90
```python { .api }
91
from typing import Optional, Tuple
92
import threading
93
from django.test.testcases import LiveServerTestCase
94
95
class LiveServer:
96
"""Live Django server for integration testing."""
97
98
def __init__(self, addr: str, *, start: bool = True) -> None:
99
"""
100
Initialize live server instance.
101
102
Args:
103
addr: Server address in format "host" or "host:port"
104
start: Whether to start server immediately (default: True)
105
"""
106
107
def start(self) -> None:
108
"""Start the live server thread."""
109
110
def stop(self) -> None:
111
"""Stop the live server and clean up resources."""
112
113
@property
114
def url(self) -> str:
115
"""Full server URL (e.g., "http://127.0.0.1:8081")"""
116
117
def __str__(self) -> str:
118
"""Return server URL as string."""
119
120
def __add__(self, other) -> str:
121
"""Concatenate server URL with path."""
122
123
def __repr__(self) -> str:
124
"""Return server representation."""
125
126
# Server configuration types
127
ServerAddress = str # Format: "host:port" or "host:port_start-port_end"
128
HostAddress = str # IP address or hostname
129
PortNumber = int # Port number
130
PortRange = Tuple[int, int] # (start_port, end_port)
131
132
# Threading types for server management
133
ServerThread = threading.Thread
134
```