forked from morepath/morepath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
43 lines (29 loc) · 1.1 KB
/
conftest.py
File metadata and controls
43 lines (29 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import pytest
@pytest.fixture
def mockserver(monkeypatch):
"""Make the server in wsgiref receive Ctrl-C as soon as it starts serving.
The fixture object provides these methods:
* set_argv(list): set the command-line arguments seen by morepath.run
* run(*args, **kw): run morepath.run and return the exit code
"""
import sys
from wsgiref.simple_server import WSGIServer
def mock_serve_forever(self):
raise KeyboardInterrupt
monkeypatch.setattr(WSGIServer, 'serve_forever', mock_serve_forever)
argv = ['script-name']
monkeypatch.setattr(sys, 'argv', argv)
return MockServer(argv)
class MockServer(object):
def __init__(self, argv):
self.argv = argv
def set_argv(self, argv):
"""Set the command-line arguments seen by morepath.run."""
self.argv.extend(argv)
def run(self, *args, **kw):
"""Wrapper around morepath.run to catch SystemExit.
:return: the exit code."""
import morepath
with pytest.raises(SystemExit) as ex:
morepath.run(*args, **kw)
return ex.value.code