From 06acb0c009ac53777884cf5b0aa8942184d06622 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Mon, 24 Dec 2012 21:49:34 -0600 Subject: [PATCH] Move from unittest2 to testtools Part of blueprint grizzly-testtools Change-Id: I13e068ca156f12114eaa3a65bdb557e4eb2c988d --- tests/test_client.py | 3 --- tests/test_shell.py | 43 +++++++++++++++++++++++------------------- tests/test_utils.py | 1 + tests/utils.py | 4 ++-- tests/v1/test_shell.py | 24 ++++++++++++++--------- tools/test-requires | 7 ++++--- 6 files changed, 46 insertions(+), 36 deletions(-) diff --git a/tests/test_client.py b/tests/test_client.py index f5e4bab59..77c72973b 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -6,9 +6,6 @@ from tests import utils class ClientTest(utils.TestCase): - def setUp(self): - pass - def test_get_client_class_v1(self): output = cinderclient.client.get_client_class('1') self.assertEqual(output, cinderclient.v1.client.Client) diff --git a/tests/test_shell.py b/tests/test_shell.py index dc8628150..ce3d4294a 100644 --- a/tests/test_shell.py +++ b/tests/test_shell.py @@ -1,7 +1,11 @@ import cStringIO import os +import re import sys +import fixtures +from testtools import matchers + from cinderclient import exceptions import cinderclient.shell from tests import utils @@ -9,16 +13,19 @@ from tests import utils class ShellTest(utils.TestCase): + FAKE_ENV = { + 'OS_USERNAME': 'username', + 'OS_PASSWORD': 'password', + 'OS_TENANT_NAME': 'tenant_name', + 'OS_AUTH_URL': 'http://no.where', + } + # Patch os.environ to avoid required auth info. def setUp(self): - global _old_env - fake_env = { - 'OS_USERNAME': 'username', - 'OS_PASSWORD': 'password', - 'OS_TENANT_NAME': 'tenant_name', - 'OS_AUTH_URL': 'http://no.where', - } - _old_env, os.environ = os.environ, fake_env.copy() + super(ShellTest, self).setUp() + for var in self.FAKE_ENV: + self.useFixture(fixtures.EnvironmentVariable(var, + self.FAKE_ENV[var])) def shell(self, argstr): orig = sys.stdout @@ -36,28 +43,26 @@ class ShellTest(utils.TestCase): return out - def tearDown(self): - global _old_env - os.environ = _old_env - def test_help_unknown_command(self): self.assertRaises(exceptions.CommandError, self.shell, 'help foofoo') def test_help(self): required = [ - '^usage: ', - '(?m)^\s+create\s+Add a new volume.', - '(?m)^See "cinder help COMMAND" for help on a specific command', + '.*?^usage: ', + '.*?(?m)^\s+create\s+Add a new volume.', + '.*?(?m)^See "cinder help COMMAND" for help on a specific command', ] help_text = self.shell('help') for r in required: - self.assertRegexpMatches(help_text, r) + self.assertThat(help_text, + matchers.MatchesRegex(r, re.DOTALL|re.MULTILINE)) def test_help_on_subcommand(self): required = [ - '^usage: cinder list', - '(?m)^List all the volumes.', + '.*?^usage: cinder list', + '.*?(?m)^List all the volumes.', ] help_text = self.shell('help list') for r in required: - self.assertRegexpMatches(help_text, r) + self.assertThat(help_text, + matchers.MatchesRegex(r, re.DOTALL|re.MULTILINE)) diff --git a/tests/test_utils.py b/tests/test_utils.py index 39fb2c913..22a167afa 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -45,6 +45,7 @@ class FakeManager(base.ManagerWithFind): class FindResourceTestCase(test_utils.TestCase): def setUp(self): + super(FindResourceTestCase, self).setUp() self.manager = FakeManager(None) def test_find_none(self): diff --git a/tests/utils.py b/tests/utils.py index 7a4c3df68..a2fd0347b 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,9 +1,9 @@ -import unittest2 +import testtools import requests -class TestCase(unittest2.TestCase): +class TestCase(testtools.TestCase): TEST_REQUEST_BASE = { 'config': {'danger_mode': False}, 'verify': True, diff --git a/tests/v1/test_shell.py b/tests/v1/test_shell.py index 0bb8de8db..816bfddf5 100644 --- a/tests/v1/test_shell.py +++ b/tests/v1/test_shell.py @@ -17,6 +17,8 @@ import os +import fixtures + from cinderclient import client from cinderclient import shell from tests.v1 import fakes @@ -25,17 +27,21 @@ from tests import utils class ShellTest(utils.TestCase): + FAKE_ENV = { + 'CINDER_USERNAME': 'username', + 'CINDER_PASSWORD': 'password', + 'CINDER_PROJECT_ID': 'project_id', + 'OS_COMPUTE_API_VERSION': '1.1', + 'CINDER_URL': 'http://no.where', + } + # Patch os.environ to avoid required auth info. def setUp(self): """Run before each test.""" - self.old_environment = os.environ.copy() - os.environ = { - 'CINDER_USERNAME': 'username', - 'CINDER_PASSWORD': 'password', - 'CINDER_PROJECT_ID': 'project_id', - 'OS_COMPUTE_API_VERSION': '1.1', - 'CINDER_URL': 'http://no.where', - } + super(ShellTest, self).setUp() + for var in self.FAKE_ENV: + self.useFixture(fixtures.EnvironmentVariable(var, + self.FAKE_ENV[var])) self.shell = shell.OpenStackCinderShell() @@ -44,7 +50,6 @@ class ShellTest(utils.TestCase): client.get_client_class = lambda *_: fakes.FakeClient def tearDown(self): - os.environ = self.old_environment # For some method like test_image_meta_bad_action we are # testing a SystemExit to be thrown and object self.shell has # no time to get instantatiated which is OK in this case, so @@ -54,6 +59,7 @@ class ShellTest(utils.TestCase): #HACK(bcwaldon): replace this when we start using stubs client.get_client_class = self.old_get_client_class + super(ShellTest, self).tearDown() def run_command(self, cmd): self.shell.main(cmd.split()) diff --git a/tools/test-requires b/tools/test-requires index 10b90c438..e36dc9498 100644 --- a/tools/test-requires +++ b/tools/test-requires @@ -1,10 +1,11 @@ - distribute>=0.6.24 + +fixtures mock nose +nosehtmloutput nosexcover openstack.nose_plugin -nosehtmloutput pep8==1.3.3 sphinx>=1.1.2 -unittest2 +testtools>=0.9.22