From 8b1441dd34762addd41668d2d4e1c31e49ff21db Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Fri, 23 Mar 2018 11:53:03 +0100 Subject: [PATCH] changed from unittest to py.test --- Makefile | 3 -- ordr/tests.py | 65 ------------------------------------------ ordr/tests/__init__.py | 51 +++++++++++++++++++++++++++++++++ pytest.ini | 3 -- setup.cfg | 2 ++ 5 files changed, 53 insertions(+), 71 deletions(-) delete mode 100644 ordr/tests.py create mode 100644 ordr/tests/__init__.py delete mode 100644 pytest.ini diff --git a/Makefile b/Makefile index b6e8651..4775771 100644 --- a/Makefile +++ b/Makefile @@ -55,9 +55,6 @@ lint: ## check style with flake8 test: ## run tests quickly with the default Python py.test -test-all: ## run tests on every Python version with tox - tox - coverage: ## check code coverage quickly with the default Python coverage run --source ordr -m pytest coverage report -m diff --git a/ordr/tests.py b/ordr/tests.py deleted file mode 100644 index a568769..0000000 --- a/ordr/tests.py +++ /dev/null @@ -1,65 +0,0 @@ -import unittest -import transaction - -from pyramid import testing - - -def dummy_request(dbsession): - return testing.DummyRequest(dbsession=dbsession) - - -class BaseTest(unittest.TestCase): - def setUp(self): - self.config = testing.setUp(settings={ - 'sqlalchemy.url': 'sqlite:///:memory:' - }) - self.config.include('.models') - settings = self.config.get_settings() - - from .models import ( - get_engine, - get_session_factory, - get_tm_session, - ) - - self.engine = get_engine(settings) - session_factory = get_session_factory(self.engine) - - self.session = get_tm_session(session_factory, transaction.manager) - - def init_database(self): - from .models.meta import Base - Base.metadata.create_all(self.engine) - - def tearDown(self): - from .models.meta import Base - - testing.tearDown() - transaction.abort() - Base.metadata.drop_all(self.engine) - - -class TestMyViewSuccessCondition(BaseTest): - - def setUp(self): - super(TestMyViewSuccessCondition, self).setUp() - self.init_database() - - from .models import MyModel - - model = MyModel(name='one', value=55) - self.session.add(model) - - def test_passing_view(self): - from .views.default import my_view - info = my_view(None, dummy_request(self.session)) - self.assertEqual(info['one'].name, 'one') - self.assertEqual(info['project'], 'Ordr') - - -class TestMyViewFailureCondition(BaseTest): - - def test_failing_view(self): - from .views.default import my_view - info = my_view(None, dummy_request(self.session)) - self.assertEqual(info.status_int, 500) diff --git a/ordr/tests/__init__.py b/ordr/tests/__init__.py new file mode 100644 index 0000000..41a4dd6 --- /dev/null +++ b/ordr/tests/__init__.py @@ -0,0 +1,51 @@ +import pytest +import transaction + +from pyramid import testing + + +APP_SETTINGS = { + 'sqlalchemy.url': 'sqlite:///:memory:', + } + + +@pytest.fixture(scope='session') +def app_config(): + ''' fixture for tests requiring a pyramid.testing setup ''' + with testing.testConfig(settings=APP_SETTINGS) as config: + config.include('pyramid_jinja2') + config.include('pyramid_listing') + yield config + + +@pytest.fixture(scope='session') +def dbsession(app_config): + ''' fixture for testing with database connection ''' + from ordr.models.meta import Base + from ordr.models import ( + get_engine, + get_session_factory, + get_tm_session + ) + + settings = app_config.get_settings() + engine = get_engine(settings) + session_factory = get_session_factory(engine) + session = get_tm_session(session_factory, transaction.manager) + Base.metadata.create_all(engine) + + yield session + + transaction.abort() + Base.metadata.drop_all(engine) + + +def test_passing_view(dbsession): + from ordr.views.default import my_view + from ordr.models import MyModel + model = MyModel(name='one', value=55) + dbsession.add(model) + info = my_view(None, testing.DummyRequest(dbsession=dbsession)) + assert info['one'].name == 'one' + assert info['project'] == 'Ordr' + diff --git a/pytest.ini b/pytest.ini deleted file mode 100644 index 7286498..0000000 --- a/pytest.ini +++ /dev/null @@ -1,3 +0,0 @@ -[pytest] -testpaths = ordr -python_files = *.py diff --git a/setup.cfg b/setup.cfg index 8d7bf4d..9ba64ba 100644 --- a/setup.cfg +++ b/setup.cfg @@ -23,5 +23,7 @@ hang_closing = True test = pytest [tool:pytest] +testpaths = ordr +python_files = *.py collect_ignore = ['setup.py']