Skip to content

Commit

Permalink
Moved common fixtures into conftest.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Apr 14, 2020
1 parent dc58e20 commit c65aa71
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 68 deletions.
56 changes: 56 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import pytest
from flask import Flask


@pytest.fixture()
def app(request):

app = Flask(__name__)
app.config["TESTING"] = True

# pushes an application context manually
ctx = app.app_context()
ctx.push()

# bind the test life with the context through the
request.addfinalizer(ctx.pop)
return app


@pytest.fixture()
def debug_app(request):

app = Flask(__name__)
app.config["TESTING"] = True
app.debug = True

# pushes an application context manually
ctx = app.app_context()
ctx.push()

# bind the test life with the context through the
request.addfinalizer(ctx.pop)
return app


@pytest.fixture()
def app_ctx(app):
with app.app_context():
yield app


@pytest.fixture()
def app_ctx_debug(debug_app):
with debug_app.app_context():
yield debug_app


@pytest.fixture
def req_ctx(app):
with app.test_request_context() as ctx:
yield ctx


@pytest.fixture
def client(app):
return app.test_client()
25 changes: 2 additions & 23 deletions tests/test_core_tasks_pool.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
from labthings.core import tasks
from flask import Flask, Response
import pytest

import gevent


@pytest.fixture()
def app(request):

app = Flask(__name__)

# pushes an application context manually
ctx = app.app_context()
ctx.push()

# bind the test life with the context through the
request.addfinalizer(ctx.pop)
return app


@pytest.fixture()
def app_context(app):
with app.app_context():
yield app


def test_taskify_without_context():
def task_func():
pass
Expand All @@ -33,11 +12,11 @@ def task_func():
assert isinstance(task_obj, gevent.Greenlet)


def test_taskify_with_context(app_context):
def test_taskify_with_context(app_ctx):
def task_func():
pass

with app_context.test_request_context():
with app_ctx.test_request_context():
task_obj = tasks.taskify(task_func)()
assert isinstance(task_obj, gevent.Greenlet)

Expand Down
49 changes: 4 additions & 45 deletions tests/test_server_representations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,6 @@
import pytest


@pytest.fixture()
def app(request):

app = Flask(__name__)

# pushes an application context manually
ctx = app.app_context()
ctx.push()

# bind the test life with the context through the
request.addfinalizer(ctx.pop)
return app


@pytest.fixture()
def debug_app(request):

app = Flask(__name__)
app.debug = True

# pushes an application context manually
ctx = app.app_context()
ctx.push()

# bind the test life with the context through the
request.addfinalizer(ctx.pop)
return app


@pytest.fixture()
def app_context(app):
with app.app_context():
yield app


@pytest.fixture()
def app_context_debug(debug_app):
with debug_app.app_context():
yield debug_app


@pytest.fixture
def labthings_json_encoder():
return representations.LabThingsJSONEncoder
Expand All @@ -67,15 +26,15 @@ def test_encode_json(labthings_json_encoder):
)


def test_output_json(app_context):
def test_output_json(app_ctx):
data = {
"a": "String",
"b": 5,
"c": [10, 20, 30, 40, 50],
"d": {"a": "String", "b": 5, "c": [10, 20, 30, 40, 50]},
}

with app_context.test_request_context():
with app_ctx.test_request_context():
response = representations.output_json(data, 200)
assert isinstance(response, Response)
assert response.status_code == 200
Expand All @@ -85,15 +44,15 @@ def test_output_json(app_context):
)


def test_pretty_output_json(app_context_debug):
def test_pretty_output_json(app_ctx_debug):
data = {
"a": "String",
"b": 5,
"c": [10, 20, 30, 40, 50],
"d": {"a": "String", "b": 5, "c": [10, 20, 30, 40, 50]},
}

with app_context_debug.test_request_context():
with app_ctx_debug.test_request_context():
response = representations.output_json(data, 200)
assert isinstance(response, Response)
assert response.status_code == 200
Expand Down

0 comments on commit c65aa71

Please sign in to comment.