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

Commit f51c674

Browse files
committed
Add a mechanism for per-test configs (#5657)
2 parents 333ae76 + 6bb0357 commit f51c674

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

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: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ 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 use the @override_config decorator to give a
172+
dictionary containing additional configuration settings to be added to the basic
173+
config dict.
174+
160175
Attributes:
161176
servlets (list[function]): List of servlet registration function.
162177
user_id (str): The user ID to assume if auth is hijacked.
@@ -168,6 +183,13 @@ class HomeserverTestCase(TestCase):
168183
hijack_auth = True
169184
needs_threadpool = False
170185

186+
def __init__(self, methodName, *args, **kwargs):
187+
super().__init__(methodName, *args, **kwargs)
188+
189+
# see if we have any additional config for this test
190+
method = getattr(self, methodName)
191+
self._extra_config = getattr(method, "_extra_config", None)
192+
171193
def setUp(self):
172194
"""
173195
Set up the TestCase by calling the homeserver constructor, optionally
@@ -276,7 +298,14 @@ def default_config(self, name="test"):
276298
Args:
277299
name (str): The homeserver name/domain.
278300
"""
279-
return default_config(name)
301+
config = default_config(name)
302+
303+
# apply any additional config which was specified via the override_config
304+
# decorator.
305+
if self._extra_config is not None:
306+
config.update(self._extra_config)
307+
308+
return config
280309

281310
def prepare(self, reactor, clock, homeserver):
282311
"""
@@ -534,3 +563,27 @@ def attempt_wrong_password_login(self, username, password):
534563
)
535564
self.render(request)
536565
self.assertEqual(channel.code, 403, channel.result)
566+
567+
568+
def override_config(extra_config):
569+
"""A decorator which can be applied to test functions to give additional HS config
570+
571+
For use
572+
573+
For example:
574+
575+
class MyTestCase(HomeserverTestCase):
576+
@override_config({"enable_registration": False, ...})
577+
def test_foo(self):
578+
...
579+
580+
Args:
581+
extra_config(dict): Additional config settings to be merged into the default
582+
config dict before instantiating the test homeserver.
583+
"""
584+
585+
def decorator(func):
586+
func._extra_config = extra_config
587+
return func
588+
589+
return decorator

0 commit comments

Comments
 (0)