Skip to content

Commit bad2159

Browse files
committed
Use fixtures and addCleanup instead of tearDown
Nothing ensures tearDown call as tearDown is called only if test succeeds. This change replaces tearDown use with: * addCleanup use to stop mocks * EnvFixture which ensures to unmock environment thanks to useFixture. Change-Id: I1ff422e6a7585bc48b04b8f5c4cc1e7e9ddab1bc
1 parent 97492c1 commit bad2159

1 file changed

Lines changed: 25 additions & 34 deletions

File tree

openstackclient/tests/test_shell.py

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#
1515

1616
import copy
17+
import fixtures
1718
import mock
1819
import os
1920
import testtools
@@ -161,19 +162,33 @@ def fake_execute(shell, cmd):
161162
return shell.run(cmd.split())
162163

163164

165+
class EnvFixture(fixtures.Fixture):
166+
"""Environment Fixture.
167+
168+
This fixture replaces os.environ with provided env or an empty env.
169+
"""
170+
171+
def __init__(self, env=None):
172+
self.new_env = env or {}
173+
174+
def _setUp(self):
175+
self.orig_env, os.environ = os.environ, self.new_env
176+
self.addCleanup(self.revert)
177+
178+
def revert(self):
179+
os.environ = self.orig_env
180+
181+
164182
class TestShell(utils.TestCase):
165183

166184
def setUp(self):
167185
super(TestShell, self).setUp()
168186
patch = "openstackclient.shell.OpenStackShell.run_subcommand"
169187
self.cmd_patch = mock.patch(patch)
170188
self.cmd_save = self.cmd_patch.start()
189+
self.addCleanup(self.cmd_patch.stop)
171190
self.app = mock.Mock("Test Shell")
172191

173-
def tearDown(self):
174-
super(TestShell, self).tearDown()
175-
self.cmd_patch.stop()
176-
177192
def _assert_initialize_app_arg(self, cmd_options, default_args):
178193
"""Check the args passed to initialize_app()
179194
@@ -285,11 +300,7 @@ class TestShellHelp(TestShell):
285300

286301
def setUp(self):
287302
super(TestShellHelp, self).setUp()
288-
self.orig_env, os.environ = os.environ, {}
289-
290-
def tearDown(self):
291-
super(TestShellHelp, self).tearDown()
292-
os.environ = self.orig_env
303+
self.useFixture(EnvFixture())
293304

294305
@testtools.skip("skip until bug 1444983 is resolved")
295306
def test_help_options(self):
@@ -310,11 +321,7 @@ class TestShellOptions(TestShell):
310321

311322
def setUp(self):
312323
super(TestShellOptions, self).setUp()
313-
self.orig_env, os.environ = os.environ, {}
314-
315-
def tearDown(self):
316-
super(TestShellOptions, self).tearDown()
317-
os.environ = self.orig_env
324+
self.useFixture(EnvFixture())
318325

319326
def _test_options_init_app(self, test_opts):
320327
for opt in test_opts.keys():
@@ -402,11 +409,7 @@ def setUp(self):
402409
"OS_TOKEN": DEFAULT_TOKEN,
403410
"OS_AUTH_URL": DEFAULT_AUTH_URL,
404411
}
405-
self.orig_env, os.environ = os.environ, env.copy()
406-
407-
def tearDown(self):
408-
super(TestShellTokenAuthEnv, self).tearDown()
409-
os.environ = self.orig_env
412+
self.useFixture(EnvFixture(env.copy()))
410413

411414
def test_env(self):
412415
flag = ""
@@ -450,11 +453,7 @@ def setUp(self):
450453
"OS_TOKEN": DEFAULT_TOKEN,
451454
"OS_URL": DEFAULT_SERVICE_URL,
452455
}
453-
self.orig_env, os.environ = os.environ, env.copy()
454-
455-
def tearDown(self):
456-
super(TestShellTokenEndpointAuthEnv, self).tearDown()
457-
os.environ = self.orig_env
456+
self.useFixture(EnvFixture(env.copy()))
458457

459458
def test_env(self):
460459
flag = ""
@@ -501,11 +500,7 @@ def setUp(self):
501500
"OS_VOLUME_API_VERSION": DEFAULT_VOLUME_API_VERSION,
502501
"OS_NETWORK_API_VERSION": DEFAULT_NETWORK_API_VERSION,
503502
}
504-
self.orig_env, os.environ = os.environ, env.copy()
505-
506-
def tearDown(self):
507-
super(TestShellCli, self).tearDown()
508-
os.environ = self.orig_env
503+
self.useFixture(EnvFixture(env.copy()))
509504

510505
def test_shell_args_no_options(self):
511506
_shell = make_shell()
@@ -719,11 +714,7 @@ def setUp(self):
719714
env = {
720715
'OS_REGION_NAME': 'occ-env',
721716
}
722-
self.orig_env, os.environ = os.environ, env.copy()
723-
724-
def tearDown(self):
725-
super(TestShellCliEnv, self).tearDown()
726-
os.environ = self.orig_env
717+
self.useFixture(EnvFixture(env.copy()))
727718

728719
@mock.patch("os_client_config.config.OpenStackConfig._load_vendor_file")
729720
@mock.patch("os_client_config.config.OpenStackConfig._load_config_file")

0 commit comments

Comments
 (0)