From 0d7fe979c2a6bf9aafdc5c152910c3dbd42b161b Mon Sep 17 00:00:00 2001 From: Federico Ressi Date: Mon, 4 Mar 2019 09:22:36 +0100 Subject: [PATCH] Use mock library to verify setUp and cleanUp are called Change-Id: I8731e3e676eeb8099dea9b13f524e72292911d7b --- tobiko/common/managers/fixture.py | 10 +++++----- tobiko/tests/test_fixture.py | 31 ++++++++++++++++--------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/tobiko/common/managers/fixture.py b/tobiko/common/managers/fixture.py index c1629f5ae..e4afc1a6c 100644 --- a/tobiko/common/managers/fixture.py +++ b/tobiko/common/managers/fixture.py @@ -162,11 +162,11 @@ class FixtureManager(object): def get_fixture(self, obj, init=init_fixture): name = get_object_name(obj) - _fixture = self.fixtures.get(name) - if _fixture is None: - self.fixtures[name] = _fixture = init(name=name, obj=obj) - assert isinstance(_fixture, fixtures.Fixture) - return _fixture + fixture = self.fixtures.get(name) + if fixture is None: + self.fixtures[name] = fixture = init(name=name, obj=obj) + assert isinstance(fixture, fixtures.Fixture) + return fixture FIXTURES = FixtureManager() diff --git a/tobiko/tests/test_fixture.py b/tobiko/tests/test_fixture.py index a5b04ea28..8ebfe244a 100644 --- a/tobiko/tests/test_fixture.py +++ b/tobiko/tests/test_fixture.py @@ -14,19 +14,14 @@ from __future__ import absolute_import import fixtures +import mock import tobiko from tobiko.tests import base class MyFixtureClass(fixtures.Fixture): - - def _setUp(self): - self.setup_executed = True - self.addCleanup(self._cleanUp) - - def _cleanUp(self): - self.cleanup_executed = True + pass MY_FIXTURE_NAME = __name__ + '.' + MyFixtureClass.__name__ @@ -58,11 +53,11 @@ class FixtureTypeTest(base.TobikoTest): def _test_setup_fixture(self, obj): fixture = tobiko.get_fixture(obj) - fixture.setup_executed = False + fixture.setUp = setup = mock.MagicMock() tobiko.setup_fixture(obj) - self.assertTrue(fixture.setup_executed) + setup.assert_called_once_with() def test_cleanup_fixture_by_name(self): self._test_cleanup_fixture(MY_FIXTURE_NAME) @@ -71,12 +66,14 @@ class FixtureTypeTest(base.TobikoTest): self._test_cleanup_fixture(MyFixtureClass) def _test_cleanup_fixture(self, obj): + cleanup = mock.MagicMock() fixture = tobiko.get_fixture(obj) fixture.setUp() + fixture.addCleanup(cleanup) tobiko.cleanup_fixture(obj) - self.assertTrue(fixture.cleanup_executed) + cleanup.assert_called_once_with() def test_list_required_fixtures_from_module(self): result = tobiko.list_required_fixtures([__name__]) @@ -108,14 +105,18 @@ class FixtureTypeTest(base.TobikoTest): def test_setup_required_fixtures(self, fixture_type=MyFixtureClass): fixture = tobiko.get_fixture(fixture_type) - fixture.setup_executed = False + fixture.setUp = setup = mock.MagicMock() + tobiko.setup_required_fixtures([self.id()]) - self.assertTrue(fixture.setup_executed) + + setup.assert_called_once_with() def test_cleanup_required_fixtures(self, fixture_type=MyFixtureClass): + cleanup = mock.MagicMock() fixture = tobiko.get_fixture(fixture_type) - fixture.cleanup_executed = False fixture.setUp() + fixture.addCleanup(cleanup) + tobiko.cleanup_required_fixtures([self.id()]) - self.assertTrue(fixture.setup_executed) - self.assertTrue(fixture.cleanup_executed) + + cleanup.assert_called_once_with()