diff --git a/tests/.configs/pytest-py27.ini b/tests/.configs/pytest-py27.ini index b67e2860..14d76ae7 100644 --- a/tests/.configs/pytest-py27.ini +++ b/tests/.configs/pytest-py27.ini @@ -3,3 +3,4 @@ testpaths = tests/unit python_files = test_*_py2_py3.py filterwarnings = ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning + ignore:Module \"dependency_injector.ext.flask\" is deprecated since version 4\.0\.0:DeprecationWarning diff --git a/tests/.configs/pytest-py35.ini b/tests/.configs/pytest-py35.ini index 7fe9953c..81330704 100644 --- a/tests/.configs/pytest-py35.ini +++ b/tests/.configs/pytest-py35.ini @@ -3,3 +3,4 @@ testpaths = tests/unit python_files = test_*_py3.py filterwarnings = ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning + ignore:Module \"dependency_injector.ext.flask\" is deprecated since version 4\.0\.0:DeprecationWarning diff --git a/tests/.configs/pytest.ini b/tests/.configs/pytest.ini index 0ae166c3..3640bffd 100644 --- a/tests/.configs/pytest.ini +++ b/tests/.configs/pytest.ini @@ -3,3 +3,4 @@ testpaths = tests/unit python_files = test_*_py3*.py filterwarnings = ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\.0\.0:DeprecationWarning + ignore:Module \"dependency_injector.ext.flask\" is deprecated since version 4\.0\.0:DeprecationWarning diff --git a/tests/unit/ext/test_flask_py2_py3.py b/tests/unit/ext/test_flask_py2_py3.py index 60ca5408..e64de165 100644 --- a/tests/unit/ext/test_flask_py2_py3.py +++ b/tests/unit/ext/test_flask_py2_py3.py @@ -1,11 +1,10 @@ -"""Dependency injector Flask extension unit tests.""" - -import unittest -from flask import Flask, url_for -from flask.views import MethodView +"""Flask extension tests.""" from dependency_injector import containers from dependency_injector.ext import flask +from flask import Flask, url_for +from flask.views import MethodView +from pytest import fixture def index(): @@ -30,47 +29,47 @@ class ApplicationContainer(containers.DeclarativeContainer): test_class_view = flask.ClassBasedView(Test) -def create_app(): +@fixture +def app(): container = ApplicationContainer() app = container.app() app.container = container + app.config["SERVER_NAME"] = "test-server.com" app.add_url_rule("/", view_func=container.index_view.as_view()) app.add_url_rule("/test", "test-test", view_func=container.test_view.as_view()) app.add_url_rule("/test-class", view_func=container.test_class_view.as_view("test-class")) return app -class ApplicationTests(unittest.TestCase): +@fixture +def client(app): + with app.test_client() as client: + yield client - def setUp(self): - self.app = create_app() - self.app.config["SERVER_NAME"] = "test-server.com" - self.client = self.app.test_client() - self.client.__enter__() - def tearDown(self): - self.client.__exit__(None, None, None) +def test_index(client): + response = client.get("/") - def test_index(self): - response = self.client.get("/") + assert response.status_code == 200 + assert response.data == b"Hello World!" - self.assertEqual(response.status_code, 200) - self.assertEqual(response.data, b"Hello World!") - def test_test(self): - response = self.client.get("/test") +def test_test(client): + response = client.get("/test") - self.assertEqual(response.status_code, 200) - self.assertEqual(response.data, b"Test!") + assert response.status_code == 200 + assert response.data == b"Test!" - def test_test_class_based(self): - response = self.client.get("/test-class") - self.assertEqual(response.status_code, 200) - self.assertEqual(response.data, b"Test class-based!") +def test_test_class_based(client): + response = client.get("/test-class") - def test_endpoints(self): - with self.app.app_context(): - self.assertEqual(url_for("index"), "http://test-server.com/") - self.assertEqual(url_for("test-test"), "http://test-server.com/test") - self.assertEqual(url_for("test-class"), "http://test-server.com/test-class") + assert response.status_code == 200 + assert response.data == b"Test class-based!" + + +def test_endpoints(app): + with app.app_context(): + assert url_for("index") == "http://test-server.com/" + assert url_for("test-test") == "http://test-server.com/test" + assert url_for("test-class") == "http://test-server.com/test-class"