Skip to content

Commit 8d9b34f

Browse files
committed
Added basic unit tests
1 parent 8f00569 commit 8d9b34f

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def hello_world():
2727

2828

2929
if __name__ == "__main__":
30-
fastwsgi.run(wsgi_app=app, host="0.0.0.0", port=5000)
30+
fastwsgi.run(wsgi_app=app, host="127.0.0.1", port=5000)
3131
```
3232

3333

@@ -39,9 +39,16 @@ def application(environ, start_response):
3939
return [b"Hello, World!"]
4040

4141
if __name__ == "__main__":
42-
fastwsgi.run(wsgi_app=application, host="0.0.0.0", port=5000)
42+
fastwsgi.run(wsgi_app=application, host="127.0.0.1", port=5000)
4343
```
4444

45+
## Testing
46+
47+
To run the test suite using [pytest](https://docs.pytest.org/en/latest/getting-started.html), run the following command:
48+
49+
```bash
50+
python3 -m pytest
51+
```
4552

4653
## TODO
4754

example.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,5 @@ def hello_world():
99
return "Hello, World!", 200
1010

1111

12-
def application(environ, start_response):
13-
start_response('200 OK', [('Content-Type', 'text/html')])
14-
return [b"Hello, World!"]
15-
16-
1712
if __name__ == "__main__":
18-
fastwsgi.run(wsgi_app=application, host="0.0.0.0", port=5000)
13+
fastwsgi.run(wsgi_app=app, host="0.0.0.0", port=5000)

tests/conftest.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pytest
2+
import fastwsgi
3+
from multiprocessing import Process
4+
5+
6+
HOST = "127.0.0.1"
7+
PORT = 8080
8+
9+
10+
class ServerProcess:
11+
def __init__(self, application, host=HOST, port=PORT) -> None:
12+
self.process = Process(target=fastwsgi.run, args=(application, host, port))
13+
14+
def __enter__(self):
15+
self.process.start()
16+
return self.process
17+
18+
def __exit__(self, exc_type, exc_value, exc_tb):
19+
if self.process.is_alive():
20+
self.process.terminate()
21+
22+
23+
@pytest.fixture(autouse=True, scope="session")
24+
def server_process():
25+
return ServerProcess

tests/test_flask.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import requests
2+
from flask import Flask
3+
4+
app = Flask(__name__)
5+
6+
7+
@app.get("/")
8+
def hello_world():
9+
return "Hello, World!", 200
10+
11+
12+
def test_uwsgi_hello_world(server_process):
13+
with server_process(app):
14+
result = requests.get("http://127.0.0.1:8080/")
15+
assert result.status_code == 200
16+
assert result.text == "Hello, World!"

tests/test_uwsgi.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import requests
2+
3+
4+
def wsgi_app(environ, start_response):
5+
start_response('200 OK', [('Content-Type', 'text/html')])
6+
return [b"Hello, World!"]
7+
8+
9+
def test_uwsgi_hello_world(server_process):
10+
with server_process(wsgi_app):
11+
result = requests.get("http://127.0.0.1:8080/")
12+
assert result.status_code == 200
13+
assert result.text == "Hello, World!"

0 commit comments

Comments
 (0)