Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] plugin fixtures: return testrunner result #1676

Merged
merged 2 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .travis-data/test_plugin_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,7 @@ def test_stupid(self):
if __name__ == '__main__':
MODULE = sys.modules[__name__]
SUITE = unittest.defaultTestLoader.loadTestsFromModule(MODULE)
TestRunner().run(SUITE, backend=determine_backend())
RESULT = TestRunner().run(SUITE, backend=determine_backend())

EXIT_CODE = int(not RESULT.wasSuccessful())
sys.exit(EXIT_CODE)
18 changes: 12 additions & 6 deletions aiida/utils/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ def backend(self):

@backend.setter
def backend(self, backend):
if self.__is_running_on_test_profile:
raise FixtureError(
'backend cannot be changed after setting up the environment')

valid_backends = [BACKEND_DJANGO, BACKEND_SQLA]
if backend not in valid_backends:
raise ValueError('invalid backend {}, must be one of {}'.format(
Expand Down Expand Up @@ -387,24 +391,27 @@ def has_profile_open(self):


@contextmanager
def fixture_manager():
def fixture_manager(backend=BACKEND_DJANGO):
"""
Context manager for FixtureManager objects

Example test runner (unittest)::

with fixture_manager() as fixture_mgr:
with fixture_manager(backend) as fixture_mgr:
# ready for tests
# everything cleaned up

Example fixture (pytest)::

def aiida_profile():
with fixture_manager() as fixture_mgr:
with fixture_manager(backend) as fixture_mgr:
yield fixture_mgr

:param backend: database backend, either BACKEND_SQLA or BACKEND_DJANGO
"""
try:
if not _GLOBAL_FIXTURE_MANAGER.has_profile_open():
_GLOBAL_FIXTURE_MANAGER.backend = backend
_GLOBAL_FIXTURE_MANAGER.create_profile()
yield _GLOBAL_FIXTURE_MANAGER
finally:
Expand Down Expand Up @@ -467,6 +474,5 @@ def run(self, suite, backend=BACKEND_DJANGO):
"""
from aiida.utils.capturing import Capturing
with Capturing():
with fixture_manager() as manager:
manager.backend = backend
super(TestRunner, self).run(suite)
with fixture_manager(backend=backend):
return super(TestRunner, self).run(suite)