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.
This commit is contained in:
Matthew Treinish 2014-06-24 20:12:30 -04:00
parent 81f84d66f9
commit 423d13aff2
7 changed files with 139 additions and 1 deletions

8
.testr.conf Normal file
View File

@ -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

View File

@ -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

View File

@ -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))

View File

@ -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')

View File

@ -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'])

View File

@ -1,6 +1,6 @@
[tox]
minversion = 1.6
envlist = py26,py27,py33,pep8
envlist = py27,py34,pep8
skipsdist = True
[testenv]