Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 6ac7e50

Browse files
committed
Add a mechanism for per-test configs
It's useful to be able to tweak the homeserver config to be used for each test. This PR adds a mechanism to do so.
1 parent f281714 commit 6ac7e50

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

changelog.d/5657.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a mechanism for per-test homeserver configuration in the unit tests.

tests/unittest.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,20 @@ class HomeserverTestCase(TestCase):
157157
"""
158158
A base TestCase that reduces boilerplate for HomeServer-using test cases.
159159
160+
Defines a setUp method which creates a mock reactor, and instantiates a homeserver
161+
running on that reactor.
162+
163+
There are various hooks for modifying the way that the homeserver is instantiated:
164+
165+
* override make_homeserver, for example by making it pass different parameters into
166+
setup_test_homeserver.
167+
168+
* override default_config, to return a modified configuration dictionary for use
169+
by setup_test_homeserver.
170+
171+
* On a per-test basis, you can attach an 'extra_config' attribute, with a dictionary
172+
containing additional configuration settings to be added to the basic config dict.
173+
160174
Attributes:
161175
servlets (list[function]): List of servlet registration function.
162176
user_id (str): The user ID to assume if auth is hijacked.
@@ -168,6 +182,13 @@ class HomeserverTestCase(TestCase):
168182
hijack_auth = True
169183
needs_threadpool = False
170184

185+
def __init__(self, methodName, *args, **kwargs):
186+
super().__init__(methodName, *args, **kwargs)
187+
188+
# see if we have a custom config method for this test
189+
method = getattr(self, methodName)
190+
self._extra_config = getattr(method, "extra_config", None)
191+
171192
def setUp(self):
172193
"""
173194
Set up the TestCase by calling the homeserver constructor, optionally
@@ -276,7 +297,14 @@ def default_config(self, name="test"):
276297
Args:
277298
name (str): The homeserver name/domain.
278299
"""
279-
return default_config(name)
300+
config = default_config(name)
301+
302+
# apply any additional config which was specified as an extra_config attribute
303+
# on the test method..
304+
if self._extra_config is not None:
305+
config.update(self._extra_config)
306+
307+
return config
280308

281309
def prepare(self, reactor, clock, homeserver):
282310
"""

0 commit comments

Comments
 (0)