Skip to content

Commit c65aa71

Browse files
author
Joel Collins
committed
Moved common fixtures into conftest.py
1 parent dc58e20 commit c65aa71

File tree

3 files changed

+62
-68
lines changed

3 files changed

+62
-68
lines changed

tests/conftest.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import pytest
2+
from flask import Flask
3+
4+
5+
@pytest.fixture()
6+
def app(request):
7+
8+
app = Flask(__name__)
9+
app.config["TESTING"] = True
10+
11+
# pushes an application context manually
12+
ctx = app.app_context()
13+
ctx.push()
14+
15+
# bind the test life with the context through the
16+
request.addfinalizer(ctx.pop)
17+
return app
18+
19+
20+
@pytest.fixture()
21+
def debug_app(request):
22+
23+
app = Flask(__name__)
24+
app.config["TESTING"] = True
25+
app.debug = True
26+
27+
# pushes an application context manually
28+
ctx = app.app_context()
29+
ctx.push()
30+
31+
# bind the test life with the context through the
32+
request.addfinalizer(ctx.pop)
33+
return app
34+
35+
36+
@pytest.fixture()
37+
def app_ctx(app):
38+
with app.app_context():
39+
yield app
40+
41+
42+
@pytest.fixture()
43+
def app_ctx_debug(debug_app):
44+
with debug_app.app_context():
45+
yield debug_app
46+
47+
48+
@pytest.fixture
49+
def req_ctx(app):
50+
with app.test_request_context() as ctx:
51+
yield ctx
52+
53+
54+
@pytest.fixture
55+
def client(app):
56+
return app.test_client()

tests/test_core_tasks_pool.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,9 @@
11
from labthings.core import tasks
2-
from flask import Flask, Response
32
import pytest
43

54
import gevent
65

76

8-
@pytest.fixture()
9-
def app(request):
10-
11-
app = Flask(__name__)
12-
13-
# pushes an application context manually
14-
ctx = app.app_context()
15-
ctx.push()
16-
17-
# bind the test life with the context through the
18-
request.addfinalizer(ctx.pop)
19-
return app
20-
21-
22-
@pytest.fixture()
23-
def app_context(app):
24-
with app.app_context():
25-
yield app
26-
27-
287
def test_taskify_without_context():
298
def task_func():
309
pass
@@ -33,11 +12,11 @@ def task_func():
3312
assert isinstance(task_obj, gevent.Greenlet)
3413

3514

36-
def test_taskify_with_context(app_context):
15+
def test_taskify_with_context(app_ctx):
3716
def task_func():
3817
pass
3918

40-
with app_context.test_request_context():
19+
with app_ctx.test_request_context():
4120
task_obj = tasks.taskify(task_func)()
4221
assert isinstance(task_obj, gevent.Greenlet)
4322

tests/test_server_representations.py

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,6 @@
33
import pytest
44

55

6-
@pytest.fixture()
7-
def app(request):
8-
9-
app = Flask(__name__)
10-
11-
# pushes an application context manually
12-
ctx = app.app_context()
13-
ctx.push()
14-
15-
# bind the test life with the context through the
16-
request.addfinalizer(ctx.pop)
17-
return app
18-
19-
20-
@pytest.fixture()
21-
def debug_app(request):
22-
23-
app = Flask(__name__)
24-
app.debug = True
25-
26-
# pushes an application context manually
27-
ctx = app.app_context()
28-
ctx.push()
29-
30-
# bind the test life with the context through the
31-
request.addfinalizer(ctx.pop)
32-
return app
33-
34-
35-
@pytest.fixture()
36-
def app_context(app):
37-
with app.app_context():
38-
yield app
39-
40-
41-
@pytest.fixture()
42-
def app_context_debug(debug_app):
43-
with debug_app.app_context():
44-
yield debug_app
45-
46-
476
@pytest.fixture
487
def labthings_json_encoder():
498
return representations.LabThingsJSONEncoder
@@ -67,15 +26,15 @@ def test_encode_json(labthings_json_encoder):
6726
)
6827

6928

70-
def test_output_json(app_context):
29+
def test_output_json(app_ctx):
7130
data = {
7231
"a": "String",
7332
"b": 5,
7433
"c": [10, 20, 30, 40, 50],
7534
"d": {"a": "String", "b": 5, "c": [10, 20, 30, 40, 50]},
7635
}
7736

78-
with app_context.test_request_context():
37+
with app_ctx.test_request_context():
7938
response = representations.output_json(data, 200)
8039
assert isinstance(response, Response)
8140
assert response.status_code == 200
@@ -85,15 +44,15 @@ def test_output_json(app_context):
8544
)
8645

8746

88-
def test_pretty_output_json(app_context_debug):
47+
def test_pretty_output_json(app_ctx_debug):
8948
data = {
9049
"a": "String",
9150
"b": 5,
9251
"c": [10, 20, 30, 40, 50],
9352
"d": {"a": "String", "b": 5, "c": [10, 20, 30, 40, 50]},
9453
}
9554

96-
with app_context_debug.test_request_context():
55+
with app_ctx_debug.test_request_context():
9756
response = representations.output_json(data, 200)
9857
assert isinstance(response, Response)
9958
assert response.status_code == 200

0 commit comments

Comments
 (0)