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

Commit

Permalink
Add a mechanism for per-test configs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
richvdh committed Jul 10, 2019
1 parent f281714 commit 6ac7e50
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/5657.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a mechanism for per-test homeserver configuration in the unit tests.
30 changes: 29 additions & 1 deletion tests/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,20 @@ class HomeserverTestCase(TestCase):
"""
A base TestCase that reduces boilerplate for HomeServer-using test cases.
Defines a setUp method which creates a mock reactor, and instantiates a homeserver
running on that reactor.
There are various hooks for modifying the way that the homeserver is instantiated:
* override make_homeserver, for example by making it pass different parameters into
setup_test_homeserver.
* override default_config, to return a modified configuration dictionary for use
by setup_test_homeserver.
* On a per-test basis, you can attach an 'extra_config' attribute, with a dictionary
containing additional configuration settings to be added to the basic config dict.
Attributes:
servlets (list[function]): List of servlet registration function.
user_id (str): The user ID to assume if auth is hijacked.
Expand All @@ -168,6 +182,13 @@ class HomeserverTestCase(TestCase):
hijack_auth = True
needs_threadpool = False

def __init__(self, methodName, *args, **kwargs):
super().__init__(methodName, *args, **kwargs)

# see if we have a custom config method for this test
method = getattr(self, methodName)
self._extra_config = getattr(method, "extra_config", None)

def setUp(self):
"""
Set up the TestCase by calling the homeserver constructor, optionally
Expand Down Expand Up @@ -276,7 +297,14 @@ def default_config(self, name="test"):
Args:
name (str): The homeserver name/domain.
"""
return default_config(name)
config = default_config(name)

# apply any additional config which was specified as an extra_config attribute
# on the test method..
if self._extra_config is not None:
config.update(self._extra_config)

return config

def prepare(self, reactor, clock, homeserver):
"""
Expand Down

0 comments on commit 6ac7e50

Please sign in to comment.