Created
December 22, 2011 19:30
-
-
Save hgdeoro/1511536 to your computer and use it in GitHub Desktop.
Context manager for Django's TestServerThread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# To be used with 'http://djangosnippets.org/snippets/1570/' | |
# | |
@contextmanager | |
def server_thread(address, port): | |
server = TestServerThread(address, port) | |
try: | |
server.start() | |
yield server | |
finally: | |
server.stop() | |
class TestOfTestServerThread(TestCase): | |
def test_ctx_manager(self): | |
with server_thread("127.0.0.1", 18913): | |
urllib.urlopen("http://127.0.0.1:18913/some/url/hope/dont/exists/") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment