From 423d13aff28f5d191ab8758196527f07e155518e Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Tue, 24 Jun 2014 20:12:30 -0400 Subject: [PATCH] Add basic unit tests This commit adds the first unit tests to the projects. Right now it's just testing a couple of utility methods. Mostly because they were the easiest tests to write. --- .testr.conf | 8 +++ requirements.txt | 1 + subunit2sql/tests/base.py | 33 ++++++++++++ subunit2sql/tests/subunit.py | 0 subunit2sql/tests/test_read_subunit.py | 26 ++++++++++ subunit2sql/tests/test_shell.py | 70 ++++++++++++++++++++++++++ tox.ini | 2 +- 7 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 .testr.conf delete mode 100644 subunit2sql/tests/subunit.py create mode 100644 subunit2sql/tests/test_read_subunit.py create mode 100644 subunit2sql/tests/test_shell.py diff --git a/.testr.conf b/.testr.conf new file mode 100644 index 0000000..87e3abe --- /dev/null +++ b/.testr.conf @@ -0,0 +1,8 @@ +[DEFAULT] +test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \ + OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} \ + OS_TEST_TIMEOUT=${OS_TEST_TIMEOUT:-500} \ + OS_TEST_LOCK_PATH=${OS_TEST_LOCK_PATH:-${TMPDIR:-'/tmp'}} \ + ${PYTHON:-python} -m subunit.run discover -t ./ ./subunit2sql/tests $LISTOPT $IDOPTION +test_id_option=--load-list $IDFILE +test_list_option=--list diff --git a/requirements.txt b/requirements.txt index 0f9e617..7caf183 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,5 +2,6 @@ alembic>=0.4.1 oslo.config>=1.2.0 oslo.db pbr>=0.6,<1.0 +python-subunit>=0.0.18 six>=1.5.2 SQLAlchemy>=0.7.8 diff --git a/subunit2sql/tests/base.py b/subunit2sql/tests/base.py index e69de29..846f77a 100644 --- a/subunit2sql/tests/base.py +++ b/subunit2sql/tests/base.py @@ -0,0 +1,33 @@ +# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import fixtures +import testtools + + +class TestCase(testtools.TestCase): + + true = ('True', 'true', '1', 'yes') + + def setUp(self): + super(TestCase, self).setUp() + if os.environ.get('OS_STDOUT_CAPTURE') in self.true: + stdout = self.useFixture(fixtures.StringStream('stdout')).stream + self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) + if os.environ.get('OS_STDERR_CAPTURE') in self.true: + stderr = self.useFixture(fixtures.StringStream('stderr')).stream + self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) diff --git a/subunit2sql/tests/subunit.py b/subunit2sql/tests/subunit.py deleted file mode 100644 index e69de29..0000000 diff --git a/subunit2sql/tests/test_read_subunit.py b/subunit2sql/tests/test_read_subunit.py new file mode 100644 index 0000000..3a75ebc --- /dev/null +++ b/subunit2sql/tests/test_read_subunit.py @@ -0,0 +1,26 @@ +# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import datetime + +from subunit2sql import read_subunit as subunit +from subunit2sql.tests import base + + +class TestReadSubunit(base.TestCase): + def test_get_duration(self): + dur = subunit.get_duration(datetime.datetime(1914, 6, 28, 10, 45, 0), + datetime.datetime(1914, 6, 28, 10, 45, 50)) + self.assertEqual(dur, '50.000000s') diff --git a/subunit2sql/tests/test_shell.py b/subunit2sql/tests/test_shell.py new file mode 100644 index 0000000..d02c0b1 --- /dev/null +++ b/subunit2sql/tests/test_shell.py @@ -0,0 +1,70 @@ +# Copyright (c) 2014 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import datetime + +import mock + +from subunit2sql import shell +from subunit2sql.tests import base + + +class TestShell(base.TestCase): + def test_run_totals(self): + fake_results = {} + # Fake Success + for num in range(100): + test_name = 'fake_test_' + str(num) + fake_results[test_name] = {'status': 'success'} + # Fake skips + for num in range(50): + test_name = 'fake_test_skip_' + str(num) + fake_results[test_name] = {'status': 'skip'} + # Fake skips + for num in range(16): + test_name = 'fake_test_fail_' + str(num) + fake_results[test_name] = {'status': 'fail'} + totals = shell.get_run_totals(fake_results) + self.assertEqual(totals['success'], 100) + self.assertEqual(totals['fails'], 16) + self.assertEqual(totals['skips'], 50) + + def test_running_avg(self): + fake_test = mock.MagicMock() + fake_test.success = 150 + fake_test.run_time = 30.452 + fake_result = { + 'start_time': datetime.datetime(1914, 6, 28, 10, 45, 0), + 'end_time': datetime.datetime(1914, 6, 28, 10, 45, 50), + } + fake_values = {} + result = shell.running_avg(fake_test, fake_values, fake_result) + # Let's do some arithmatic + expected_avg = ((150 * 30.452) + 50) / 151 + # Both values should be 30.581456953642384 + self.assertEqual(expected_avg, result['run_time']) + + def test_running_avg_no_prev(self): + fake_test = mock.MagicMock() + fake_test.success = 1 + fake_test.run_time = None + fake_result = { + 'start_time': datetime.datetime(1914, 6, 28, 10, 45, 0), + 'end_time': datetime.datetime(1914, 6, 28, 10, 45, 50), + } + fake_values = {} + result = shell.running_avg(fake_test, fake_values, fake_result) + expected_avg = 50 + self.assertEqual(expected_avg, result['run_time']) diff --git a/tox.ini b/tox.ini index f826fb7..fe7f347 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] minversion = 1.6 -envlist = py26,py27,py33,pep8 +envlist = py27,py34,pep8 skipsdist = True [testenv]